8 Oct 2008
How many trees in the City Map? (Object counts in Adobe Illustrator CS3)
This is one of the questions that the organiser asked me a few days ago.
Here is a short script, written in Javascript, which, when run from within Illustrator, will count and list the number of occurrences of each type of symbol (or SymbolItem) showing in the visible layers. As a bonus, it also counts the number of paths (i.e. areas and lines) although these are only broken out by the type of colour associated with each, and it also prints out the total area – which should be taken with a pinch of salt, as it gets messy with subtracted areas and areal holes. It is extremely basic, but I couldn’t find an equivalent script on a cursory trawl of the internet, so here it is.
(If you uncomment the commented-out lines, then you get an animation while each object is examined, however this does massively slow down the speed at which you get the result, so may be painful for large numbers of object. It does look purty though.)
//Count number of objects and areas/lines in AI CS3
if ( app.documents.length > 0 ) {
noOfSymbols = app.activeDocument.symbolItems.length;
inc = 0
symbols = {}
while (inc < noOfSymbols)
{
thesymbol = app.activeDocument.symbolItems[inc]
tname = thesymbol.symbol.name
inc++
if (thesymbol.layer.visible)
{
if (symbols[tname] == undefined)
{
symbols[tname] = {}
symbols[tname]["name"] = tname
symbols[tname]["count"] = 0
}
symbols[tname]["count"]++
//thesymbol.selected = true
//redraw()
//thesymbol.selected = false
}
}
noOfPathItems = app.activeDocument.pathItems.length;
inc = 0
area = 0
pathItems = {}
while (inc < noOfPathItems)
{
thepathitem = app.activeDocument.pathItems[inc]
cftype = thepathitem.fillColor.typename
cstype = thepathitem.strokeColor.typename
tname = "Fill: " + cftype + ", Stroke: " + cstype
inc++
if (thepathitem.layer.visible)
{
if (pathItems[name] == undefined)
{
pathItems[tname] = {}
pathItems[tname]["name"] = tname
pathItems[tname]["count"] = 0
}
pathItems[tname]["count"]++
if (thepathitem.area > 0)
{
area += thepathitem.area
}
else
{
area -= thepathitem.area
}
//thepathitem.selected = true
//redraw()
//thepathitem.selected = false
}
}
res = "Count of currently visible items"
res += "\nSymbols:\n\n"
for (var i in symbols)
{
res += symbols[i]["name"]+" - "+symbols[i]["count"]+"\n"
}
res += "\nPaths:\n\n"
for (var i in pathItems)
{
res += pathItems[i]["name"]+" - "+pathItems[i]["count"]+"\n"
}
res += "Area: " + area
alert(res)
}
Here’s an example output:

Adobe’s AI CS3 Javascript Reference was very useful.
To answer the question… nearly 800.
That’s a different answer to what you’d told me!
Brooner
October 9th, 2008 at 22:31permalink
Ah – yes – a few fell down in the strong winds a couple of days ago.
Ollie
October 9th, 2008 at 23:23permalink