• 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
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;

var selectFile = app.openDialog();
var selectedFile = File(selectFile);

if (/\.(jpg|jpeg)$/i.test(selectedFile) === true) {
  setResMeta(selectedFile, 300); // Res value in PPI
} else {
  alert("The file isn't a JPEG!")
}


function setResMeta(file, res) {
    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);
}

 

 

 

/*
Set all JPEG resolution metadata in selected top-level folder to 300 PPI
v1.0, 10th November 2022, Stephen Marsh
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

var inputFolder = Folder.selectDialog("Please select folder to process");
if (inputFolder !== null) {
  var fileList = inputFolder.getFiles(/\.(jpg|jpeg)$/i);

  var timeDiff = {
    setStartTime: function () {
      d = new Date();
      time = d.getTime();
    },
    getDiff: function () {
      d = new Date();
      t = d.getTime() - time;
      time = d.getTime();
      return t;
    }
  };

  timeDiff.setStartTime();

  var counter = 0;

  for (var a in fileList) {
    setResMeta(fileList[a], 300); // Res value in PPI
    counter++;
  }
}

alert("Resolution metadata set to 300 PPI!" + "\r" + "Files selected: " + fileList.length + "\r" + "Files processed: " + counter + "\r" + "(" + timeDiff.getDiff() / 1000 + " seconds)");


function setResMeta(file, res) {
  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);
}

 

 

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

 

TOPICS
Actions and scripting

Views

928

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

var selectFiles = File.openDialog("Select the file/s:", Multiselect = true);

var timeDiff = {
    setStartTime: function() {
        d = new Date();
        time = d.getTime();
    },
    getDiff: function() {
        d = new Date();
        t = d.getTime() - time;
        time = d.getTime();
        return t;
    }
};

timeDiff.setStartTime();

var counter = 0;

for (var i = 0; i < selectFiles.length; i++) {
    if (/\.(jpg|jpeg)$/i.test(selectFiles[i]) === true) {
        setResMeta(selectFiles[i], 300); // Res value in PPI
        counter++;
    } else {
        alert("The file isn't a JPEG!")
    }
}
alert("Resolution metadata set to 300 PPI!" + "\r" + "Files selected: " + selectFiles.length + "\r" + "Files processed: " + counter + "\r" + "(" + timeDiff.getDiff() / 1000 + " seconds)");


function setResMeta(file, res) {
    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);
}

 

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