Categories
Uncategorized

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  0)
    {
      area += thepathitem.area
    }
    else
    {
      area -= thepathitem.area
    }
    //thepathitem.selected = true
    //redraw()
    //thepathitem.selected = false

  }
}
  
res = "Count of currently visible items"
res += "nSymbols:nn"
for (var i in symbols)
{
  res += symbols[i]["name"]+" - "+symbols[i]["count"]+"n"
} 
res += "nPaths:nn"
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.

2 replies on “How many trees in the City Map? (Object counts in Adobe Illustrator CS3)”

Leave a Reply

Your email address will not be published. Required fields are marked *