Skip to main content
martinp38890197
Participant
July 30, 2015
Answered

simple javascript with .onClick function not working. I have the following small script, and cannot figure out why it is not running through. any ideas?

  • July 30, 2015
  • 2 replies
  • 1470 views

#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();

This topic has been closed for replies.
Correct answer Ten A

You can reference below:

Calling functions from UI palette

Ten

2 replies

Inspiring
July 30, 2015

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.

martinp38890197
Participant
July 31, 2015

Thanks W_J_T.

Actually the script posted isn't the real script, I just made it up to clarify my question ;-)

Ten A
Community Expert
Ten ACommunity ExpertCorrect answer
Community Expert
July 30, 2015

You can reference below:

Calling functions from UI palette

Ten

martinp38890197
Participant
July 30, 2015

cheers.