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

Get an array from a listbox

Participant ,
May 18, 2019 May 18, 2019

Hi!

I'm new in javascript and I'm having a trouble I can't fix.

I've wrote a script for Indesign and I want obtain and array from a multiselect listbox.

Untitled-2.jpg

The way I wrote I think I've two problems right now:

  1. The variable isn't working outside the function (for example, if I put an alert for "listOne" inside the function it prints the names I had selected. But, when I do the same outside the function it tells me "Object Invalid". It also alerts that "[Object ListItem]".
  2. I think the list is creating an object and, in order to work for me I need an array. Is there any way to convert the object into an array? I've tried a few things like keys and similar, but don't really know how to work with it.

Thanks anyway! rs

var listOne;

listParticipants();

function listParticipants(){

  

    //window preferences

    var myWindow = new Window ("dialog", "Definição dos Participantes");

   

    var groups = myWindow.add("group");

           groups.orientation = "row";

   

    var group1 = groups.add("panel");

           group1.orientation = "column";

    var list1 = group1.add ("listbox", undefined, ["cat", "dog", "horse", "mouse"], {multiselect: true});

   

    var group2 = groups.add("panel");

           group2.orientation = "column";

    var list2 = group2.add ('listbox', undefined, ['one', 'two', 'three'], {multiselect: true});

   

    var group3 = groups.add("panel");

          group3.orientation = "column";

    var list3 = group3.add ('listbox', undefined, ['one', 'two', 'three'], {multiselect: true});

   

    var button = myWindow.add("button", undefined, "OK", {name:"ok"});

    button.alignment = "right";

    

    myWindow.show ();

       

     //defining the list

    listOne = list1.selection;

   

    alert (listOne)

  

}

alert (listOne)

TOPICS
Scripting
2.2K
Translate
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

Advisor , May 18, 2019 May 18, 2019

try this.......

function listParticipants(){

   

    //window preferences

    var myWindow = new Window ("dialog", "Definição dos Participantes");

    

    var groups = myWindow.add("group");

           groups.orientation = "row";

    

    var group1 = groups.add("panel");

           group1.orientation = "column";

    var list1 = group1.add ("listbox", undefined, ["cat", "dog", "horse", "mouse"], {multiselect: true});

    

    var group2 = groups.add("panel");

           group2.orientation = "col

...
Translate
Advisor ,
May 18, 2019 May 18, 2019

try this.......

function listParticipants(){

   

    //window preferences

    var myWindow = new Window ("dialog", "Definição dos Participantes");

    

    var groups = myWindow.add("group");

           groups.orientation = "row";

    

    var group1 = groups.add("panel");

           group1.orientation = "column";

    var list1 = group1.add ("listbox", undefined, ["cat", "dog", "horse", "mouse"], {multiselect: true});

    

    var group2 = groups.add("panel");

           group2.orientation = "column";

    var list2 = group2.add ('listbox', undefined, ['one', 'two', 'three'], {multiselect: true});

    

    var group3 = groups.add("panel");

          group3.orientation = "column";

    var list3 = group3.add ('listbox', undefined, ['one', 'two', 'three'], {multiselect: true});

    

    var button = myWindow.add("button", undefined, "OK", {name:"ok"});

    button.alignment = "right";

     

    myWindow.show ();

        

     //defining the list

    listOne = list1.selection;

    

    alert (listOne)

 

selected = [];

for (var i = 0; i < listOne.length; i++) {

selected.push (listOne.text);

   }

}

   

alert (selected)

you'll have to do the same for the other two listboxes.

Regards

Mike

Translate
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
Engaged ,
May 18, 2019 May 18, 2019

The alert inside your function is working because it has access to all the variables it needs: list1, group1, groups, myWindow. If you declare all of these variables without using the keyword "var", they become globals and therefore are available to the top level of your script.

This means that all of your variables would have to be globals, which kind of defeats one of the purposes of writing listParticipants as a function. If it were me, I'd start like this:

var allLists = listParticipants(); // the variable will populated by your function.

after myWindow.show() in the function, list1 will contain an array of listItems. The info you want is the .text property of each listItem, so you need to loop throiugh them. For instance:

var listOne = new Array;

for (var i = 0, i < list1.length; i++) {

     listOne.push(list1.text);

}

At this point, listOne is an array of strings (cat, dog, whatever). I assume that you want to repeat this process for list2 and list3. So you create similar arrays called listTwo and listThree. At the end of your function, add:

return [listOne, listTwo, listThree];

which become the alllLists variable in the top level of your script. It's an array of three arrays, so allLists[0] is listOne, allLists[1] is listTwo, etc.

Hope this helps

Bob

Translate
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
Participant ,
May 19, 2019 May 19, 2019

Thank you so much!

Both answers helped a lot.

I couldn't make it work using this:

var listOne = new Array;

for (var i = 0, i < list1.length; i++) {

    listOne.push(list1.text);

}

So I put together both answers and it worked perfectly like this:

#target Indesign

var allLists = listParticipants();

function listParticipants(){

  

    //window preferences

    var myWindow = new Window ("dialog", "Definição dos Participantes");

   

    var groups = myWindow.add("group");

           groups.orientation = "row";

   

    var group1 = groups.add("panel");

           group1.orientation = "column";

    var list1 = group1.add ("listbox", undefined, ["cat", "dog", "horse", "mouse"], {multiselect: true});

   

    var group2 = groups.add("panel");

           group2.orientation = "column";

    var list2 = group2.add ('listbox', undefined, ['one', 'two', 'three'], {multiselect: true});

   

    var group3 = groups.add("panel");

          group3.orientation = "column";

    var list3 = group3.add ('listbox', undefined, ['one', 'two', 'three'], {multiselect: true});

   

    var button = myWindow.add("button", undefined, "OK", {name:"ok"});

    button.alignment = "right";

    

    myWindow.show ();

   

   

    //defining the lists   

    var aux = list1.selection;

    var listOne = new Array;

    for (var i = 0; i < aux.length; i++) {

        listOne.push (aux.text);

   }

    var aux = list2.selection;

    var listTwo = new Array;

    for (var i = 0; i < aux.length; i++) {

        listTwo.push (aux.text);

   }

    var aux = list3.selection;

    var listThree = new Array;

    for (var i = 0; i < aux.length; i++) {

        listThree.push (aux.text);

   }

    return [listOne, listTwo, listThree];

}

alert ("List One: " + allLists[0] + "\nList Two: " + allLists[1] + "\nList Three: " + allLists[2]);

Translate
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
Advisor ,
May 19, 2019 May 19, 2019

FYI

the for loop has a typo after the zero.....

var listOne = new Array;

for (var i = 0, i < list1.length; i++) {

    listOne.push(list1.text);

}

it should be....

var listOne = new Array;

for (var i = 0; i < list1.length; i++) {

    listOne.push(list1.text);

}

Regards

Mike

Translate
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
Participant ,
May 19, 2019 May 19, 2019

Yes! I had seen that, but it didn't work also. Mbut now I could solve. I already put it another code in order to create all the script and it's working perfectly the way you showed the function as a variable!

Thanks!

Translate
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
Engaged ,
May 19, 2019 May 19, 2019
LATEST

Good catch, Mike.

Bob

Translate
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