Skip to main content
ScriptingRules
Known Participant
December 6, 2013
Question

Keyword Replacement in Illustrator

  • December 6, 2013
  • 0 replies
  • 510 views

The following script looks at the active document and then replaces the keywords in the NC_DC schema with a new set.

I am having troubles with deleting the properties and assigning new ones.

Can someone help me with the replacement of the items in the NC_DC:'subject' fields. (See the area between the ***********)

The active AI file has layers called ET0030 and ET0031.  The keywords are test1, test2, ET0001, ET0002 which are stored in File/File Info/Keywords.

Thanks for the help in advance.

#target illustrator

//  This script looks in the layer structure of the active document for a specific layers (ET or tools layers) and replaces the existing tools (if they exist) in the Keywords area of the XMP data.
test();

function test()
{
    var sourceDoc = app.activeDocument;
    var toolList = findETLayers(sourceDoc);  //This variable is assigned the sourceDoc layers that start with ET
    var keepValue = []
    var newData = []
    var mySubjectString = []

    if (ExternalObject.AdobeXMPScript == undefined) {  // Load the XMP Script library
        ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
        app.synchronousMode = true;
    }

    var myXmp = new XMPMeta(sourceDoc.XMPString);  //Create an XMPMeta object from the active documents XMPString.

    var mySubjectCount = myXmp.countArrayItems(XMPConst.NS_DC, 'subject');  //Counts the number of objects in the array valued XMP property.

    for (i = 1; i <= mySubjectCount; i++){  //Cycles through the elements and determines if there are any ET tool keywords.
        var mySubject = myXmp.getArrayItem(XMPConst.NS_DC, 'subject', i);
        mySubjectString = String (mySubject); //Converts the mySubject variable to a string to process in the if statement with the substr (I don't know a better way yet)
       
        if(mySubjectString.substr(0,2) != "ET") {  //This if statement looks to see if there are Keywords other than the ET tools and if so saves them out to the keepValue array.
            keepValue = keepValue + mySubjectString + ";";
        }
    }
newData = keepValue + toolList;  //Create the new keywords array with the old and new values

//Test Values
alert("Keep Values = " + keepValue);  //Shows the keywords that we want to keep
alert("New Values = " + newData);  //Shows the new keywords needing to be assigned to the file

//**********************************************************************************************************************

//  DELETE NC_DC 'subject' AND REPLACE WITH THE newData array.  This could go up in the FOR loop.

//**********************************************************************************************************************

//activeDocument.save();

}

function findETLayers(sourceDoc)  //  Need to find if there are tool layers and assign their names to an array "tools"
{
                var myDoc0=sourceDoc
                var myLayerCount0 = myDoc0.layers.length
                var tools = new Array

                         for(var myCounter0 = 0; myCounter0 < myLayerCount0; myCounter0++) {
                            var selectLayer0 = myDoc0.layers[myCounter0]
                           
                            selectLayer0.visible=true
                            selectLayer0.locked=false

                            if(selectLayer0.name.substr(0,2) == "ET") {
                            selectLayer0.visible=false
                            selectLayer0.locked=false
                           
                            tools = tools + selectLayer0.name.substr(0,6) + ";"
                            }
                        }
      return tools
}

This topic has been closed for replies.