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

Listbox .selectedItem, .selection (scripting question)

Explorer ,
Apr 04, 2017 Apr 04, 2017

I have a listbox, I want to be able to take the value from a selected item, hit my apply button and it will put the value into my text layer. I've tried .getDisplayedItem, .selectedItem, .selection, .prettyMuchAnythingICanFindOnline.

 

The only one that has brough back anything is myList.selection, which brings back "NULL", everything else either brings back Null is not an object, or it does nothing. Anyone know how to resolve this before I decide to use a dropdownlist instead. I would prefer to have a listbox, just because I want multicolumn.

 

//BTN: to apply selected listbox item

var myBtn = w.add ("button", undefined, "Apply");

var myComp = app.project.activeItem;

var Text1 = myList.selection;

myBtn.onClick = function(){

myComp.layer("Amount").property("ADBE Text Properties").property("ADBE Text Document").setValue(Text1);

}

TOPICS
Scripting
3.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
Enthusiast ,
Apr 04, 2017 Apr 04, 2017

You Text1 variable should be inside .onClick function.

//BTN: to apply selected listbox item

var myBtn = w.add ("button", undefined, "Apply");

myBtn.onClick = function(){

var myComp = app.project.activeItem; if(!myComp || myComp.typeName !== "Composition") return;

var Text1 = myList.selection;

var textValue = myComp.layer("Amount").property("ADBE Text Properties").property("ADBE Text Document").value;

textValue.text = Text1;

myComp.layer("Amount").property("ADBE Text Properties").property("ADBE Text Document").setValue(textValue);

}

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
Explorer ,
Apr 05, 2017 Apr 05, 2017

This worked great (ish), I got a new value rather than Null, I now received (ARRAY) (I know why i got this, but I don't want this hahaha). As you can gather I am new to this. I am learning as I go along and improving in strides, but I have a long way to go. Trying to find examples is proving difficult and although it now makes sense to put the variables into the function. I'm slowly getting used to this.

So...I forgot to take into account in my previous post, two things.


Firstly is the Array, how do I turn this into a value?

Secondly. Like the above script you have written, I wanted it to take the value out so I could make the script look like this. So by using the code you provided. I made tweaks to the area I want it too.

  1. //BTN: to apply selected listbox item 
  2. var myBtn = w.add ("button", undefined, "Apply"); 
  3.  
  4. myBtn.onClick = function(){ 
  5.  
  6. var myComp = app.project.activeItem; if(!myComp || myComp.typeName !== "Composition") return;  
  7. var Text1 = "Column2"
  8. var textValue = myComp.layer("Column1").property("ADBE Text Properties").property("ADBE Text Document").value; 
  9. textValue.text = Text1;  
  10. myComp.layer("Column1").property("ADBE Text Properties").property("ADBE Text Document").setValue(textValue); 
  11.  
  12. }

Column 1 is my Array, that comes out [Bar 1, Bar 2, Bar 3...etc]. This will be used to refer to myComp.layer("Bar 1")

Column 2 is my value from my "edittext", although I could reference this straight from the "edittext", this may want to be changed, or referenced again later on, so I do need it to come from Column2 of the listbox.


This is my script that add the values to Column 1 and 2 of myListbox

  1. var input = w.add ("edittext");
  2. input.active = true;
  3. var b = w.add ("button", undefined, "Insert", {name: "ok"});
  4. b.onClick = function () {
  5. insert_item (myList, input.text);
  6. input.text = "";
  7. input.active = true;
  8. }
  9. function insert_item (list_obj, new_item)
  10. {
  11. var new_item = testArr.push ((testArr.length + 1));
  12. var myItem = list_obj.add ("item", "Bar" + " " + new_item);
  13. myItem.subItems[0].text = (input.text);
  14. }
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
Explorer ,
Apr 05, 2017 Apr 05, 2017

Ok so I managed to get rid of the value Array and reveal the value by changing myList.selection to myList.selection[0].text

How do I get the value of Column 2. I tried myList.selection[1].text, myList.subItems.selection[0].text, myList.selection[0].subItems[0].text .... etc

At the moment, this is how it should look


//BTN: to apply selected listbox item 

var myBtn = w.add ("button", undefined, "Apply"); 

 

myBtn.onClick = function(){ 

 

var myComp = app.project.activeItem; if(!myComp || myComp.typeName !== "Composition") return;  

var Text1 = myList.selection[0].text;

var Text2 = "Column 2"

var textValue = myComp.layer(""+Text1+"").property("ADBE Text Properties").property("ADBE Text Document").value; 

textValue.text = Text2;  

myComp.layer(""+Text1+"").property("ADBE Text Properties").property("ADBE Text Document").setValue(textValue);  

}

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
Advocate ,
Apr 05, 2017 Apr 05, 2017

This should normally work (havent tried).

Apparently you changed your list to multiselect: true, so that the selection is now an array of ListItems(s) instead of a single ListItem.

var myBtn = w.add ("button", undefined, "Apply");

function updateButtons(){

    // call this function whenever something changes in the ui that should prevent some buttons to be clickable

    myBtn.enabled = (myList.selection !== null);

    // and maybe some other buttons to update

    }

myList.onChange = function(){

    updateButtons();

    // and maybe something else

    };

myBtn.onClick = function(){

    var listSel = myList.selection; if (listSel===null){alert("Debug: updating buttons has failed somehow"); return;};

    var myComp = app.project.activeItem; if(!myComp || myComp.typeName !== "Composition") return;

    var text1, text2;

    var n;

    if (!myList.properties.multiselect){

        // use this for single selection list

        // (listSel is a ListItem)

        text1 = listSel.text;

        text2 = listSel.subItems[0].text;

        setTextLayerText(comp, text1, text2);

        }

    else{

        // use this for a multiselect list

        // (listSel is an Array of ListItem)

        for (n=0; n<listSel.length; n++){

            text1 = listSel.text;

            text2 = listSel.subItems[0].text;

            setTextLayerText(comp, text1, text2);

            };

        };

    };

function setTextLayerText(comp, textLayerName, textToSet){/*some stuff*/;};

Xavier

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
Advocate ,
Apr 04, 2017 Apr 04, 2017

For a single selection listbox, listbox.selection can be null or a ListItem.

In the first case, there is no text to retrieve, in the second one, it is listbox.selection.text

Therefore you have to check.

Side note : The variables 'myComp' and 'Text1' should be defined INSIDE the onClick callback, not outside, since they are meant to change over time, and what you need is their current value, not the ones they had when the script was launched.

Xavier

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
Explorer ,
Apr 05, 2017 Apr 05, 2017

Cheers for the quick reply....I'll have a play around with this once I finished another job.

I get everything you have written except for this end point

  1. function setTextLayerText(comp, textLayerName, textToSet){/*some stuff*/;};

I assume it is just doing this what you have referenced in the single or multi select (setTextLayerText(comp, text1, text2)

which is essentially doing this.

myComp.layer(""+text1+"").property("ADBE Text Properties").property("ADBE Text Document").setValue(""+text2+"");


Sorry this is a new one for me and I've not seen that way of writing it unless you are creating a button for example.

I guess with the way I want to use it at the moment. I don't need multi select. I just thought that if there is 2 layers I need to change. You can highlight them both and hit apply rather than individually, but at the moment there is no need to include this.

I only started learning this 3 weeks ago and I appear to be picking this up easier than french.

After all the waffle...Thanks for your help. As I read, I learn and as I learn my brain turns to mush and I need a nap.

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
Explorer ,
Apr 05, 2017 Apr 05, 2017
LATEST

I tried that script and struggled to get it to work. I turned off the multiselect for time being. Got rid of the debug as it was causing me jip and got rid of the if and else and just kept the for loop which helps this alot.

I like the enable function which I didn't even know existed, but again...I've only been learning this for about 3 weeks.

//BTN: to apply selected listbox item 

var myBtn = w.add ("button", undefined, "Apply");

function updateButtons(){ 

    // call this function whenever something changes in the ui that should prevent some buttons to be clickable 

    myBtn.enabled = (myList.selection !== null); 

    // and maybe some other buttons to update 

    } 

myList.onChange = function(){ 

    updateButtons(); 

    // and maybe something else 

    };

myBtn.onClick = function(){

   

var listSel = myList.selection;  

var myComp = app.project.activeItem; if(!myComp || myComp.typeName !== "Composition") return;

var n;

for (n=0; n<listSel.length; n++){

var Text1 = listSel.text;

var Text2 = listSel.subItems[0].text

var textValue = myComp.layer(""+Text1+"").property("ADBE Text Properties").property("ADBE Text Document").value;

textValue.text = Text2;

};

myComp.layer(""+Text1+"").property("ADBE Text Properties").property("ADBE Text Document").setValue(textValue); 

};


Cheers for the help.

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