Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Adding Meta keywords to jpg images

Participant ,
Mar 15, 2023 Mar 15, 2023

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

TOPICS
Actions and scripting
9.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 16, 2023 Mar 16, 2023

@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 
...
Translate
Adobe
Community Expert ,
Mar 15, 2023 Mar 15, 2023

@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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 15, 2023 Mar 15, 2023

It's still not working with the changes made. Could you please help? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2023 Mar 15, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 15, 2023 Mar 15, 2023

I understand but wanted to stick to one tool. That's the reason for building this out. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2023 Mar 15, 2023
quote

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 15, 2023 Mar 15, 2023

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

OK, I think that something like this would work:

 

  1. Open a folder of docs, loop over one doc at a time to open the document (using Photoshop to visually preview)
  2. Bring up the metadata prompt to add the required keywords from a fixed range of possible keywords
  3. Apply the keyword metadata directly to the open document's XMP metadata
  4. Close the open doc without saving (there is no need and this should be avoided for JPEG files), open the next doc in the loop and repeat

 

This I might possibly be able to help with, however, I haven't done a whole lot of metadata scripting though...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 16, 2023 Mar 16, 2023

Thanks, Stephen. Could you please help in modifying the above script? Please let me know if that's time-consuming for you. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

I wouldn't bother trying, it's easier for me to go from scratch.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 16, 2023 Mar 16, 2023

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 16, 2023 Mar 16, 2023

Sounds great Stephen. Thank you so much 😊 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

What about existing keywords? 

Should those be replaced or added to? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 16, 2023 Mar 16, 2023

The existing one if any, can be replaced. Thanks 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

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 );
  };

 

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

@c.pfaffenbichler 

 

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!

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

My bad! 

I amended the code, if you have time please test it. (Edit: The entries need to be separated by commas.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

@c.pfaffenbichler 

 

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>

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

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. 

Screenshot 2023-03-16 at 13.06.16.pngScreenshot 2023-03-16 at 13.06.31.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

It is strange indeed! I have shut down and restarted, tried in 2021 and 2023 with the same result, no keywords!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023
quote

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 … 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2023 Mar 16, 2023

xmpMetaDataAddKeywordsWithoutOpeningImage.gif

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines