12 Replies Latest reply on Jul 14, 2015 10:02 AM by pixxxel schubser

    Selecting objects by colour and moving to another layer

    Sketch_Ak

      Hi there,

       

      I've managed to google together a script based off others that work in illustrator CS 5.5 but not in the latest version?

      Objectives of this script

      Step 1: Add Layer and call it "Register"

      Step 2: Select all objects based on colour called "Register" and move to layer called "Register"

      Step 3: Rename "Layer 1" to "Thru-cut"

      Step 4: Save and then close document.

       

      Could someone here have a look at the below script and let me know where its gone wrong? Below is also a screenshot of the script error in the latest version of illustrator.

       

      Thank you.

       

      Error.jpg

       

      var myDoc = app.activeDocument;

      var myLayer = myDoc.layers.add();

      myLayer.name = "Register";

       

       

      function getObjectsByColor ( colorName )

      {

      var doc, items, i = 0, n = 0, item, color, selectionArray = [];

       

       

      if ( app.documents.length == 0 ){

          alert("No documents open");

          return;

      }

       

       

      doc = app.activeDocument;

      try

      {

          color = doc.swatches.getByName ( colorName );

      }

      catch(e)

      {

          alert( "No such color !");

          return;

      }

       

       

      color = color.color ;

       

       

      items = doc.pageItems;

      n = items.length;

      if ( items.length == 0 )

      {

          alert( "No items found");

          return;

      }

       

       

      for ( i = 0; i < n ; i++ )

      {

          item = items[i];

          if ( item.fillColor.typename == color.typename

          && item.fillColor.cyan == color.cyan

          && item.fillColor.magenta == color.magenta

          && item.fillColor.yellow == color.yellow

          && item.fillColor.black == color.black )

          {

              selectionArray [ selectionArray.length ] = item;

          }

      }

       

       

      for ( i = 0; i < n ; i++ )

      {

              item = items[i];

          if ( item.fillColor.typename == color.typename

          && item.fillColor.cyan == color.cyan

          && item.fillColor.magenta == color.magenta

          && item.fillColor.yellow == color.yellow

          && item.fillColor.black == color.black )

          {

      item.move( myLayer, ElementPlacement.PLACEATBEGINNING );

      }

      }

       

       

      if ( selectionArray.length == 0 )

      {

          alert( "Nothing found" );

          return;

      }

      app.selection = selectionArray;

      }

       

       

      getObjectsByColor ("Register");

       

       

      function renameText() {

          if (app.documents.length == 0) return;

          var docRef = app.activeDocument;

          recurseLayers(docRef.layers);

          }

           

          renameText();

         

      function recurseLayers(objArray) {

          for (var i = 0; i < objArray.length; i++) {        

          objArray[i].name = objArray[i].name.replace(/\s*Layer 1\s*\d*/, 'Thru-cut');        

          if (objArray[i].layers.length > 0) recurseLayers(objArray[i].layers);

          }

      }

       

       

      // save as

       

       

      var originalInteractionLevel = userInteractionLevel;

      userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

       

       

      saveFileToPDF ();

       

       

      function saveFileToPDF () {

          var doc = app.activeDocument;

          if ( app.documents.length > 0 ) {

              saveOpts = new PDFSaveOptions();

              saveOpts.compatibility = PDFCompatibility.ACROBAT5;

              saveOpts.generateThumbnails = true;

              saveOpts.preserveEditability = true;

              doc.save(saveOpts );

          }

      }

       

       

      userInteractionLevel = originalInteractionLevel;

       

       

      myDoc.close();