2 Replies Latest reply on Mar 7, 2017 12:54 AM by lucas.maximus

    Help with converting an InDesign script to Illustrator

    lucas.maximus

      Hi all,

       

      I have this snippet that works in InDesign, but I'm guessing due to the object model won't do anything in Illustrator.

      This counts how many objects in the document sit on a layer called 'cutter' and also have a stroke applied in a colour called 'cutter'

      The script alert displays the total count of objects meeting both these conditions.


      Could anyone please point me in the right direction regarding the changes I need to make?
      This is part of a larger script but I'm taking the re-write one part at a time.

       

      Here goes and thank you in advance for any help;

       

      var myDoc = app.activeDocument;     

      var myLayerObj = app.activeDocument.layers.item("cutter").allPageItems;   

      var myArray = new Array();   

         

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

          var myObj = myLayerObj[i];   

          if((myObj.strokeColor.name == "cutter") && (myObj.fillColor.name == "None")){  

              myArray.push(myObj);   

          }   

      }   

      alert("Number of 'cutter' stroke applied objects: "+myArray.length); 

        • 1. Re: Help with converting an InDesign script to Illustrator
          Loic.Aigon Adobe Community Professional

          Hi,

           

          Indeed there are a lot of differencies between the two models.

           

          1) Indesign's item() vs Illustrator items

          InDesign has the concept of collections. A collection is mostly like an array except that is has specific methods. Item() is one of thjem and consists in reaching a member of taht collection. Generally you use an index:

          app.activeDocument.layers.item( 0 ) // [0] may be preferred …layers[0]

          If you want to use the object name, you would prefer app.activeDocument.layers.itemByName ("cutter");

           

          Illustrator, doesn't know item() but offers a getByName function. So you can have:

          var myLayerObj = app.activeDocument.layers.getByName("cutter");

           

          2) Object reference

          Be aware that in both case you can set object in variable that won't throw an error until you try to use them.

          For example:

          app.documents[0] won't fail if no documents are open in both apps. But app.documents[0].name will because you are resolving the reference and if no valid, it will throw.

          InDesign has a sugar property : isValid

          app.documents[0].isValid will help check that the object is ok to work with.

          Illustrator on the contrary has no such mechanisms. So it's better to double check objects prior to use them.

          In your layer case, I tend to loop through objects and store thme into an object so I can check them at any time without error.

           

          3) allPageItems vs pageItems

          allPageItems is InDesign only and will return an array of every single page items of the parent. The closest in Ai would be doc.pageItems. Be aware that InDesign also owns a pageItems collection but it's very different. InDesign PageItems will return every single pageItems of the parent when Illustrator resolves not only the native items that belongs to the parent but also the internal ones.

          Type an "O" and outline it. In ID, pageItems.length will return 1, 2 in AI. 2 because there are two pathItems elements in the doc.

          So in AI, you better work with native collections (i.e. pathItems, groupItems…)

           


          4) Color

          In AI, a fillColor might not return a name so you need to look at its properties instead.

           

          main();
          
          
          function main() {
            var doc, layers;
          
            if ( !app.documents.length ) return;
          
            doc = app.activeDocument;
            layers = getLayersInfos ( doc );
          
            if ( !layers.cutter ) return;
          
            processLayerItems(layers.cutter );
          }
          
          
          function getLayersInfos ( doc ) {
            var o = {};
            var layers = doc.layers,
            n = layers.length;
            while ( n--) o[layers[n].name]=layers[n];
            return o;
          }
          
          
          
          
          function processLayerItems ( layer ) {
            var pathItems = layer.pathItems, 
            n = pathItems.length, arr = [];
          
            while (n-- ) checkColor ( pathItems[n] ) && arr.push(pathItems[n]); 
          
          
          
          alert("Number of 'cutter' stroke applied objects: "+arr.length); 
          }
          
          
          
          
          function checkColor ( myObj ) {
            if ( myObj.fillColor.typename=="GrayColor"
            && myObj.fillColor.gray == 0
            && myObj.strokeColor.typename=="SpotColor"
            && myObj.strokeColor.spot.color.cyan == 100
            && myObj.strokeColor.spot.color.magenta == 0
            && myObj.strokeColor.spot.color.yellow == 0
            && myObj.strokeColor.spot.color.black == 0 ) return true;
          
            return false;
          }
          
          2 people found this helpful
          • 2. Re: Help with converting an InDesign script to Illustrator
            lucas.maximus Level 1

            Thank you so much for helping with the script - and also for going the extra mile and explaining the differences in more detail, it makes very interesting reading and is very valuable information. Much appreciated.

            Regards

            Luke