Modify XMP data of a document

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

poortip

Modify XMP data of a document

Post by poortip »

Hi!

I want to store some information in the metadata of the document. This storage event takes place at a button click event on my UI. The user can click the button N number of times, and the storage will take place N times too (depending on whether the information has not already been stored). I found many examples on the internet pertaining to this, for eg., XMP-Toolkit-SDK, and XMPCore libraries for Action Script. But, I found that both these methods required that the document be saved to disk at the time of managing the XMP data. This has become a hurdle to my plugin, as it is not mandatory that when user clicks the button on the UI, the document is saved.

Please guide, as to what should be the correct way to go about this.

Thanks!
Paul MR

Modify XMP data of a document

Post by Paul MR »

Here are a couple of ways to do it...

Code: Select all//Photoshop CS4 or better required
if (ExternalObject.AdobeXMPScript == undefined)  ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var xmp;
xmp = new XMPMeta( app.activeDocument.xmpMetadata.rawData );
//do your changes here eg:
var Name = decodeURI(activeDocument.name).replace(/\.[^\.]+$/, '');
xmp.deleteProperty(XMPConst.NS_DC, "description");
xmp.setLocalizedText( XMPConst.NS_DC, "description", null, "x-default", Name );
app.activeDocument.xmpMetadata.rawData = xmp.serialize();

Code: Select allapp.activeDocument.info.author="Author";
app.activeDocument.info.caption="Caption"; //Description field
/*
//etc
app.activeDocument.info.captionWriter
app.activeDocument.info.headline
app.activeDocument.info.instructions   
app.activeDocument.info.keywords
app.activeDocument.info.author
app.activeDocument.info.authorPosition
app.activeDocument.info.credit
app.activeDocument.info.source
app.activeDocument.info.category
app.activeDocument.info.supplementalCategories
app.activeDocument.info.title
app.activeDocument.info.creationDate
app.activeDocument.info.city
app.activeDocument.info.provinceState
app.activeDocument.info.country
app.activeDocument.info.transmissionReference
app.activeDocument.info.copyrightNo
*/
csuebele

Modify XMP data of a document

Post by csuebele »

I've run into the same issue. I've written a script that takes info from a text file generated by a barcode scanner and imported that into the metadata of an image in PS. This metadata contains custom fields also, which is not normally accessible thought file.info Trouble is the entire file has to be saved if it is in PS for the metadata to take, and if the file was put through ACR, I can't seem to access the metadata to change it, so I have to save the file first then change the metadata packet. I need to learn to set up Bridgetalk to change this metadata before the file is open. I'm sure Paul know all about that too.
Paul MR

Modify XMP data of a document

Post by Paul MR »

There is no need to open any file to update the metadata with Photoshop CS4 or better. As an example....
N.B. This is for non raw files, for raw files you would edit the xmp file.

Code: Select allvar f = File("/c/captures/a.jpg");
setDescription(f,"My new description");

function setDescription( 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, "description");
        xmp.setLocalizedText( XMPConst.NS_DC, "description", null, "x-default", descStr );
      if (xmpf.canPutXMP( xmp )) {
         xmpf.putXMP( xmp );
      }
      xmpf.closeFile( XMPConst.CLOSE_UPDATE_SAFELY );
}
csuebele

Modify XMP data of a document

Post by csuebele »

Paul, if that's the case, it should be easy to update the metadata. I had the file open in PS, but if I don't need to do that or go through Bridge, that would be great. I was having issues with files that had corrections done in ACR, but maybe doing it the way you described will avoid that. That's so much!!!
csuebele

Modify XMP data of a document

Post by csuebele »

Just tried this out, Paul, and it worked great and even worked with an image corrected in ACR. Thanks again!!!