Site logo

Write keywords into the metadata in Bridge

Here's a function that writes keywords into the metadata. It creates a temporary metadata template file and then deletes it after use. It works on both Macintosh (tested on macOS 10.13.6, Bridge CC 2018) and Windows (tested on Windows 10, Bridge 2022).

main();

function main() {
	var sel = app.document.selections[0];
	var md = sel.metadata;
	var keywords = ["one", "two", "three"];
	SetKeywords(md, keywords);
}

function SetKeywords(metadata, keywords) { 
	var strTmpl = "TempTmpl"; 
	var strUser = Folder.userData.absoluteURI; 
	var filTmpl = new File(strUser + "/Adobe/XMP/Metadata Templates/" + strTmpl + ".xmp"); 
	var i = 0; 
	var fResult = false; 

try {
	if (filTmpl.exists) filTmpl.remove();
	fResult = filTmpl.open("w"); 
		if (fResult) {
			filTmpl.writeln(""); 
			filTmpl.writeln(" "); 
			filTmpl.writeln(" "); 
			filTmpl.writeln(" "); 
			filTmpl.writeln(" ");
			
			if (keywords != null) {
				for (i = 0; i < keywords.length; ++i) {
					filTmpl.writeln(" ", keywords[i], "");
				}
			
				filTmpl.writeln(" ");
				filTmpl.writeln(" ");
				filTmpl.writeln(" ");
				filTmpl.writeln(" ");
				filTmpl.writeln("");
				fResult = filTmpl.close();
				metadata.applyMetadataTemplate(strTmpl, "replace");
				filTmpl.remove();
			}
		}
	} 
	catch(e) {
		fResult = false;
	} 

	return fResult; 
}

Click here to download the function.