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

Photoshop Script to Add Missing Resolution Metadata to Exported JPEG Files

Community Expert ,
Nov 10, 2022 Nov 10, 2022

Copy link to clipboard

Copied

I have been tinkering and have the following two scripts to offer to the community. 

 

This may be desired when making use of Photoshop's Export features which are designed to strip irrelevant metadata that is not required for web/device viewing. Some users still wish to have the resolution metadata included, but do not wish to use Save As/Save As a Copy which includes all metadata.

 

I haven't done a lot of metadata scripting, so if there are any inconsistencies please let me know. I have always used ExifTool for this task, wondering why nobody had scripted this before.

 

The first script will add 300 PPI metadata to a single selected JPEG file (the metadata is injected directly into the file without having to open/decompress the image data).

 

The second script will add 300 PPI metadata into all root-level JPEG files in a nominated directory/folder (again, the metadata is injected directly into the file without having to open/decompress the image data).

 

If possible, it would be great if somebody could modify the code to use multiselect = true so that multiple selected files can be processed (I tried but I had issues processing the array of file objects returned).

 

Bridge scripting is the obvious place to do this, however, I started in Photoshop as I am more comfortable there. I'll see if it is possible for me to adapt an existing Bridge script for this purpose.

 

/*
Set the selected JPEG resolution metadata to 300 PPI
v1.0, 10th November 2022, Stephen Marsh
v1.1, 29th September 2024 - Corrected the cancel button logic
Add the stripped resolution metadata back to exported JPEG files
https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-to-add-missing-resolution-metadata-to-exported-jpeg-files/td-p/13335061
*/

#target photoshop

(function () {

  var selectFile = app.openDialog();
  if (selectFile === null || selectFile.length === 0) {  // Check if the user cancelled file selection
    alert('Script cancelled!');
    return;
  }

  var selectedFile = File(selectFile[0]);

  if (/\.(jpg|jpeg)$/i.test(selectedFile.name) === true) {
    var result = setResMeta(selectedFile, 300); // Resolution value in PPI
    if (result === null) {
      alert('Script cancelled or failed!');
      return;
    }
    alert("Resolution metadata set to 300 ppi");
  } else {
    alert("The file isn't a JPEG!");
  }

  function setResMeta(file, res) {
    try {
      if (!ExternalObject.AdobeXMPScript) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
      var xmpFile = new XMPFile(file.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
      var xmp = xmpFile.getXMP();

      xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'XResolution', res), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'DisplayedUnitsX', 'inches'), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'YResolution', res), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'DisplayedUnitsY', 'inches'), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/tiff/1.0/', 'XResolution', res), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/tiff/1.0/', 'YResolution', res), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/tiff/1.0/', 'ResolutionUnit', 2), xmp.rawData = xmp.serialize();

      if (xmpFile.canPutXMP(xmp)) {
        xmpFile.putXMP(xmp);
        xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
        return true;  // Success
      } else {
        xmpFile.closeFile();
        return null;  // Failure, cannot put XMP
      }
    } catch (e) {
      alert('An error occurred: ' + e.message);
      return null;  // Error occurred
    }
  }

}());

 

 

 

/*
Set all JPEG resolution metadata in selected top-level folder to 300 PPI
v1.0, 10th November 2022, Stephen Marsh
v1.1, 29th September 2024 - Corrected the cancel button logic
Add the stripped resolution metadata back to exported JPEG files
https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-to-add-missing-resolution-metadata-to-exported-jpeg-files/td-p/13335061
*/

#target photoshop

(function() {
  var inputFolder = Folder.selectDialog("Please select folder to process");
  if (inputFolder === null) {
    alert('Script cancelled!');
    return;
  }

  var fileList = inputFolder.getFiles(/\.(jpg|jpeg)$/i);

  var timeDiff = {
    startTime: null,
    setStartTime: function () {
      var d = new Date();
      this.startTime = d.getTime();
    },
    getDiff: function () {
      var d = new Date();
      return (d.getTime() - this.startTime) / 1000; // return time difference in seconds
    }
  };

  timeDiff.setStartTime();

  var counter = 0;

  for (var a = 0; a < fileList.length; a++) {
    var result = setResMeta(fileList[a], 300); // Resolution value in PPI
    if (result !== null) {
      counter++;  // Increment counter only if file was successfully processed
    } else {
      $.writeln('Failed to process: ' + fileList[a].name);  // Log the failed file
    }
  }

  alert("Resolution metadata set to 300 ppi" +
    "\rFiles selected: " + fileList.length +
    "\rFiles processed: " + counter +
    "\rTime elapsed: " + timeDiff.getDiff() + " seconds"
  );

  function setResMeta(file, res) {
    try {
      if (!ExternalObject.AdobeXMPScript) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
      var xmpFile = new XMPFile(File(file).fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
      var xmp = xmpFile.getXMP();

      xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'XResolution', res), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'DisplayedUnitsX', 'inches'), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'YResolution', res), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'DisplayedUnitsY', 'inches'), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/tiff/1.0/', 'XResolution', res), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/tiff/1.0/', 'YResolution', res), xmp.rawData = xmp.serialize();
      xmp.setProperty('http://ns.adobe.com/tiff/1.0/', 'ResolutionUnit', 2), xmp.rawData = xmp.serialize();

      if (xmpFile.canPutXMP(xmp)) {
        xmpFile.putXMP(xmp);
        xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
        return true;  // Return true for successful processing
      } else {
        xmpFile.closeFile();
        return null;  // Return null if the XMP cannot be updated
      }
    } catch (e) {
      $.writeln('Error processing file: ' + file.name + ' - ' + e.message);
      return null;  // Return null if there is an error
    }
  }

})();

 

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

TOPICS
Actions and scripting

Views

1.4K

Translate

Translate

Report

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
Adobe
Community Expert ,
Nov 19, 2022 Nov 19, 2022

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

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 ,
Jan 28, 2024 Jan 28, 2024

Copy link to clipboard

Copied

LATEST

Although I feel that Bridge is generally the best place to perform this task, here is an updated version offering the selection of multiple files from Photoshop:

 

/*
Set Multiple Selected JPEG Resolution Metadata to 300ppi.jsx
v1.0, 29th January 2024, Stephen Marsh
v1.1, 29th September 2024 - Corrected the cancel button logic
Add the stripped resolution metadata back to exported JPEG files
https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-to-add-missing-resolution-metadata-to-exported-jpeg-files/td-p/13335061

Bridge version:
https://community.adobe.com/t5/bridge-discussions/bridge-script-to-add-missing-resolution-metadata-to-exported-jpeg-files/td-p/13357144
*/

#target photoshop

(function () {

    // Prompt user to select files (allows multiple selection)
    var selectFiles = File.openDialog("Select the file/s:", undefined, true);
    
    if (selectFiles === null || selectFiles.length === 0) { // If no files are selected, cancel the script
        alert('Script cancelled!');
        return;
    }

    var timeDiff = {
        startTime: null,
        setStartTime: function() {
            var d = new Date();
            this.startTime = d.getTime();
        },
        getDiff: function() {
            var d = new Date();
            return (d.getTime() - this.startTime) / 1000; // return time difference in seconds
        }
    };

    timeDiff.setStartTime();

    var counter = 0; // Counter for processed files
    var totalFiles = selectFiles.length;

    for (var i = 0; i < totalFiles; i++) {
        if (/\.(jpg|jpeg)$/i.test(selectFiles[i].name)) {
            var result = setResMeta(selectFiles[i], 300); // Resolution value in PPI
            if (result !== null) {
                counter++; // Increment counter only if successful
            } else {
                $.writeln('Failed to process: ' + selectFiles[i].name);
            }
        } else {
            alert("The file '" + selectFiles[i].name + "' isn't a JPEG!");
        }
    }

    alert("Resolution metadata set to 300 ppi" +
          "\rFiles selected: " + totalFiles +
          "\rFiles processed: " + counter +
          "\rTime elapsed: " + timeDiff.getDiff() + " seconds");

    // Function to set resolution metadata
    function setResMeta(file, res) {
        try {
            if (!ExternalObject.AdobeXMPScript) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
            var xmpFile = new XMPFile(File(file).fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
            var xmp = xmpFile.getXMP();

            xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'XResolution', res), xmp.rawData = xmp.serialize();
            xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'DisplayedUnitsX', 'inches'), xmp.rawData = xmp.serialize();
            xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'YResolution', res), xmp.rawData = xmp.serialize();
            xmp.setProperty('http://ns.adobe.com/photoshop/1.0/', 'DisplayedUnitsY', 'inches'), xmp.rawData = xmp.serialize();
            xmp.setProperty('http://ns.adobe.com/tiff/1.0/', 'XResolution', res), xmp.rawData = xmp.serialize();
            xmp.setProperty('http://ns.adobe.com/tiff/1.0/', 'YResolution', res), xmp.rawData = xmp.serialize();
            xmp.setProperty('http://ns.adobe.com/tiff/1.0/', 'ResolutionUnit', 2), xmp.rawData = xmp.serialize();

            if (xmpFile.canPutXMP(xmp)) {
                xmpFile.putXMP(xmp);
                xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
                return true;  // Return success
            } else {
                xmpFile.closeFile();
                return null;  // Return null if failed to save XMP
            }
        } catch (e) {
            $.writeln('Error processing file: ' + file.name + ' - ' + e.message);
            return null;  // Return null on error
        }
    }

})();

 

 

Votes

Translate

Translate

Report

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