Skip to main content
Known Participant
April 27, 2014
Answered

Drop Down List index - my script does not recognize the index line in my function

  • April 27, 2014
  • 2 replies
  • 1189 views

AE script Community-

I am a designer not a programmer so bare with me.  I'm just trying something simple, baby steps and I can't for the life of me to get this simple script to work.

I saw the script QUICK ADD and thought I would try to mimic this script for myself as a challenge.  I have watched David Torno's videos on Pro Video Coalition and made a simple Script UI from his example.  I searched for my answer on these forums here and tried to cobble together a simple script from the examples within these forums and can not get it to recognize the indices for my DROP DOWN LIST.  Here is my simple script:

{

function myScript(thisObj){

    function myScript_buildUI(thisObj){

        var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "Add This", undefined, {resizeable:true});

       

        res = "group{orientation:'row', alignment:['fill', 'fill'], alignChildren:['fill', 'fill'],\

                    groupOne: Group{orientation:'row', alignment:['fill', 'fill'], alignChildren:['fill', 'top'],\

                        myStaticText: StaticText{text:'New:'},\

                        myDropDownList: DropDownList{properties:{items:['Solid', 'Light']}},\

                },\

                    groupTwo: Group{orientation:'row', alignment:['fill', 'fill'], alignChildren:['fill', 'top'],\

                        myButton: Button{text:'Add'},\

                },\

                }";

       

        myPanel.grp = myPanel.add(res);

        //Defaults

        myPanel.grp.groupOne.myDropDownList.selection = 0;

              myPanel.grp.groupTwo.myButton.onClick = myButtonClick;

        //Setup panel sizing

        myPanel.layout.layout(true);

        myPanel.grp.minimumSize = myPanel.grp.size;

       

        //Make the panel resizeable

        myPanel.layout.resize();

        myPanel.onResizing = myPanel.onResize = function(){this.layout.resize()};

                    

        return myPanel;

    }      

function myButtonClick() {

    var myComp = app.project.activeItem;

   

    if (myComp != null && myComp instanceof CompItem) {

        if (myPanel.grp.groupOne.myDropDownList.selection.index == 0){

               myComp.layers.addSolid([0,0,0],"MySolid", myComp.width, myComp.height, 1)

      } else if (myPanel.grp.groupOne.myDropDownList.selection.index == 2){

               myComp.layers.addLight("Light", [myComp.width/2, myComp.height/2]);

        }

    }

}

// Make floating window

    var myScriptPal = myScript_buildUI(thisObj);

   

    if((myScriptPal != null) && (myScriptPal instanceof Window)){

        myScriptPal.center();

        myScriptPal.show();

    }

}

myScript (this);

}

If I comment out the lines

if (myPanel.grp.groupOne.myDropDownList.selection.index == 0){ and if (myPanel.grp.groupOne.myDropDownList.selection.index == 1){

the script will add the SOLID and the LIGHT.  But it will never work with these lines active - The ADD button does nothing.

I could just make use of the onChange with the drop down list, but I want to figure out how to connect onClick actions to the drop down list.

I copied this script piece from the forum and changed it to work with my script, but nothing seems to work:

buttonOne.onClick = onButtonOneClick;

function onButtonOneClick() {

     if (myPalette.dropdownlistOne.selection.index == 0) {

          // do this

     } else {

          // do this instead

     }

}

Can someone take a quick look at my top script and tell me why it will not work?  I've pretty much pulled out the last bit of hair I have over this - simple problem????

Thank you in advance.

This topic has been closed for replies.
Correct answer Dan Ebberts

Your button click function doesn't know what "myPanel" is. You could do something like this inside the function:

var myPanel = this.parent.parent.parent;

There are other ways to do it, like maybe a global variable that holds the panel reference.

Also, your second if is looking for selection.index == 2 instead of 1.

Dan

2 replies

UQg
Legend
April 27, 2014

Inside your main function (myScript) you have two functions (myScript_buildUI and myButtonClick).

The variable myPanel is defined only in the first one (myScript_buildUI) so myButtonClick is unaware of it.

There are several ways, the simplest is probably to replace myPanel.grp.groupOne.myDropDownList.selection.index

by myScriptPal.grp.groupOne.myDropDownList.selection.index, which will work since myScriptPal is defined inside your main function.

Another way is to slide the definition of myButtonClick inside myScript_buildUI, so that myButtonClick is aware of the myPanel variable.

You can also do:

function myButtonClick() {

    var myComp = app.project.activeItem;

    if (myComp != null && myComp instanceof CompItem) {

        switch(this.parent.parent.groupOne.myDropDownList.selection.index){     // this is the button, and this.parent.parent.groupOne.myDropDownList is the path from the button to the dropdown

            case 0 :

                myComp.layers.addSolid([0,0,0],"MySolid", myComp.width, myComp.height, 1);

                break;

            case 1 :

                myComp.layers.addLight("Light", [myComp.width/2, myComp.height/2]);

                break;

            default : ;

            };

        };

    return;

    };

Xavier

Edit : wow sorry, was sleeping, i didnt see there were answers already

Known Participant
April 28, 2014

Dan,

I just reread your message about making a GLOBAL VARIABLE.  Would that go outside of the SCRIPT UI BUILD function??? Where would I declare that variable?

Xavier,

Thank you as well.  I will look at that format in some depth tomorrow.

I appreciate your guys help.

Dwayne

Dan Ebberts
Community Expert
Community Expert
April 28, 2014

Xavier is right, since myScriptPal is defined outside the scope of your button function, you can just use it instead of "myPanel" inside the function.

Dan

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
April 27, 2014

Your button click function doesn't know what "myPanel" is. You could do something like this inside the function:

var myPanel = this.parent.parent.parent;

There are other ways to do it, like maybe a global variable that holds the panel reference.

Also, your second if is looking for selection.index == 2 instead of 1.

Dan

Known Participant
April 27, 2014

Oops, I removed some stuff from the drop down list and forgot to change the index to 1 for the light in my function.  My apologies.

Thank you, Dan, that worked perfectly.  I appreciate the quick response.  I knew it was something simple (banging my head against the wall).