Copy link to clipboard
Copied
Hi Everyone,
I am trying to add keywords to multiple jpg images in a folder. But each jpeg file will have different keywords. Here's the script I wrote, but it's not working as I wanted. Could you please help modify it?
//Check if ExternalObject.AdobeXMPScript is available, and if not, create it
if (!ExternalObject.AdobeXMPScript) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
// prompt the user to select the folder that contains the images to modify
var folderPath = Folder.selectDialog("Select the folder that contains the images you want to modify");
if (folderPath) { // check if a folder was selected
// prompt the user to enter the keywords to add
var keywords = prompt("Enter the keywords you want to add to the metadata, separated by commas:");
if (keywords) { // check if the user entered keywords
// get an array of all the files in the folder
var folder = new Folder(folderPath);
var files = folder.getFiles();
// loop through each file in the folder
for (var i = 0; i < files.length; i++) {
// open the file
var file = files[i];
var doc = app.open(file);
// add the desired keywords to the metadata
var xmp = new XMPMeta(app.activeDocument.xmpMetadata.rawData);
var subjectArray = xmp.getProperty(XMPConst.NS_DC, "subject");
if (subjectArray == null) {
subjectArray = new Array();
}
var newKeywords = keywords.split(","); // split the keywords into an array
for (var j = 0; j < newKeywords.length; j++) {
var keyword = newKeywords[j].trim(); // trim leading and trailing whitespace
subjectArray[subjectArray.length] = keyword; // add the keyword to the subject array
}
// save the metadata to the file
var xmpAsString = xmp.serialize(); // convert the XMPMeta object to a string
app.activeDocument.info.xmpMetadata = xmpAsString; // set the metadata of the active document
doc.close(SaveOptions.SAVECHANGES);
}
// display a message when the script is finished
alert("Metadata update complete.");
}
}
Two points to be noted. The jpegs are not looping. And keywords are not getting saved in the jpeg after running the script. Kindly help.
Also would be great if the predefined keywords appear as a checkbox rather than the user entering it. Thanks in advance
@Vibi Dev , please try this:
// prompt the user to select the folder that contains the images to modify
var folderPath = Folder.selectDialog("Select the folder that contains the images you want to modify");
if (folderPath) { // check if a folder was selected
// get an array of all the files in the folder
var folder = new Folder(folderPath);
var files = folder.getFiles(/\.(jpg|jpeg)$/i);
// loop through each file in the folder
for (var i = 0; i < files.length; i++) {
// open the ...
Copy link to clipboard
Copied
@Vibi Dev – I'd suggest that you simply use Adobe Bridge's Keywords panel, rather than reinventing the wheel.
When testing your script I had to change:
var files = folder.getFiles();
to:
var files = folder.getFiles(/\.(jpg|jpeg)$/i);
To overcome an error (Mac).
P.S. It is far more efficient to write the dc:subject metadata directly into the file object than actually opening the file.
Copy link to clipboard
Copied
It's still not working with the changes made. Could you please help?
Copy link to clipboard
Copied
I'm not sure if I can, however, I'd suggest that you simply use Adobe Bridge's Keywords panel, rather than reinventing the wheel.
Copy link to clipboard
Copied
I understand but wanted to stick to one tool. That's the reason for building this out.
Copy link to clipboard
Copied
I understand but wanted to stick to one tool. That's the reason for building this out.
By @Vibi Dev
I thought that would be your answer... There has to be a good reason to go through the pain when Bridge exists.
So, as I mentioned, opening a file to update metadata is not very efficient, when Adobe apps can update metadata directly in a file without opening the pixel data. This is even more important for images such as JPEG that use lossy compression.
You mention that each JPEG will have different keyword metadata, but how will you make that work when looping over all files in a folder? This would appear to contradict the loop, unless there were multiple conditionals or something, but this would probably get pretty messy.
Let me think about this, I have to be blunt, this isn't really a workflow that I would personally invest much time in.
Copy link to clipboard
Copied
Thanks for the immediate response, Stephen. I have plenty of images that need to be tagged with multiple keywords but might differ for each jpeg. But the keyword set is the same for all the images. One image will have 5/10 keywords, other will have 9/10 keywords. I thought of automating this in photoshop so that keywords are added to the jpegs and then put those jpegs in the bridge to convert to an excel sheet. Please help if there's a better solution.
Copy link to clipboard
Copied
OK, I think that something like this would work:
This I might possibly be able to help with, however, I haven't done a whole lot of metadata scripting though...
Copy link to clipboard
Copied
Thanks, Stephen. Could you please help in modifying the above script? Please let me know if that's time-consuming for you.
Copy link to clipboard
Copied
I wouldn't bother trying, it's easier for me to go from scratch.
Copy link to clipboard
Copied
I really appreciate your time on this, Stephen. Since you are starting from scratch, it would be great if you keep predefined keywords (atleast 5) appearing in a checkbox model rather than the user entering it. Also wanted to know if exporting it as an excel file is also possible along with these steps. So it adds meta keywords to each jpeg and finally exports an excel sheet with all the images that have been modified. Thanks in advance.
Copy link to clipboard
Copied
Baby steps!
I would start with a "proof of concept" with a prompt pre-filled with all keywords. Unwanted keywords would then be removed.
If this all works, then a second version would need to be created with a scriptUI GUI with checkboxes.
Finally the export to csv would be added as a third version.
One has to crawl, then walk - before running!
Copy link to clipboard
Copied
Sounds great Stephen. Thank you so much 😊
Copy link to clipboard
Copied
What about existing keywords?
Should those be replaced or added to?
Copy link to clipboard
Copied
The existing one if any, can be replaced. Thanks
Copy link to clipboard
Copied
I am not sure if this was the problem or if the issue is assigning different keywords to some images; as for the checkboxes I expect @Stephen Marsh might post something anyway.
edited
// prompt the user to select the folder that contains the images to modify
var folderPath = Folder.selectDialog("Select the folder that contains the images you want to modify");
if (folderPath) { // check if a folder was selected
// prompt the user to enter the keywords to add
var keywords = prompt("Enter the keywords you want to add to the metadata, separated by commas:");
if (keywords) { // check if the user entered keywords
// get an array of all the files in the folder
var folder = new Folder(folderPath);
var files = folder.getFiles(/\.(jpg|jpeg)$/i);
// loop through each file in the folder
for (var i = 0; i < files.length; i++) {
// open the file
var file = files[i];
// add the desired keywords to the metadata
setSubject( file, keywords.split(","));
}
// display a message when the script is finished
alert("Metadata update complete.");
}
};
////// based on code by paul riggott //////
function setSubject( file, anArray ){
// function setSubject( file, descStr ){
if ( !ExternalObject.AdobeXMPScript ) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
var xmpf = new XMPFile( File(file).fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE );
var xmp = xmpf.getXMP();
xmp.deleteProperty(XMPConst.NS_DC, "subject");
for(var s in anArray){
xmp.appendArrayItem(XMPConst.NS_DC, "subject", anArray[s], 0,XMPConst.PROP_IS_ARRAY);
};
//xmp.setLocalizedText( XMPConst.NS_DC, "subject", null, "x-default", descStr );
if (xmpf.canPutXMP( xmp )) {
xmpf.putXMP( xmp );
}
xmpf.closeFile( XMPConst.CLOSE_UPDATE_SAFELY );
};
Copy link to clipboard
Copied
You beat me to it, however, I don't believe that it works as intended...
We need to have an array as input, perhaps something like this:
var keys = new Array;
var keysPrompt = prompt("Remove any keyword which isn't required:", "Key1,Key2,Key3");
keys.push(keysPrompt);
keys = (keysPrompt.split(','));
Then write a metadata array, not a single flat entry.
Description and Subject metadata are different in this respect.
I can update an open file metadata, but the file needs to be saved. I don't want to do that, I want to write the metadata directly.
I'm still trying to adapt various Bridge scripts as I can't find any Photoshop scripts for working with metadata arrays which directly update the metadata.
And no, I'm a long way from any scriptUI checkboxes, baby steps!
Copy link to clipboard
Copied
My bad!
I amended the code, if you have time please test it. (Edit: The entries need to be separated by commas.)
Copy link to clipboard
Copied
I'm still trying to adapt various Bridge scripts as I can't find any Photoshop scripts for working with metadata arrays which directly update the metadata.
The amended Script I posted (based on code by Paul Riggott) seems to do that.
I ran it from Photoshop and the Keywords reflected the entry when next opening the jpgs in the selected Folder.
Copy link to clipboard
Copied
It’s not updating for me...
If I enter:
1,2,3
The raw metadata should show the following:
<dc:subject>
<rdf:Bag>
<rdf:li>1</rdf:li>
<rdf:li>2</rdf:li>
<rdf:li>3</rdf:li>
</rdf:Bag>
</dc:subject>
Copy link to clipboard
Copied
Strange, if I enter »1,2,3« in the prompt I get the three numbers in the Keywords when I open the images from the selected Folder subsequently.
Copy link to clipboard
Copied
It is strange indeed! I have shut down and restarted, tried in 2021 and 2023 with the same result, no keywords!
Copy link to clipboard
Copied
It is strange indeed! I have shut down and restarted, tried in 2021 and 2023 with the same result, no keywords!
By @Stephen Marsh
Well, I am baffled.
But I expect if you should pursue the issue with the dialog you will solve that issue along the way …
Copy link to clipboard
Copied
I'm going to have to give up for now, until I can find a script to update the subject metadata directly, I can't go any further.
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now