5 Replies Latest reply on May 12, 2016 8:24 PM by Ten A

    Script to copy variables into metadata

    llkinn

      I have various text anchors with given as variables. I am able to alert the object information of variables in an illustrator document using the script below. Is there a way of copying the contents of those into the metadata? I don't mind which field of metadata. Thanks.

       

      var idoc = app.activeDocument; 
      var vars = idoc.variables; 
      for (j=0; j<vars.length; j++) 
          alert(vars[j].name + ' : ' + vars[j].pageItems[0].contents); 
        • 2. Re: Script to copy variables into metadata
          llkinn Level 1

          To clarify, I would like to write to the metadata form the variable object data in the file, not write the metadata to an exported file.

          • 3. Re: Script to copy variables into metadata
            Ten A Adobe Community Professional

            You can use it to write opened document metadata.

            To set custom metadata and get opened document path(ex: XMPtool.f = app.activeDocument.fullName).

            Call the write function with previouse path.

            • 4. Re: Script to copy variables into metadata
              llkinn Level 1

              Thanks for your help, but sorry, I don't follow at all. Why are we talking about previous paths? Could you specify the script which would enable me to write to the keywords metadata? I've tried with the GitHub script and it isn't doing anything.

              • 5. Re: Script to copy variables into metadata
                Ten A Adobe Community Professional

                Try it:

                XMPtool = {
                    ns : "http://ns.example.com/metawrite/1.0/", //You shuld rewrite your own Name Space.
                    prefix : "Vriable:", //You can set your prefix
                    f : new Object(),
                    read : function(prop){
                        if(xmpLib==undefined) var xmpLib = new ExternalObject('lib:AdobeXMPScript');
                        var xmpFile = new XMPFile(this.f.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ);
                        var xmpPackets = xmpFile.getXMP();
                        var xmp = new XMPMeta(xmpPackets.serialize());
                        alert(xmp.getProperty(this.ns, prop).toString());
                        },
                    write : function(prop, val){ //f:fileObject, arg1:String, arg2:String
                        if(xmpLib==undefined) var xmpLib = new ExternalObject('lib:AdobeXMPScript');
                        var xmpFile = new XMPFile(this.f.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
                        var xmp = xmpFile.getXMP();
                        var mt = new XMPMeta(xmp.serialize());
                        XMPMeta.registerNamespace(this.ns, this.prefix);
                        mt.setProperty(this.ns, prop, val);
                        if (xmpFile.canPutXMP(xmp)) xmpFile.putXMP(mt);
                        xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
                        }
                    }
                
                
                //If you want write into activeDocument, Set activeDocuments fullName property like below.
                XMPtool.f = app.activeDocument.fullName;
                XMPtool.write("key1", "Your value");
                XMPtool.read("key1");
                
                1 person found this helpful