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.8K
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 ,
May 06, 2023 May 06, 2023
LATEST

Please explain what your exact problem is. 

And please don’t post unrelated links lest you be suspected of spamming, 

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