AEM CC - Sample CC Scripts for handling Metadata

Goal


Sample Extend Scripts for handling metadata from CEP panels. Check the following posts for more information on coding HTML panels for CC usign CEP

http://experience-aem.blogspot.com/2015/01/aem-6-search-and-download-dam-assets-in-adobe-cc-like-illustrator.html


GitHub

Solution


For testing the extend script pieces (before including them in CEP Panels) use Adobe Extendscript Toolkit CC. In the following screenshots toolkit connects to InDesign CC / Illustrator CC, runs the script to set metadata. From the CEP Panels, JS object parameters to ES functions are passed as strings by calling JSON.stringify() and converted to objects using JSON.parse() - get the json2.jsx script from github)






Script - Set Metadata in InDesign CC

#include "json2.jsx"

(function () {
    if (typeof EAEM == "undefined") {
        EAEM = {};
    }

    EAEM.NS_EAEM = "NS_EAEM";
    EAEM.NS_URI = "http://experience-aem.blogspot.com/eaem";
    EAEM.NS_PREFIX = "eaem";

    var ERROR = "ERROR",
        SUCCESS = "SUCCESS";

    function init() {
        ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
        XMPMeta.registerNamespace(EAEM.NS_URI, EAEM.NS_PREFIX);
    }

    EAEM.setMetaData = setMetaData;

    function setMetaData(namespace, metadataJSONstr) {
        var result = ERROR;

        try {
            var doc = app.activeDocument,
                metadataObj = JSON.parse(metadataJSONstr),
                value, nUri = (namespace === EAEM.NS_EAEM) ? EAEM.NS_URI : XMPConst[namespace],
                xmp = doc.metadataPreferences;

            for (var x in metadataObj) {
                if (!metadataObj.hasOwnProperty(x)) {
                    continue;
                }

                value = metadataObj[x];

                xmp.setProperty(nUri, x, '');

                if (!value) {
                    continue;
                }

                if (value instanceof Array) {
                    if (value.length > 0) {
                        xmp.createContainerItem(nUri, x, undefined, ContainerType.BAG);

                        for (var y = 1; y <= value.length; y++) {
                            xmp.setProperty(nUri, x + "[" + y + "]", value[y - 1]);
                        }
                    }
                } else {
                    xmp.setProperty(nUri, x, value);
                }
            }

            result = SUCCESS;
        } catch (err) {
            result = ERROR + "-" + err.message;
        }

        return result;
    }

    function testSetMetadata(){
        var metadata = {
            "abbr" : "AEM",
            "title" : "Adobe Experience Manager"
        };

        var result = setMetaData("NS_EAEM", JSON.stringify(metadata));

        $.writeln("result -> " + result);
    }

    function testSetMetadataArray(){
        var metadata = {
            "products" : [ "Sites", "Assets", "Mobile"]
        };

        var result = setMetaData("NS_EAEM", JSON.stringify(metadata));

        $.writeln("result -> " + result);
    }

    init();

    testSetMetadata();

    testSetMetadataArray();
})();


Script - Get Metadata in InDesign CC

#include "json2.jsx"

(function () {
    if (typeof EAEM == "undefined") {
        EAEM = {};
    }

    EAEM.NS_EAEM = "NS_EAEM";
    EAEM.NS_URI = "http://experience-aem.blogspot.com/eaem";
    EAEM.NS_PREFIX = "eaem";

    var ERROR = "ERROR",
        SUCCESS = "SUCCESS";

    function init() {
        ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
        XMPMeta.registerNamespace(EAEM.NS_URI, EAEM.NS_PREFIX);
    }

    EAEM.getMetaData = getMetaData;

    function getMetaData(props) {
        var propValues = {};

        try {
            var propsArr = JSON.parse(props), nUri, propertyValue,
                xmp = app.activeDocument.metadataPreferences;

            for (var x = 0; x < propsArr.length; x++) {
                propertyValue = xmp.getProperty(EAEM.NS_URI, propsArr[x]);

                if(!propertyValue){
                    propertyValue = getXMPArrayData(xmp, EAEM.NS_URI, propsArr[x]);
                }

                if(propertyValue){
                    propValues[propsArr[x]] = propertyValue;
                }
            }
        }
        catch (err) {
            propValues = ERROR + "-" + err.message;
        }

        return JSON.stringify(propValues);
    }

    function getXMPArrayData(xmp, nUri, propertyName){
        var index = 1, values = [],
            propertyValue = xmp.getProperty(nUri, propertyName + "[" + index++ + "]");

        //its an array
        if(!propertyValue) {
            return values;
        }

        do{
            values.push(propertyValue);

            propertyValue = xmp.getProperty(nUri, propertyName + "[" + index++ + "]");

            if(!propertyValue){
                break;
            }
        }while(true);

        return values;
    }

    function testGetMetadata(){
        var metadata = [ "abbr", "title", "products" ];

        var result = getMetaData(JSON.stringify(metadata));

        $.writeln("result -> " + result);
    }

    init();

    testGetMetadata();
})();


Script - Set Metadata in Illustrator CC

#include "json2.jsx"

(function () {
    if (typeof EAEM == "undefined") {
        EAEM = {};
    }

    EAEM.NS_EAEM = "NS_EAEM";
    EAEM.NS_URI = "http://experience-aem.blogspot.com/eaem";
    EAEM.NS_PREFIX = "eaem";

    var ERROR = "ERROR",
        SUCCESS = "SUCCESS";

    function init() {
        ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
        XMPMeta.registerNamespace(EAEM.NS_URI, EAEM.NS_PREFIX);
    }

    EAEM.setMetaData = setMetaData;

    function setMetaData(namespace, metadataJSONstr) {
        var result = ERROR;

        try {
            var doc = app.activeDocument,
                metadataObj = JSON.parse(metadataJSONstr),
                value, nUri = (namespace === EAEM.NS_EAEM) ? EAEM.NS_URI : XMPConst[namespace],
                xmp = new XMPMeta(doc.XMPString);

            for (var x in metadataObj) {
                if (!metadataObj.hasOwnProperty(x)) {
                    continue;
                }

                value = metadataObj[x];

                xmp.deleteProperty(nUri, x);

                if (!value) {
                    continue;
                }

                if (value instanceof Array) {
                    for (var y = 0; y < value.length; y++) {
                        xmp.appendArrayItem(nUri, x, value[y], 0, XMPConst.ARRAY_IS_ORDERED);
                    }
                } else {
                    xmp.setProperty(nUri, x, value);
                }
            }

            doc.XMPString = xmp.serialize(XMPConst.SERIALIZE_USE_COMPACT_FORMAT);

            result = SUCCESS;
        } catch (err) {
            result = ERROR + "-" + err.message;
        }

        return result;
    }

    function testSetMetadata(){
        var metadata = {
            "abbr" : "AEM",
            "title" : "Adobe Experience Manager"
        };

        var result = setMetaData("NS_EAEM", JSON.stringify(metadata));

        $.writeln("result -> " + result);
    }

    function testSetMetadataArray(){
        var metadata = {
            "products" : [ "Sites", "Assets", "Mobile"]
        };

        var result = setMetaData("NS_EAEM", JSON.stringify(metadata));

        $.writeln("result -> " + result);
    }

    init();

    testSetMetadata();

    testSetMetadataArray();
})();


Script - Get Metadata in Illustrator CC

#include "json2.jsx"

(function () {
    if (typeof EAEM == "undefined") {
        EAEM = {};
    }

    EAEM.NS_EAEM = "NS_EAEM";
    EAEM.NS_URI = "http://experience-aem.blogspot.com/eaem";
    EAEM.NS_PREFIX = "eaem";

    var ERROR = "ERROR",
        SUCCESS = "SUCCESS";

    function init() {
        ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
        XMPMeta.registerNamespace(EAEM.NS_URI, EAEM.NS_PREFIX);
    }

    EAEM.getMetaData = getMetaData;

    function getMetaData(props) {
        var propValues = {};

        try {
            var propsArr = JSON.parse(props), nUri, property,
                xmp = new XMPMeta(app.activeDocument.XMPString);

            for (var x = 0; x < propsArr.length; x++) {
                if (!xmp.doesPropertyExist(EAEM.NS_URI, propsArr[x])) {
                    continue;
                }

                property = xmp.getProperty(EAEM.NS_URI, propsArr[x]);

                if (property.options & XMPConst.PROP_IS_ARRAY) {
                    var count = xmp.countArrayItems(EAEM.NS_URI, propsArr[x]),
                        resArr = [];

                    for (var k = 1; k <= count; k++) {
                        resArr.push(xmp.getArrayItem(EAEM.NS_URI, propsArr[x], k).toString());
                    }

                    propValues[propsArr[x]] = resArr;
                }else{
                    propValues[propsArr[x]] = property.value;
                }
            }
        }
        catch (err) {
            propValues = ERROR + "-" + err.message;
        }

        return JSON.stringify(propValues);
    }

    function testGetMetadata(){
        var metadata = [ "abbr", "title", "products" ];

        var result = getMetaData(JSON.stringify(metadata));

        $.writeln("result -> " + result);
    }

    init();

    testGetMetadata();
})();

Script - Get Metadata in PhotoshopCC

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

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

$.writeln(xmp.getProperty(XMPConst.NS_DC,'format'));

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete