Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to read xmp metadata in PS using javascript

Engaged ,
May 25, 2022 May 25, 2022

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>

 

TOPICS
Actions and scripting , macOS
4.3K
Translate
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

correct answers 1 Correct answer

Community Expert , May 30, 2022 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:

 

esko-meta.png

 

P.S. The p

...
Translate
Adobe
LEGEND ,
May 26, 2022 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

 

Translate
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 ,
May 27, 2022 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)!

Translate
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
Engaged ,
May 27, 2022 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/12964...

Translate
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
LEGEND ,
May 28, 2022 May 28, 2022

My script example shows you how to read individual values.

Translate
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 ,
May 28, 2022 May 28, 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-fi...

 

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

 

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

 

Translate
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 ,
May 30, 2022 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:

 

esko-meta.png

 

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

 

Translate
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
Engaged ,
May 30, 2022 May 30, 2022

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

@Stephen Marsh thanks a lot

Translate
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
Engaged ,
Jun 02, 2022 Jun 02, 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>

 

Translate
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
Engaged ,
Jun 02, 2022 Jun 02, 2022

I tried to find a solution in the scripts that @Lumigraphics shared, but i got empty message.

What is wrong in my code?

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.data.(@Name == 'Job').@value;
alert(job_name)
Translate
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
LEGEND ,
Jun 03, 2022 Jun 03, 2022

Make sure you are casting your data types, so job_name.toString() as an example.

I don't know what that schema defines so there isn't a way to know if your usage is correct.

Translate
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 ,
Jun 04, 2022 Jun 04, 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/11546...

 

Manan Joshi ?

Translate
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
Engaged ,
Jun 29, 2022 Jun 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)

 

Translate
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 ,
Jun 29, 2022 Jun 29, 2022

@milevic - fantastic! Thank you for coming back and sharing with the community!

Translate
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
Engaged ,
Dec 26, 2022 Dec 26, 2022

Dear @Stephen Marsh could you help me with this data? I want to read all colors and their angles, but i am stucked...

 

 

<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>
          <egGr:inks>
            <rdf:Seq>
               <rdf:li rdf:parseType="Resource">
                  <egInk:name>Cyan</egInk:name>
                  <egInk:frequency>320</egInk:frequency>
                  <egInk:angle>15</egInk:angle>
                  <egInk:dotshape>C</egInk:dotshape>
               </rdf:li>
               <rdf:li rdf:parseType="Resource">
                  <egInk:name>Magenta</egInk:name>
                  <egInk:frequency>320</egInk:frequency>
                  <egInk:angle>30</egInk:angle>
                  <egInk:dotshape>C</egInk:dotshape>
               </rdf:li>
               <rdf:li rdf:parseType="Resource">
                  <egInk:name>Yellow</egInk:name>
                  <egInk:frequency>320</egInk:frequency>
                  <egInk:angle>45</egInk:angle>
                  <egInk:dotshape>C</egInk:dotshape>
               </rdf:li>
               <rdf:li rdf:parseType="Resource">
                  <egInk:name>Black</egInk:name>
                  <egInk:frequency>320</egInk:frequency>
                  <egInk:angle>75</egInk:angle>
                  <egInk:dotshape>C</egInk:dotshape>
               </rdf:li>
            </rdf:Seq>
      </rdf:Description>
</x:xmpmeta>

 

 

I tried just to read angle with code bellow, but i got NaN alert. I am not sure how to read an array of those nested values 

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, "angle"));
alert(theValue);
Translate
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 ,
Dec 26, 2022 Dec 26, 2022
LATEST

@milevic – I'd need a sample file to test... However the following code from @jazz-y should in theory provide the framework, you would of course need to exchange the various info such as namespaces etc. as previously shown in this topic thread.

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-get-metadata-without-openin...

 

Translate
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