CHAPTER 10: Scripting Access to XMP Metadata Accessing the XMP scripting API 259
// retrieve property
prop = xmp.getProperty(XMPConst.NS_XMP, "CreatorTool");
$.writeln("namespace: " + prop.namespace + \n + "property path + name: " +
prop.path + \n + "value: " + prop); // same as prop.value
Modifying existing metadata
This code accesses an existing XMP packet, assuming the location has been assigned to a string variable. It
sets the modification-date property to the current date and time, and stores the updated XMP packet back
to the string, making it as small as possible.
xmp = new XMPMeta(xmpStr); // object initialized with xmp packet as string
dateTime = new XMPDateTime(new Date()); // now
$.writeln("old modification date: " +
xmp.getProperty(XMPConst.NS_XMP, "ModifyDate", "xmpdate"));
xmp.setProperty(XMPConst.NS_XMP, "ModifyDate", dateTime, "xmpdate");
// serialize to XML, in compact style
xmpStr = xmp.serialize(XMPConst.SERIALIZE_USE_COMPACT_FORMAT);
Using XMPFile for batch processing
This example iterates through a folder of image files and processes the metadata. The script processes
each picture as follows:
X Reads and parses the metadata. If an image file does not contain XMP metadata, the legacy metadata
is automatically converted to XMP.
X Deletes the list of existing creators, and adds a new creator value.
X Writes the modified metadata back to the file.
$.writeln("XMPFiles batch processing example");
// define folder containing images (make sure that you use copies)
var picFolder = "/c/temp/photos";
// load the XMPScript library
if (ExternalObject.AdobeXMPScript == undefined)
ExternalObject.AdobeXMPScript =
new ExternalObject(’lib:AdobeXMPScript’);
// iterate through the photos in the folder
var pics = Folder(picFolder).getFiles();
for (f in pics) {
var file = pics[f];
$.writeln("process file: " + file.fsName);
// applies only to files, not to folders
if (file instanceof File) {
var xmpFile = new XMPFile(file.fsName, XMPConst.UNKNOWN,
XMPConst.OPEN_FOR_UPDATE);
var xmp = xmpFile.getXMP();
// delete existing authors and add a new one
// existing metadata stays untouched
xmp.deleteProperty(XMPConst.NS_DC, "creator");
xmp.appendArrayItem(XMPConst.NS_DC, "creator", "Judy", 0,
XMPConst.ARRAY_IS_ORDERED);
// write updated metadata into the file
if (xmpFile.canPutXMP(xmp)) {
xmpFile.putXMP(xmp);
}
xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
}
}
Comentarios a estos manuales