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

Count both Path Items and Group Items

Community Beginner ,
Apr 20, 2017 Apr 20, 2017

Copy link to clipboard

Copied

Hi all,

I was helped with a script on this forum and hoped somebody could help me with adapting it slightly.

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.

However this counts only 'path items' - is there a way I could rewrite it to count both path items and group items?

Often I type text, outline it and apply a stroke called 'cutter', but as this is a group object the script isn't counting it.

Script is below, thanks in advance for any help.

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; 
}
TOPICS
Scripting

Views

2.1K

Translate

Translate

Report

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
Explorer ,
Apr 21, 2017 Apr 21, 2017

Copy link to clipboard

Copied

If you are counting through specific layer name, you must use pageItems.length

Votes

Translate

Translate

Report

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
Participant ,
Apr 25, 2017 Apr 25, 2017

Copy link to clipboard

Copied

Unfortunately, 'layer.pathItems', unlike 'doc.pathItems', returns only the paths that are immediately below the layer, not all path items in that layer.

Screen Shot 2017-04-25 at 9.43.48 AM.png

You can loop through all of the document's path items and check which layer they belong to, using the PathItem 'layer' property.

var pathItems = doc.pathItems,   // get all path items in the document

     n = pathItems.length,              // get the length of the list of path items

     arr = [];                                     // empty array

while ( n-- )  // loop through all valid indices, starting at the end and working backwards

     pathItems.layer.name == 'cutter' &&   // check if the current path item is on the layer named 'cutter'

     checkColor( pathItems ) &&                 // check if the color is correct

     arr.push( pathItems );                           // both previous tests passed, push the path to our array

This might be slower if you have a bunch path items on layers other than the 'cutter' layer, since it will check each of them individually.

Another option would be to recursively loop through all pageItems in the cutter layer, which would be faster but would require tweaking the script a bit more.

Good luck!

Votes

Translate

Translate

Report

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
Valorous Hero ,
Apr 25, 2017 Apr 25, 2017

Copy link to clipboard

Copied

Recursive function is good for also navigating the objects with conditional blocks to account for all the various cases such as groups, compoundPaths and sub-layers.

Votes

Translate

Translate

Report

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 ,
May 01, 2017 May 01, 2017

Copy link to clipboard

Copied

LATEST

Thank you so much guys, with your help I have got the script working exactly to brief!

Votes

Translate

Translate

Report

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