Skip to main content
Inspiring
May 26, 2022
Answered

How to read xmp metadata in PS using javascript

  • May 26, 2022
  • 5 replies
  • 4072 views

How its possible, using javascript, to read some information from pdf with xmp metada (I think it's an xmp metadata, but maybe I'm wrong since I don't have much experience with these files).

I have some pdf template files and they have some information imortant for me. Could you help me how to read information for vsize hsize  from this document, using javascript? Thanks in advanced

 

<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 5.1.2">
   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      <rdf:Description rdf:about=""
            xmlns:egGr="http://ns.esko-graphics.com/"
            xmlns:egInk="http://ns.esko-graphics.com/">
         <egGr:nrpages>1</egGr:nrpages>
         <egGr:startlogpage>1</egGr:startlogpage>
         <egGr:units>mm</egGr:units>
         <egGr:vsize>120</egGr:vsize>
         <egGr:hsize>120</egGr:hsize>
         <egGr:margtop>20</egGr:margtop>
         <egGr:margbot>10</egGr:margbot>
         <egGr:margleft>20</egGr:margleft>
         <egGr:margright>20</egGr:margright>
         <egGr:readerspread>False</egGr:readerspread>
         <egGr:screenreg>False</egGr:screenreg>
      </rdf:Description>
      </x:xmpmeta>

 

This topic has been closed for replies.
Correct answer Stephen Marsh

@milevic 

 

OK, I finally have it working, based on the script from jazz-y and another one from the Illustrator forum... For an open file:

 

#target photoshop
var ns = "http://ns.esko-graphics.com/grinfo/1.0/";
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
xmpMeta = new XMPMeta(app.activeDocument.xmpMetadata.rawData);
var theValue = xmpMeta.getProperty(ns, "vsize");
alert(theValue);

 

Here is a visual of the XMP metadata and how it relates to the code:

 

 

P.S. The previous example parses the XML value as a string, you may need to convert it to a number if you need to perform mathematical operations on the value, again, for an open file:

 

#target photoshop
var ns = "http://ns.esko-graphics.com/grinfo/1.0/";
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
xmpMeta = new XMPMeta(app.activeDocument.xmpMetadata.rawData);
var theValue = Math.abs(xmpMeta.getProperty(ns, "vsize"));
alert(theValue);

 

Edit: Via a hard-coded path:

 

#target photoshop
var f = new File('~/Desktop/esko-art-sample.pdf');
var ns = "http://ns.esko-graphics.com/grinfo/1.0/";
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
xmpMeta = new XMPFile(f.fsName, XMPConst.FILE_PDF, XMPConst.OPEN_FOR_READ).getXMP();
var theValue = Math.abs(xmpMeta.getProperty(ns, "vsize"));
alert(theValue);

 

Via an open dialog:

 

#target photoshop
var f = File.openDialog();
var ns = "http://ns.esko-graphics.com/grinfo/1.0/";
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
xmpMeta = new XMPFile(f.fsName, XMPConst.FILE_PDF, XMPConst.OPEN_FOR_READ).getXMP();
var theValue = Math.abs(xmpMeta.getProperty(ns, "vsize"));
alert(theValue);

 

5 replies

milevicAuthor
Inspiring
June 3, 2022

@Stephen Marsh could you help me with this one. I have an jdf file with xml listed bellow, but i stuck, how to read "value" for "job"?

<?xml version="1.0" encoding="UTF-8"?>
<JDF xmlns="http://www.CIP4.org/JDFSchema_1_1" xmlns:eg="http://www.esko-graphics.com/EGSchema1_0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Product">
<eg:SmartName Name="Job" Value="Poland_001"/>
<eg:SmartName Name="Barcode" Value="Yes"/>
<eg:SmartName Name="Separation" Value="CMYK"/>
<eg:SmartName Name="Paper_grade" Value="300g"/>
<eg:SmartName Name="Template" Value="Template_box"/>
</JDF>

 

Stephen Marsh
Braniac
June 5, 2022

Sorry, no luck with the JDF, perhaps a more knowledgeable scripter can post working code...

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/use-values-from-xml-into-ps/m-p/11546356

 

Manan Joshi ?

milevicAuthor
Inspiring
June 29, 2022

I finally resolved this.  Maybe it will be useful for someone.  

var XMLfile = new File("/Users/rsmilevicm/Documents/test.jdf");
XMLfile.open('r');
var str = "";
str = XMLfile.read();
XMLfile.close();
var xml = new XML(str);
default xml namespace = "http://www.esko-graphics.com/EGSchema1_0";
var job_name = xml.xpath("//*[@Name = 'Job']/@Value");
alert(job_name)

 

Stephen Marsh
Braniac
May 29, 2022

Here is an example of reading XMP from a file or from an opened document from jazz-y:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/determine-compression-used-on-tiff-file/m-p/11141851

 

I think you would need to get it from the file, not the opened document. Without access to a sample PDF file containing this Esko metadata, I can't test or offer direct assistance.

 

Edit: Another couple of examples –

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-get-metadata-without-opening-the-file/td-p/11992046

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-print-dpi-resolution/m-p/10502703

 

Stephen Marsh
Stephen MarshCorrect answer
Braniac
May 30, 2022

@milevic 

 

OK, I finally have it working, based on the script from jazz-y and another one from the Illustrator forum... For an open file:

 

#target photoshop
var ns = "http://ns.esko-graphics.com/grinfo/1.0/";
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
xmpMeta = new XMPMeta(app.activeDocument.xmpMetadata.rawData);
var theValue = xmpMeta.getProperty(ns, "vsize");
alert(theValue);

 

Here is a visual of the XMP metadata and how it relates to the code:

 

 

P.S. The previous example parses the XML value as a string, you may need to convert it to a number if you need to perform mathematical operations on the value, again, for an open file:

 

#target photoshop
var ns = "http://ns.esko-graphics.com/grinfo/1.0/";
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
xmpMeta = new XMPMeta(app.activeDocument.xmpMetadata.rawData);
var theValue = Math.abs(xmpMeta.getProperty(ns, "vsize"));
alert(theValue);

 

Edit: Via a hard-coded path:

 

#target photoshop
var f = new File('~/Desktop/esko-art-sample.pdf');
var ns = "http://ns.esko-graphics.com/grinfo/1.0/";
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
xmpMeta = new XMPFile(f.fsName, XMPConst.FILE_PDF, XMPConst.OPEN_FOR_READ).getXMP();
var theValue = Math.abs(xmpMeta.getProperty(ns, "vsize"));
alert(theValue);

 

Via an open dialog:

 

#target photoshop
var f = File.openDialog();
var ns = "http://ns.esko-graphics.com/grinfo/1.0/";
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
xmpMeta = new XMPFile(f.fsName, XMPConst.FILE_PDF, XMPConst.OPEN_FOR_READ).getXMP();
var theValue = Math.abs(xmpMeta.getProperty(ns, "vsize"));
alert(theValue);

 

milevicAuthor
Inspiring
May 31, 2022

Thank you all, for your commitment, this is really good job.

@Stephen Marsh thanks a lot

milevicAuthor
Inspiring
May 27, 2022

Hmmm... i just need to read this specific attribute and use that value later in PS script, i dont need to import pdf into PS. I had similar issue with XML and it s resolved, but now i am stucked. I dont know how to read those egGR tags.

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/use-values-from-xml-into-ps/m-p/12964501#M646001

Braniac
May 28, 2022

My script example shows you how to read individual values.

Stephen Marsh
Braniac
May 27, 2022

I would think that one would need to use BridgeTalk or some other method to *directly* get the XMP metadata out of the PDF and do something with it in Photoshop, as once you rasterize the PDF that metadata would be lost (or so I'm assuming).

 

Edit: I have just pulled a customer file out of the archive that was processed by Esko Automation Engine, and lo and behold, when rasterizing the PDF into Photoshop – the Esko metadata is retained (much to my surprise)!

Braniac
May 26, 2022

XMP files are plain text. You just need to parse it to get your data. Extendscript/ScriptUI has built-in XML/XMP tools or you can do it yourself.

https://developer.adobe.com/creative-cloud/#api-list

I have a demo on how to work with XML data on my dropbox, and many of my scripts deal with XMP data.

https://www.dropbox.com/sh/mg817g9a9ymbasi/AADTmXUVxmFfM58bcyYE7yiwa?dl=0