Copy link to clipboard
Copied
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
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 + "
Copy link to clipboard
Copied
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?");
}
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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(); }
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
I suppose it's just visual, I can see exactly what I'm passing in where I am writing the callback.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Ok yes, I see that now, thanks.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now