2 Replies Latest reply on Jul 10, 2017 11:35 PM by denisc31888652

    Illustrator 2017 script for creating layers from spot colors in document

    denisc31888652

      Hello, i have script that I use in my workflow everyday but with new Illustrator 2017 I get an error.

      The script is trying to find 4 spot color in document (Cut, Crease, Reverse crease and Outside bleed) and put every single color in own layer named as follows:

      Spot color "Cut" > layer "Thru-cut"

      Spot color "Crease" > layer "Crease"

      Spot color "Outside bleed" > layer "Bleed"

      Spot color "Reverse crease" > layer "Crease"

       

      Script that I have used in old Illustrator is this:

       

      #target Illustrator
      //Apply to myDoc the active document
      var myDoc = app.activeDocument;
      //define firts caracter and how many layers do you need
      var layerIniName =65;
      var numberOfLayers=2;

      //Create the layers
      for(var i=0; i<=numberOfLayers; i++) {
      var layerName = String.fromCharCode(layerIniName)
      var myLayer = myDoc.layers.add();
      myLayer.name = layerName
      layerIniName++

      // JavaScript Document 
      var doc = app.activeDocument; 
      // name indexed object 
      var layernames = { 
      'A':'Bleed / Grafika', 
      'B':'Crease', 
      'C':'Thru-cut' 
      }; 
      // loop through all layers 
      for (var i = 0; i < doc.layers.length; i++) 

      //Set up Variable to access layer name 
      var currentLayer = app.activeDocument.layers[i]; 
      if (layernames[currentLayer.name]) 

        currentLayer.name = layernames[currentLayer.name]; 




      var doc = app.activeDocument;
      var paths = doc.pathItems;
      var destLayer = doc.layers.getByName ("Thru-cut");


      for (var g=0; g<paths.length;g++){
          if (paths[g].strokeColor.typename == "SpotColor" && paths[g].strokeColor.spot.name == "Cut") {
              paths[g].move(destLayer, ElementPlacement.PLACEATBEGINNING);
          };
      };

      var doc = app.activeDocument;
      var paths = doc.pathItems;
      var destLayer = doc.layers.getByName ("Crease");


      for (var g=0; g<paths.length;g++){
          if (paths[g].strokeColor.typename == "SpotColor" && paths[g].strokeColor.spot.name == "Crease") {
              paths[g].move(destLayer, ElementPlacement.PLACEATBEGINNING);
          };
      };

      var doc = app.activeDocument;
      var paths = doc.pathItems;
      var destLayer = doc.layers.getByName ("Bleed / Grafika");


      for (var g=0; g<paths.length;g++){
          if (paths[g].strokeColor.typename == "SpotColor" && paths[g].strokeColor.spot.name == "Outside Bleed") {
              paths[g].move(destLayer, ElementPlacement.PLACEATBEGINNING);
          };
      };

      var doc = app.activeDocument;
      var paths = doc.pathItems;
      var destLayer = doc.layers.getByName ("Crease");


      for (var g=0; g<paths.length;g++){
          if (paths[g].strokeColor.typename == "SpotColor" && paths[g].strokeColor.spot.name == "Reverse crease") {
              paths[g].move(destLayer, ElementPlacement.PLACEATBEGINNING);
          };
      };

       

      Can anybody help me resolve this script with new Illustrator?

      Thanks

      Denis

       

      Sample file and script link:

      https://www.dropbox.com/sh/hiffbk7yd3lspjg/AACdWhCbuMf2QC3fET8dd4mta?dl=0

        • 1. Re: Illustrator 2017 script for creating layers from spot colors in document
          Loic.Aigon Adobe Community Professional

          Hi,

           

          Let's try to make things simple.

           

          var main = function(layerCount) {
          
          var doc, layers;
          
          if ( !app.documents.length ) {
          alert("This scripts needs an open document !");
          return;
          }
          
          
          //The doc
          doc = app.activeDocument;
          
          //Adding useful layers
          addLayers ( doc );
          
          //Moving items onto specific layers
          moveItems ( doc );
          
          }
          
          
          
          
          var moveItems = function ( doc ) {
          var items = doc.pageItems, item, n = items.length, sc, tn, sn, 
          layers = getLayersDB(doc),
          cutLayer = layers['Thru-cut'],
          creaseLayer = layers.Crease,
          bleedLayer = layers['Bleed / Grafika'];
          
          while ( n-- ) {
          item = items[n];
          sc = item.strokeColor;
          tn = sc.typename;
          
          if ( tn =="SpotColor" ) {
          sn = sc.spot.name;
          if ( sn=="Cut") {
          item.move(cutLayer, ElementPlacement.PLACEATBEGINNING);
          }
          else if ( /^(Reverse )?Crease$/i.test( sn ) ){
          item.move(creaseLayer, ElementPlacement.PLACEATBEGINNING);
          }
          else if ( sn=="Outside Bleed") {
          item.move(bleedLayer, ElementPlacement.PLACEATBEGINNING);
          }
          }
          }
          
          }
          
          
          var addLayers = function ( doc ) {
          var layers = getLayersDB ( doc );
          var layerNames = [
          'Bleed / Grafika',
          'Crease',
          'Thru-cut'
          ],
          name;
          
          while ( name = layerNames.pop() )  !layers[name]  && addLayer ( doc, name );
          };
          
          
          var addLayer = function ( doc, name ) {
          doc.layers.add().name = name;
          }
          
          
          var getLayersDB = function ( doc ) {
          var db = {}, layers = doc.layers, n = layers.length, layer;
          
          while ( n-- ) {
          layer = layers[n];
          db[layer.name] = layer;
          }
          return db;
          }
          
          
          main();
          

           

           

          HTH