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 | ||
return o; | ||
} | ||
function processLayerItems ( layer ) { | ||
var pathItems = layer.pathItems, | ||
n = pathItems.length, arr = []; | ||
while (n-- ) checkColor ( 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; | ||
} |
Copy link to clipboard
Copied
If you are counting through specific layer name, you must use pageItems.length
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.
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
checkColor( pathItems
arr.push( pathItems
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!
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.
Copy link to clipboard
Copied
Thank you so much guys, with your help I have got the script working exactly to brief!