• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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?

New Here ,
Jul 30, 2015 Jul 30, 2015

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

TOPICS
Scripting

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 30, 2015 Jul 30, 2015

You can reference below:

Calling functions from UI palette

Ten

Votes

Translate

Translate
Adobe
Community Expert ,
Jul 30, 2015 Jul 30, 2015

Copy link to clipboard

Copied

You can reference below:

Calling functions from UI palette

Ten

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 30, 2015 Jul 30, 2015

Copy link to clipboard

Copied

cheers.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jul 30, 2015 Jul 30, 2015

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 30, 2015 Jul 30, 2015

Copy link to clipboard

Copied

LATEST

Thanks W_J_T.

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines