Copy link to clipboard
Copied
#target Illustrator
#targetengine main
// Function
function count_selected_objects(){
alert ("Start of function"); /// works OK!
var myDocument = app.activeDocument;
var items = selection;
var totalSelected = items.length;
for ( var i = 0 ; i < totalSelected ; i++){
//var my_elementy = app.activeDocument.selection;
alert("No. "+i);
}
}
// Palette Window
var win = new Window("palette", "Count elements");
win.alignChildren = "fill";
win.my_button = win.add("button",undefined, "Run");
win.my_button.onClick = function(){
count_selected_objects();
}
win.show();
Copy link to clipboard
Copied
Copy link to clipboard
Copied
cheers.
Copy link to clipboard
Copied
Just as a side note:
Be careful using "alerts" inside loops. If you have 25-to-1000+ items selected you would have to deal with all those alerts as the loop iterates. A better way would be to declare a variable and increment the variable in the loop then report the variable only once, after the loop.
// For example:
var totalCount = 0; // before the loop
totalCount += 1; // in the loop
alert(totalCount + " Items Selected"); // after the loop
However in the case of your code you do not even need a loop to accomplish the count of the selected, all you need is the length of the selected.
var items = app.activeDocument.selection.length;
alert(items + " Items Selected");
Just wanted to point those things out, aside from needing BridgeTalk.
Copy link to clipboard
Copied
Thanks W_J_T.
Actually the script posted isn't the real script, I just made it up to clarify my question 😉