Copy link to clipboard
Copied
Hi friends
I´m having a trouble with an onClose event in a panel of type "palette". Let´s try to explain the problem.
Suppose I have this single construction:
var myDialog = new Window ("palette", "My dialog", undefined, {closeButton:false});
var bt = myDialog.add ("button", undefined, "Ok", {name:"ok"});
myDialog.onClose = function(){
//for example: app.activeDocument.layers.add();
};
myDialog.show();
If I change the window to type "dialog", everything looks preety good, but if I keep the dialog as "palette", then the button "Ok" does not close the dialog and, sure, the onClose does not work.
P.S: I need the onClose event because I want that closing code executed even if the user hit "ESC" instead of using "OK" button.
Any idea how to manage or an alternative to make the button and the onClose works??
Thank you very much
Best Regards
Gustavo.
it does not work because Palettes and Illustrator don't talk to each other, to call illustrator from a palette you MUST use BridgeTalk, search this forum, there are plenty of examples.
Copy link to clipboard
Copied
Hi Gustavo, you explicitly close the window when the ok button is pressed
var myDialog = new Window ("palette", "My dialog", undefined, {closeButton:false});
var bt = myDialog.add ("button", undefined, "Ok", {name:"ok"});
bt.onClick = function () {
myDialog.close();
}
myDialog.onClose = function(){
alert('onClose');
//for example: app.activeDocument.layers.add();
};
myDialog.show();
Copy link to clipboard
Copied
Hi Carlos, thank you as ever
Yes, after posting my first message I tried to declare the onClick for button and it worked for closing the dialog...but...the onClose event is not working here yet.
Yes..it shows the single alert, but if you insert a bigger code, for example, adding layers, using loops...then it does not execute...
I have no idea why. Do not know id it makes influence, but my dialog is invoked inside a function...at true in the middle of the function (theres already a code..then, the dialog is invoked)...
If you try to insert a big code in the onClose..does it work in your side?
Thank you very much
Gustavo
Copy link to clipboard
Copied
it does not work because Palettes and Illustrator don't talk to each other, to call illustrator from a palette you MUST use BridgeTalk, search this forum, there are plenty of examples.
Copy link to clipboard
Copied
Carlos
Thank you for the information! What a pity 😕 So...as an alternative:
Is there any way to have a code and this be executed only after a palette is close in Illustrator??
For example, if I write:
var myDialog = new Window ("palette", "My dialog", undefined, {closeButton:false});
var bt = myDialog.add ("button", undefined, "Ok", {name:"ok"});
bt.onClick = function(){
myDialog.close();
};
myDialog.show();
alert("here´s the code after the dialog");
...continue script....
Since it´s a palette, the alert and the other lines after the line "myDialog.show()" will be executed automatically as long as the panel is showed.
Would it be possible to insert a stop and allow this following portion of code only be executed if the panel is closed via the button?? (such a window of type dialog behavior)??
Gustavo.
Copy link to clipboard
Copied
hmm...I don't think you can do that,
check this script if you still need to get your palette going, BridgeTalk is a little complicated at first but once you get the hang of it, it is a nice addition to your skill set.
Copy link to clipboard
Copied
Humm..interesting, Carlos
So what you mean is...when you call the event (like onClose), instead of declaring the code inside the scope of the event, you just call a function that prepares a message and send to Illustrator to execute, right?
Copy link to clipboard
Copied
correct, basically BridgeTalk sends the whole script (as a string) to illustrator, illustrator reads this string, converts it to code and executes it.
Copy link to clipboard
Copied
CarlosCanto wrote:
… and executes it.
… unless a backslash in your script exists.
Copy link to clipboard
Copied
Hi Carlos
In the past I´ve tried to play with BridgeTalk..but I assume I need to study it again...Can you tell me if the concept is correct? In my example:
var myDialog = new Window ("palette", "My dialog", undefined, {closeButton:false});
var bt = myDialog.add ("button", undefined, "Ok", {name:"ok"});
bt.onClick = function(){
myDialog.close();
};
myDialog.onClose = function (){
var messenger = new BridgeTalk;
messenger.target = "illustrator";
var message = "app.activeDocument.activeLayer.remove()";
messenger.body = message;
messenger.send();
};
myDialog.show();
The idea is to remove the active layer...but it´s not working 😕 What Am I missing??
At true, I will need to pass in the message a function with 4 arguments....so it´s more complex than just removing the activeLayer.. but...for now..I wanna just know if the construction of BridgeTalk is correct. Can you review?
Thank you very much
Gustavo.
Copy link to clipboard
Copied
it's perfect!! what's not working in your system?
if you're running the script from the Illustrator menu then you need to include this at the top of your script
#target Illustrator
#targetengine main
Copy link to clipboard
Copied
Works well here also. OSX10.6.8, AICS5
Copy link to clipboard
Copied
Hi Carlos
Arrg...it was working right. The problem is the active layer was locked...so Illustrator was not removing because the lock.
Now...going to the real scenario of my script. I have:
1 variable that stores a specific layer (let´s call "myLayer");
1 array that stores pathItems (let´s call "myArray");
1 variable that stores a single number (myNumber).
And you have a function that processes these variables. Example:
function myFunction (arg1, arg2, arg3){
...
}
How could you include the call of this function in the body of the message...passing the correct arguments? myFunction (myLayer, myArray, myNumber)
Is it possible?
Thank you very much
Gustavo.
Copy link to clipboard
Copied
Thank you Larry for the check!
Copy link to clipboard
Copied
yes it is possible, check my script, I only have 1 argument, but you can add more
put all your arguments in an object
var arguments = {myLayer:myLayer, myArray:myArray, myNumber:myNumber};
....send your message and use arguments.toSource()
in your function only ask for 1 argument (the object with all your arguments)
myFunction (arguments) {
var obj = eval(arguments);
var myLayer = obj.myLayer;
var myArray = obj.myArray;
.
.
Copy link to clipboard
Copied
Hi Carlos
Ah, great to know!
I made this simple test (suppose you have a doc with multiple layers):
var myLayer = app.activeDocument.layers[0];
When I use:
alert(myLayer.toSouce());
It returns ({}) ...empty!
If I have a variable of kind number, string, boolean...or even an array of these kind, then onSource() is perfect..it returns correct. But for specific Illustrator objects, it returns empty. So what I´m thinking is:
Instead of passing variables in the BridgeTalk message, I´ll insert tags in the objects I process earlier. So...in the body of the message I pass a function (with no arguments) that first search for such objects and catch it again and store as variable...and after process normally these objects.
Do you think my concept is correct? Or I´m going to the harder and unecessary way hehehe
Gustavo.
Copy link to clipboard
Copied
var myLayer = app.activeDocument.layers[0];
alert(myLayer.toSouce());
It returns ({}) ...empty!
you can't read app.activeDocument.layers[0] from within your palette, remember there's no connection between your palette and illustrator, what we've been doing so far is one way communication from Palette to Illustrator. To read and use something back from illustrator into your palette, you need another BridgeTalk call that sends a script and returns a result (myLayer).
to read the active layer name for instance
var bt3 = new BridgeTalk;
bt3.target = "illustrator";
var msg3 = "\n" +
"var idoc = app.activeDocument;\n" +
"layerName = idoc.activeLayer.name;\n"
bt3.body = msg3;
bt3.onResult = function(resObj) {
alert(resObj.body)
}
bt3.send();
Copy link to clipboard
Copied
Thank you Carlos
I have just one more question...but I´ll start a new discussion since the subject gets out of the question in this discussion.
Again, as ever, thank you very much for the big support.
Gustavo.
Copy link to clipboard
Copied
Hey Carlos and Larry
My new script was finally published. It´s called Output Simulation and it was inspired by the Preview Mode of Adobe InDesign (the feature enabled when you press the W key when working on that application).
Can I ask you to know this script? Here´s the blog post about: http://nucleodoillustrator.com/2013/09/11/introducing-output-simulation-v1/
There´s a thank you for you in the acknowledges
Hope you enjoy it!
Best Regards and thank you very much for all the support and help (as ever).