Here's how I would tackle this particular problem. The getXMPValue function is extracted from xtools/xlib/stdlib.js. // // getXMPValue(obj, tag) // // Get the XMP value for (tag) from the object (obj). // obj can be a String, XML, File, or Document object. // // Some non-simple metadata fields, such as those with // Seq structures are not handled, except for ISOSpeedRatings // which is handled as a special case. Others can be added as needed. // // Based on getXMPTagFromXML from Adobe's StackSupport.jsx // // Examples: // getXMPValue(xmlStr, "ModifyDate") // getXMPValue(app.activeDocument, "ModifyDate") // getXMPValue(xmlObj, "ModifyDate") // getXMPValue(File("~/Desktop/test.jpg"), "ModifyDate") // function getXMPValue(obj, tag) { var xmp = ""; if (obj == undefined) { Error.runtimeError(2, "obj"); } if (tag == undefined) { Error.runtimeError(2, "tag"); } if (obj.constructor == String) { xmp = new XML(obj); } else if (obj.typename == "Document") { xmp = new XML(obj.xmpMetadata.rawData); } else if (obj instanceof XML) { xmp = obj; } else if (obj instanceof File) { if (!ExternalObject.AdobeXMPScript) { ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript'); } // Stdlib.loadXMPScript(); if (tag == "CreateDate") { var cstr = obj.created.toISODateString(); var mstr = Stdlib.getXMPValue(obj, "ModifyDate"); return cstr += mstr.slice(mstr.length-6); } // add other exceptions here as needed var fstr = decodeURI(obj.fsName); var xmpFile = undefined; try { xmpFile = new XMPFile(fstr, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ); } catch (e) { try { xmpFile = new XMPFile(fstr, XMPConst.UNKNOWN, XMPConst.OPEN_USE_PACKET_SCANNING); } catch (e) { Error.runtimeError(19, "obj"); } } var xmpMeta = xmpFile.getXMP(); var str = xmpMeta.serialize() xmp = new XML(str); xmpFile.closeFile(); } else { Error.runtimeError(19, "obj"); } var s; // Handle special cases here if (tag == "ISOSpeedRatings") { s = String(eval("xmp.*::RDF.*::Description.*::ISOSpeedRatings.*::Seq.*::li")); } else { // Handle typical non-complex fields s = String(eval("xmp.*::RDF.*::Description.*::" + tag)); } return s; }; var file = File("~/Desktop/test.png"); var x = getXMPValue(file, "PixelXDimension"); var y = getXMPValue(file, "PixelYDimension");
... View more