Skip to main content
Participant
June 4, 2018
Answered

SYMBOLS COUNT TABLE

  • June 4, 2018
  • 3 replies
  • 3745 views

It´s possible to make a script that writes a table with all symbols instances count ? how difficult it would be?

This topic has been closed for replies.
Correct answer CarlosCanto

moving to the new Forums ruined all existing scripts, try this version

var main = function() { 

 

var doc; 

 

if ( !app.documents.length ) { 

alert("This scripts needs an open document !"); 

return; 

} 

 

 

doc = app.activeDocument; 

 

var symbols = doc.symbolItems, n = symbols.length, nSymbol, db = {}, arr = []; 

 

while ( n-- ) { 

nSymbol = symbols[n].symbol; 

db[ nSymbol.name ]  = db[ nSymbol.name ] || 0; 

db[ nSymbol.name ]++; 

} 

 

 

for ( prop in db ) { 

arr.push ( prop+" : "+db[prop] ); 

} 

var result = doc.textFrames.add();

result.contents = ( "Result:\r"+arr.join("\r") );

result.top = 700;

result.left = 400;

redraw();

 

} 

 

 

main(); 

3 replies

CarlosCanto
Community Expert
Community Expert
November 5, 2020

Hi keith, what do you mean? can you elaborate?

Participant
March 8, 2023

Is there a mac version of this?

Sergey Osokin
Inspiring
October 13, 2023

I found it only works correctly if all the symbols are in the same layer, as in your screenshot. As soon as I move some symbols to another (inactive/hidden) layer, it starts counting them again.


 

function main() {
  var title = "Symbol Instances:";
  var separator = ": ";

  if (!app.documents.length) {
    alert("This scripts needs an open document !");
    return;
  }

  var doc = app.activeDocument;
  var symbols = doc.symbolItems;
  var n = symbols.length;
  var nSymbol, db = {};
  var arr = [];

  while (n--) {
    if (isHidden(symbols[n])) continue;
    nSymbol = symbols[n].symbol;
    db[nSymbol.name] = db[nSymbol.name] || 0;
    db[nSymbol.name]++;
  }

  for (prop in db) {
    arr.push(prop + separator + db[prop]);
  }

  var lay = getEditableLayer(doc);
  var result = lay.textFrames.add();
  result.contents = (title + "\r" + arr.join("\r"));
  result.top = 700;
  result.left = 400;
  redraw();
}

function isHidden(item) {
  if (item.hasOwnProperty("hidden") && item.hidden == true) {
    return true;
  }
  if (item.hasOwnProperty("visible") && item.visible == false) {
    return true;
  }
  var result = false;
  var prnt = item.parent;
  switch (prnt.typename) {
    case "GroupItem":
      result = isHidden(prnt);
      break;
    case "Layer":
      result = isHidden(prnt);
      break;
    default:
      break;
  }
  return result;
}

function getEditableLayer(doc) {
  var aLay = doc.activeLayer;
  if (aLay.visible && !aLay.locked) return aLay;
  for (var i = 0, len = doc.layers.length; i < len; i++) {
    var curLay = doc.layers[i];
    if (curLay.visible && !curLay.locked) {
      doc.activeLayer = curLay;
      return curLay;
    }
  }
  doc.layers[0].visible = true
  doc.layers[0].locked = false;
  doc.activeLayer = doc.layers[0];
  return doc.layers[0];
}

main();

 

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
July 10, 2020

moving to the new Forums ruined all existing scripts, try this version

var main = function() { 

 

var doc; 

 

if ( !app.documents.length ) { 

alert("This scripts needs an open document !"); 

return; 

} 

 

 

doc = app.activeDocument; 

 

var symbols = doc.symbolItems, n = symbols.length, nSymbol, db = {}, arr = []; 

 

while ( n-- ) { 

nSymbol = symbols[n].symbol; 

db[ nSymbol.name ]  = db[ nSymbol.name ] || 0; 

db[ nSymbol.name ]++; 

} 

 

 

for ( prop in db ) { 

arr.push ( prop+" : "+db[prop] ); 

} 

var result = doc.textFrames.add();

result.contents = ( "Result:\r"+arr.join("\r") );

result.top = 700;

result.left = 400;

redraw();

 

} 

 

 

main(); 
Known Participant
July 10, 2020

Thank you so much, that worked! 

Loic.Aigon
Legend
June 5, 2018

var main = function() {

var doc;

if ( !app.documents.length ) {

alert("This scripts needs an open document !");

return;

}

doc = app.activeDocument;

var symbols = doc.symbolItems, n = symbols.length, nSymbol, db = {}, arr = [];

while ( n-- ) {

nSymbol = symbols.symbol;

db[ nSymbol.name ]  = db[ nSymbol.name ] || 0;

db[ nSymbol.name ]++;

}

for ( prop in db ) {

arr.push ( prop+" : "+db[prop] );

}

alert( "Result:\r"+arr.join("\r") );

}

main();

Participant
June 5, 2018

thank you very much !

I made a little change in the script making illustrator to paste the results in a text frame, because I coulnd´t copy the text from the alert.

var main = function() { 

 

var doc; 

 

if ( !app.documents.length ) { 

alert("This scripts needs an open document !"); 

return; 

 

 

doc = app.activeDocument; 

 

var symbols = doc.symbolItems, n = symbols.length, nSymbol, db = {}, arr = []; 

 

while ( n-- ) { 

nSymbol = symbols.symbol; 

db[ nSymbol.name ]  = db[ nSymbol.name ] || 0; 

db[ nSymbol.name ]++; 

 

 

for ( prop in db ) { 

arr.push ( prop+" : "+db[prop] ); 

var result = doc.textFrames.add();

result.contents = ( "Result:\r"+arr.join("\r") );

result.top = 700;

result.left = 400;

redraw();

 

 

 

main(); 

I´ll try to refine a bit to make a table, but it works anyway.

Thank you !

Known Participant
July 10, 2020

Hello! 

 

Thank you for these! 

 

I am gettign an error. is it becuase I named my symbols? They are all static graphic symbols

 

thank you!