Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help with converting an InDesign script to Illustrator

Community Beginner ,
Mar 05, 2017 Mar 05, 2017

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;   

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

        myArray.push(myObj);   

    }   

}   

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

TOPICS
Scripting
419
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
People's Champ ,
Mar 06, 2017 Mar 06, 2017

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.name]=layers;

  return o;

}

function processLayerItems ( layer ) {

  var pathItems = layer.pathItems,

  n = pathItems.length, arr = [];

  while (n-- ) checkColor ( pathItems ) && arr.push(pathItems);

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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 07, 2017 Mar 07, 2017
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines