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

send parameter when button clicked (panels)

Community Beginner ,
May 28, 2013 May 28, 2013

Hi everyone,

I am currently trying to create a dockable panel on AE with ExtendScript, and I have an issue when I creates the buttons.

the thing is the number of buttons and their callbacks isn't fixed, and is sended to the script through a socket connected to a python interpreter.

this part works well, depending on what I send on the socket, AE creates more or less buttons.

The problem comes when I have to click the buttons. For the moment, I use this:

function createUI(thisObj)

          {

                    myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "test panel", [100, 100, 300, 300]);

                    var panelstr = "myPanel.";

                    for(var i=0 ; i < commands_lenght ; i++)

                    {

                              var cmd_name = commands_name.Name;

                              cmd_name = cmd_name.replace(/\s+/g, '');

                              cmd_name = cmd_name.split('.').join("");

                              var cmd = panelstr.concat(cmd_name);

                              button = eval(cmd);

                              var posb = 5 + 25*i;

                              var pose = posb + 20;

                              button = myPanel.add("button", [0, posb, 100, pose], commands_name.Name);

                              button.onClick = testclicked;

                    }

                    return myPanel;

          }

          function testclicked()

          {

                    alert("you clicked, well done! \nwant a medal?");

          }

or what I'd like is to send a parameter to the function called when (onClick)

something like that:

function createUI(thisObj)

          {

                              button.onClick = testclicked(parameter);

          }

                    return myPanel;

          }

          function testclicked(parameter)

          {

                    alert("you clicked the" + parameter + " button, well done! \nwant a medal?");

          }

and so far, it didn't worked.

so I wonder if it is even possible to do that ^^'

thank you in advance for your answers

Justin

TOPICS
Scripting
3.0K
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

Participant , May 30, 2013 May 30, 2013

You can't add an argument to a callback.

What are you trying to do exactly? The problem is that I don't understand what the "parameter" variable should be.

If you need to use a specific variable inside your callback you could just add it to your button variables, I haven't tried but wouldn't this work?


button1.onClick = testclicked();

button1.myParameter = "first";


button2.onClick = testclicked();

button2.myParameter = "second";



function testclicked()

{

    alert("you clicked the " + this.myParameter + "

...
Translate
Participant ,
May 30, 2013 May 30, 2013

You can't add an argument to a callback.

What are you trying to do exactly? The problem is that I don't understand what the "parameter" variable should be.

If you need to use a specific variable inside your callback you could just add it to your button variables, I haven't tried but wouldn't this work?


button1.onClick = testclicked();

button1.myParameter = "first";


button2.onClick = testclicked();

button2.myParameter = "second";



function testclicked()

{

    alert("you clicked the " + this.myParameter + " button, well done! \nwant a medal?");

}

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
Community Beginner ,
May 30, 2013 May 30, 2013

perfect, that's exactly what I needed!

I didn't knew I could add variables like that in my callbacks (first time with java/Extendscript)

Thank you very Much Maxweel!

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 31, 2013 May 31, 2013

Just to make sure: in the callback, the variable this represents the object (button1 or button2), not the callback function. And you can add any property to your objects (just make sure you're not trying to overwrite one that already exists): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

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 ,
Jun 04, 2013 Jun 04, 2013

Here I am using an anonymous function in a callback allowing me to pass another function as an argument.

bStack.onClick=    function(){app.beginUndoGroup("Group- Stacking...");    groupStack(shiftState (),this);      app.endUndoGroup(); }

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 ,
Jun 04, 2013 Jun 04, 2013

I'm not sure why you'd need an anonymous function to do that. Wouldn't you get the exact same result doing this?

function onClickFunction ()

{

  app.beginUndoGroup("Group- Stacking...");

  groupStack(shiftState (),this);

  app.endUndoGroup();

}

bStack.onClick =  onClickFunction;


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 ,
Jun 04, 2013 Jun 04, 2013

In the anonymous function it allows me to see the arguments clearly that are being passed into the callback (surrounded by the anonymous function).

In this case detecting if the Shift key was held down.

Also 'this' refers to the button itself, which now is no longer the case (As far as I know).

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 ,
Jun 04, 2013 Jun 04, 2013

I suppose it's just visual, I can see exactly what I'm passing in where I am writing the callback.

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 ,
Jun 04, 2013 Jun 04, 2013

No, in the callback I wrote this is also the button, see the post right above yours.

Since he was asking for a way of having a callback function defined only once and use it for all his buttons, an anonymous function wouldn't be best here IMO since you'll have to re-write its content for each button. Then again the function is pretty small.

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 ,
Jun 05, 2013 Jun 05, 2013
LATEST

Ok yes, I see that now, 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