Help with script to read specific XMP metadata in an image

Community Beginner ,
Apr 13, 2018 Apr 13, 2018

Copy link to clipboard

Copied

I'm creating a script for Photoshop to read a specific XMP metadata field in the file info of an image. I'm trying to specifically read the data that is located under the <dc:subject> area. I want to access the data under dc: subject > rdf: Bag > rdf: li. Here's an example (as it would appear in Raw Data field under an image's file info):

<dc:subject>

    <rdf:Bag>

        <rdf:li>****DATA I NEED HERE****</rdf:li>

    </rdf:Bag>

</dc:subject>

So far, I have a script that can read XMP metadata, but I can't access that specific line of data I showed in the above example. I commented line 43 -- that needs to be updated so it can read into the dc: subject data. I can currently access certain pieces of data in an image file. For example, using this code, if i run xmp.getProperty(XMPConst.NS_DC, "subject") through the console, I get a result back, but it's not the data I need. I figured if I used the same logic as I applied to line 43, it would give me the results I am looking for. Any ideas on how to access the data I'm looking for?

function loadXMPLibrary() {

    if (!ExternalObject.AdobeXMPScript) {

        try {

            ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');

        } catch (e) {

            alert("Can't load XMP Script Library")

            return false;

        }

    }

    return true;

}

function unloadXMPLibrary() {

    if (ExternalObject.AdobeXMPScript) {

        try {

            ExternalObject.AdobeXMPScript.unloadXMPLibrary();

            ExternalObject.AdobeXMPScript = undefined;

        } catch (e) {

            alert("Couldn't unload XMP Script Library")

        }

    }

}

function writeXMP(xmpData, xmpFile) {

    xmpFile.open('w', 'Text');

    xmpFile.encoding = 'UTF-8'

    xmpFile.write(xmpData)

    xmpFile.close()

}

function showXMPMetadata() {

    var xmpLoad = loadXMPLibrary();

    if (xmpLoad) {

        var filepath = app.activeDocument.path + '/' + app.activeDocument.name;

        var myFile = File(filepath);

        xmpFile = new XMPFile(myFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ);

        xmp = xmpFile.getXMP();

        var myXmp = xmp.serialize();

        xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY)

        var skus = myXmp.getProperty(XMPConst.NS_RDF,"Bag"); //this needs to be updated

        unloadXMPLibrary ();

        return skus;

    };

};

TOPICS
Actions and scripting

Views

2.9K

Likes

Translate

Translate

Report

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

Guide , Apr 13, 2018 Apr 13, 2018

If you want the details from an opened document then this should work...

#target photoshop;

if(documents.length){

if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');

var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);

var keys = getArrayItems(XMPConst.NS_DC,'subject');

/////////////////// Do with as you wish

alert(keys.join("\n"));

//////////////

};

function getArrayItems(ns, prop){

var arrItem=[];

var items = xmp.countArrayItems(

...

Likes

Translate

Translate
Guide ,
Apr 13, 2018 Apr 13, 2018

Copy link to clipboard

Copied

If you want the details from an opened document then this should work...

#target photoshop;

if(documents.length){

if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');

var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);

var keys = getArrayItems(XMPConst.NS_DC,'subject');

/////////////////// Do with as you wish

alert(keys.join("\n"));

//////////////

};

function getArrayItems(ns, prop){

var arrItem=[];

var items = xmp.countArrayItems(ns, prop);

            for(var i = 1;i <= items;i++){

                    arrItem.push(xmp.getArrayItem(ns, prop, i));

                }

return arrItem;

};

Likes

Translate

Translate

Report

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 Beginner ,
Apr 13, 2018 Apr 13, 2018

Copy link to clipboard

Copied

LATEST

wow, this is great! Thank you so much. This seems to be working on the files I'm testing on. I'll let you know if I happen to run into anything and need some help. I appreciate it.

Likes

Translate

Translate

Report

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