Skip to main content
Silly-V
Legend
July 21, 2017
Question

Bridge metadata abstraction attempt

  • July 21, 2017
  • 0 replies
  • 334 views

I've gotten started with some Bridge scripting and got kind of frustrated with the un-intuitive ways to fill out these metadata fields, so I went on a mission to create a helpful abstraction object to help me set the fields.

I noticed that actually most of the solutions were using the external XMP lib object and dealt with manipulating the XMP from the string inside the image files, and the remainder were about the Bridge scripting commands to get the same data from the 'metadata' object which appears to be some kind of magical thing where by a namespace is set and then a key value can be retrieved so long as someone already knows the key ( which are all listed in the raw data, and by adding own text and finding it you can get all the fields, etc.).
So, as the external lib method seems rather verbose, I decided to go with the metadata object of Bridge. Here's the object I made and it appears to be working.

At least this works to set my few fields for now, is the method going to be a whole lot slower than the XMP lib, and are there any problems?

function BridgeMetaSetter(){

  function getNormalProperty(metaObj, name, ns){

    metaObj.namespace = ns;

    return metaObj[name];

  };

  function setNormalProperty(metaObj, name, ns, prop){

    metaObj.namespace = ns;

    metaObj[name] = "";

    metaObj[name] = prop;

  };

  function setArrayProperty(metaObj, name, ns, arr){

    metaObj.namespace = ns;

    metaObj[name] = "";

    metaObj[name] = arr.join(";");

  };

  function getNestedProperty(foundObj, nestedName){

    return foundObj[nestedName];

  };

  function setNestedProperty(foundObj, nestedName, prop){

    foundObj[nestedName] = prop;

  };

  var metaPropDictionary = {

    "Author Title" : {

      name : "AuthorsPosition",

      ns : "http://ns.adobe.com/photoshop/1.0/",

      get : function(metaObj){

        return getNormalProperty(metaObj, this.name, this.ns);

      },

      set : function(metaObj, prop){

        setNormalProperty(metaObj, this.name, this.ns, prop);

      }

    },

    "Description" : {

      name : "description",

      ns : "http://purl.org/dc/elements/1.1/",

      get : function(metaObj){

        return getNormalProperty(metaObj, this.name, this.ns);

      },

      set : function(metaObj, prop){

        setNormalProperty(metaObj, this.name, this.ns, prop);

      }

    },

    "Keywords" : {

      // name : "subject",

      // ns : "http://purl.org/dc/elements/1.1/", // adding an array to this was just creating a single string, Photoshop

      name : "Keywords",

      ns : "http://ns.adobe.com/photoshop/1.0/",

      get : function(metaObj){

        return getNormalProperty(metaObj, this.name, this.ns);

      },

      set : function(metaObj, prop){

        setArrayProperty(metaObj, this.name, this.ns, prop);

      }

    },

    "Description Writer" : {

      name : "CaptionWriter",

      ns : "http://ns.adobe.com/photoshop/1.0/",

      get : function(metaObj){

        return getNormalProperty(metaObj, this.name, this.ns);

      },

      set : function(metaObj, prop){

        setNormalProperty(metaObj, this.name, this.ns, prop);

      }

    },

    "_ContactInfo" : {

      name : "CreatorContactInfo",

      ns : "http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/",

      get : function(metaObj){

        return getNormalProperty(metaObj, this.name, this.ns);

      },

      set : function(metaObj, prop){

        setNormalProperty(metaObj, this.name, this.ns, ""); // sets up the ContactInfo block empty

      }

    },

    "Websites" : {

      name : "Iptc4xmpCore:CreatorContactInfo/Iptc4xmpCore:CiUrlWork",

      ns : "http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/",

      get : function(metaObj){

        var contactInfo = metaPropDictionary["_ContactInfo"].get(metaObj);

        return getNestedProperty(contactInfo, this.name);

      },

      set : function(metaObj, prop){

        //TODO: need to put logic to only set this new, if other ContactInfo items do not exist.

        metaPropDictionary["_ContactInfo"].set(metaObj, prop);

        setNormalProperty(metaObj, this.name, this.ns, prop);

      }

    }

  };

 

  function setMetadataToThumb(thumb, obj){

    var selectedItem = thumb;

    var meta = selectedItem.synchronousMetadata;

    app.synchronousMode = true;

    for(var all in obj){

      metaPropDictionary[all].set(meta, obj[all]);

    }

    thumb.core.metadata.cacheData.status = "bad";

  };

  function getMetadataFromThumb(thumb){

    var selectedItem = thumb;

    var obj = {};

    var meta = selectedItem.synchronousMetadata;

    app.synchronousMode = true;

    for(var all in metaPropDictionary){

      if(all.indexOf("_") == 0){

        continue;

      }

      obj[all] = metaPropDictionary[all].get(meta);

    }

    return obj;

  };

  return {

    setMetadataToThumb : setMetadataToThumb,

    getMetadataFromThumb : getMetadataFromThumb

  };

};

/* USAGE:

  #target bridge

  var helperObj = new BridgeMetaSetter();

  // get example

  var s = app.document.selections[0];

  var getPropertiesResult = helperObj.getMetadataFromThumb(s);

  alert(getPropertiesResult.toSource());

  // set example

  var objToSet = {

    "Description" : "A new description here!!!",

    "Keywords" : ["Word_A", "Word_X", "Word_Y"],

    "Description Writer" : "the writer of description",

    "Author Title" : "Bob Billy Joe",

    "Websites" : "web_http site"

  };

  helperObj.setMetadataToThumb(s, objToSet);

*/

This topic has been closed for replies.