Skip to main content
Known Participant
January 26, 2017
Answered

Script UI layer selection not updating after restarting AE

  • January 26, 2017
  • 1 reply
  • 1113 views

I am making a dockable UI and I have two StaticText objects in my UI where I display certain information and one of them is number of selected layers.

I also have a refresh button that updates their text values.

When I restart or open another project while the script is docked it doesn't recognize any kind of layer selections. I have to close the script and open it again for the selection to work.

function curios_buildUI(thisObj)

    {

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

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

                groupOne: Group{orientation:'row',\

                staticText1: StaticText{text: 'Imported: 0', characters: 12},\

                staticText2: StaticText{text: 'Selected: 0', characters: 11},\

                },\

                groupTwo: Group{orientation:'row',\

                button1: Button{text: 'Import'},\

                button2: Button{text: 'Execute'},\

                },\

                groupThree: Group{orientation:'row',\

                button3: Button{text: 'Refresh'},\

                button4: Button{text: 'Help?'},\

                },\

            }";

       

        myPanel.grp = myPanel.add(res);

       

        //Defaults

        myPanel.grp.groupOne.staticText1.text = "Imported: " + 0;

        myPanel.grp.groupOne.staticText2.text = "Selected: " + 0;

        updateTexts();

       

        //Sizing

        myPanel.layout.layout(true);

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

       

        myPanel.layout.resize();

        myPanel.onResizing = myPanel.onResize = function()

        {

            this.layout.resize();

        }

                    

        myPanel.grp.groupThree.button3.onClick = function()

        {

            updateTexts();

        }

           

        function updateTexts()

        {

            var select;

            try

            {

                select = mainComp.selectedLayers;

            }

            catch (e) {}

           

           

            myPanel.grp.groupOne.staticText1.text = "Imported: " + names.length;

            if(select != null)

                myPanel.grp.groupOne.staticText2.text = "Selected: " + select.length;

            else

                myPanel.grp.groupOne.staticText2.text = "Selected: " + 0;

        }

        myPanel.layout.layout(true);

        return myPanel;

    }

This topic has been closed for replies.
Correct answer Paul Tuersley

The point Dan was making is that you shouldn't be trying to grab the active comp at the point where the script is launched. Chances are there is no comp active at that point, and anyway, you want it to reflect the comp that is active whenever the button is clicked, so that's when you should grab it.

function updateTexts()  { 

     mainComp = app.project.activeItem;

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

          select = mainComp.selectedLayers;             

          myPanel.grp.groupOne.staticText2.text = "Selected: " + select.length; 

     }

1 reply

Dan Ebberts
Community Expert
Community Expert
January 26, 2017

Where is mainComp defined? That probably needs to happen whenever you run updateTexts().

Dan

1suky1Author
Known Participant
January 26, 2017

Here you go. I added the missing code. mainComp is defined outside the buildUI function as app.project.activeItem. There's curiosScript function and inside is the whole code pretty much. I tried putting the mainComp outside the function but nothing happens.

edit:

If I use directly app.project.activeItem.selectedLayers instead of mainComp var it works.

function curiosScript(thisObj)

{

    var textFile;

    var names = new Array();

   

    var mainComp = app.project.activeItem;

    var nametags;

    var defaultWidth = 425.0268;

   

    function curios_buildUI(thisObj)

    {

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

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

                groupOne: Group{orientation:'row',\

                staticText1: StaticText{text: 'Imported: 0', characters: 12},\

                staticText2: StaticText{text: 'Selected: 0', characters: 11},\

                },\

                groupTwo: Group{orientation:'row',\

                button1: Button{text: 'Import'},\

                button2: Button{text: 'Execute'},\

                },\

                groupThree: Group{orientation:'row',\

                button3: Button{text: 'Refresh'},\

                button4: Button{text: 'Help?'},\

                },\

            }";

       

        myPanel.grp = myPanel.add(res);

       

        //Defaults

        myPanel.grp.groupOne.staticText1.text = "Imported: " + 0;

        myPanel.grp.groupOne.staticText2.text = "Selected: " + 0;

        updateTexts();

       

        //Sizing

        myPanel.layout.layout(true);

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

       

        myPanel.layout.resize();

        myPanel.onResizing = myPanel.onResize = function()

        {

            this.layout.resize();

        }

       

        myPanel.grp.groupTwo.button1.onClick = function()

        {

            openFile();

            updateTexts();  

        }

       

        myPanel.grp.groupThree.button3.onClick = function()

        {

            updateTexts();

        }

       

        function updateTexts()

        {

            var select;

            try

            {

                select = mainComp.selectedLayers;

            }

            catch (e) {}

           

           

            myPanel.grp.groupOne.staticText1.text = "Imported: " + names.length;

            if(select != null)

                myPanel.grp.groupOne.staticText2.text = "Selected: " + select.length;

            else

                myPanel.grp.groupOne.staticText2.text = "Selected: " + 0;

        }

        myPanel.layout.layout(true);

        return myPanel;

    }

    var myScriptPal = curios_buildUI(thisObj);

  

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

    {

        myScriptPal.center();

        myScriptPal.show();

    }  

   

    if(this instanceof Panel)

        myScriptPal.show();

}

curiosScript(this);

Paul TuersleyCorrect answer
Inspiring
January 26, 2017

The point Dan was making is that you shouldn't be trying to grab the active comp at the point where the script is launched. Chances are there is no comp active at that point, and anyway, you want it to reflect the comp that is active whenever the button is clicked, so that's when you should grab it.

function updateTexts()  { 

     mainComp = app.project.activeItem;

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

          select = mainComp.selectedLayers;             

          myPanel.grp.groupOne.staticText2.text = "Selected: " + select.length; 

     }