Skip to main content
klemango
Inspiring
November 12, 2012
Answered

Calling functions from UI palette

  • November 12, 2012
  • 2 replies
  • 24647 views

Hello,

From what I understand, you can't call complex functions from a scripted UI palette, only from "dialog" windows (which are usless in my case) because I need the persistance of a palette.

I have a series of scripts in use now that are  accessed from the usual "file-scripts folder" within Illustrator itself (about 27 of them).  I want to script a palette window so users can acces them without the usual mouse clicks down the menu system.

I'm able to call a simple "Hello World" type function using the "onClick()" method from a button of a scripted palette window, but I cannot use my regular working scripts inside a function that's called from the button.  In X-Code, I was able to write my scripts individually, then just add them to a main floating window via a separate function when they were completed.  But, I'm finding Javascript a little more tricky.

If I copy my other's script code into the function in the palette window script, it runs, but it runs before I click the button! --  After in runs, then the palette window is displayed(?).  If I try to use the execute() method with "onClick", the script just opens in the SDK, it will not and does run in Illustrator.

I take both of these as clear indications that I have no clue what I'm doing (or that I'm trying to do the impossible).

I did find someone with a similar problem, but they were scripting After Effects and were offered this solution:

system.callSystem ('afterfx -r "/C/Program Files/Adobe/Adobe After Effects CS3/Support Files/Scripts/GlobalVars.jsx"');

Is there anything I can do in Illustrator that will allow me to call (or execute) my other scripts/functions and have them execute within Illustrator?

Thanks for any and all help!

This topic has been closed for replies.
Correct answer klemango

Okay, here we go.  I've used your example Carlos to contiuously add lines to the code and check each one as I go.  Things were going pretty well until I got to another function call and with the "else" statement.  I found that if I didn't have a semi-colon after the "else" statement, it wouldn't execute when there wasn't anything selected on the artboard.  When I do have something selected, the second function is called as expected and the "Hello" message is shown, but then the "else" statement also executes, telling the user to select the art.

#target illustrator;

#targetengine main;

var win = new Window('palette', 'Copy Objects');

var btnSelect = win.add('button', undefined, 'Copy');

btnSelect.onClick = function(){

   

    var bt = new BridgeTalk;

    bt.target = "illustrator";

   

    var script =

        "var thisDoc = app.activeDocument;" +

        "var artSel = thisDoc.selection;"+

        "var selLen = artSel.length;"+

       

        "brown = new CMYKColor();"+

        "brown.black = 50;"+

        "brown.yellow = 100;"+

        "brown.magenta = 67.065;"+

        "brown.cyan =0;"+

       

        "black = new CMYKColor();"+

        "black.black = 100;"+

        "black.yellow = 0;"+

        "black.magenta = 0;"+

        "black.cyan = 0;"+

       

        "if (thisDoc.selection.length >0){"+

       

                "copy();"+

                "paste();"+

               

                "thisDoc.rulerOrigin = [0,0]; "+

                "thisDoc.pageOrigin = [0,0];"+

                "var pageWidth = thisDoc.width/2;"+

                "var pageHeight = thisDoc.height/2;"+

                          

                "var newLayer = thisDoc.layers.add();" +

                "newLayer.name = \"Die Layer\";"+

                   

                "var artGroup = thisDoc.groupItems.add();"+

                "artGroup.name = \"Art Group Build\";"+

               

                "for(i=0;i<selLen;i++)"+

                "artSel.moveToEnd(artGroup);"+

               

                "var artGroupWidth = artGroup.width;"+

                "var artGroupHeight = artGroup.height;"+

               

                "drawBasicDie(brown,black);}"+

      

        "else;"+

            "alert(\"Please select the artwork\");"+

       

"function drawBasicDie(brown,black){"+

   

       "alert(\"Hello\");}"

       

       

         bt.body = script;

    bt.send();

       }// end function

   win.center();

win.show();


Well, here it is!!

I found this link:

http://www.davidebarranca.com/2012/11/scriptui-bridgetalk-persistent-window-examples/

(If you stumble on this by chance, thank you davide barranca!)

Now, I can make changes to the scripts in just one location and everyone will get the updates once they restart their window. 

Works like a charm and is just what I was needing!  Thank you all again for your help!

#target illustrator

var scriptToLoad = new File("S:/NEW SCRIPTS/JAVASCRIPTS IN USE NOW/02) Make/01b) Shape P - 16 UP.jsx");

var win = new Window('palette', 'Copy Objects');

var btnSelect = win.add('button', undefined, 'Copy');

btnSelect.onClick = function(){

   

   scriptToLoad.open("r");

       

   var bt = new BridgeTalk;

   bt.target = "illustrator";

       

   var script = scriptToLoad.read();

   scriptToLoad.close();

            

   bt.body = script;

   bt.send();

}// end function

win.center();

win.show();

2 replies

Inspiring
November 13, 2012

klemango wrote:

I have a series of scripts in use now that are  accessed from the usual "file-scripts folder" within Illustrator itself (about 27 of them).  I want to script a palette window so users can acces them without the usual mouse clicks down the menu system.

Not sure if I am on the right track in interpreting your request, but would Script Bay offer any viable solutions for your needs? I am however not sure if it works from a network drive/server (not sure why it wouldn't as its just pointers to folders it seems). Anyway if I am way off base then I apologize, otherwise maybe it will be helpful in some way as a more direct way for handling scripts if thats indeed what your after?

klemango
klemangoAuthor
Inspiring
November 13, 2012

Yes, it does help.  And, several of the artists at work use it all day.  They group several of the scripts into specific folders and just double click the folder.  The scripts inside that folder will then run in order.  It's a huge time saver!

That's exactly what got me interested in learing about scripting the UI.  When there was just a few scripts, it wasn't such a big problem.  But, as their numbers grew, I began looking for a long-term solution to funneling down through folders of scripts in the Illustrator File menu.  We use a two monitor set-up at work and it would be great to having a floating palette just sitting on one of them waiting for commands.

Because UI scripting is new to me, can anyone offer any real good resources to learn about scripting Bridgetalk and/or the UI?  I did download Mark's suggestion to read the UI document by Peter Kahrel and that was my first look at the UI code.  Great explanations and progressive code-flow from the beginning.  It really helped get me hooked into learning more.

As usual, the Tools Guide, like all other Adobe reference materials, is really only useful once you know somewhat how to code.  If you're a beginner, it might as well be written in Mandarin Chinese!  And, as others have said, the examples given are few.  Tthey never seem to have an example for what I'm trying to accomplish.  If I EVER master all the printing paramaters, I will throw a party and EVERYONE is invited.

Now it's time for more meat and potatoes behind the scenes stuff.

If anyone could offer any resources that would help, I'll check them all out.

And, as always, thank you for your time and your willingness to help.  From what I've already learned in these forums about Applescript and now JS, I wonder what that knowledge would've cost me in a classroom? 

Klemango

klemango
klemangoAuthor
Inspiring
November 13, 2012

Ah, I should've taken the time to actually READ the info in the tools guide first, eh?

CarlosCanto
Community Expert
Community Expert
November 12, 2012

the feature has always been broken, and for the longest I thought it was impossible to overcome, we finally found out a way to make it work, look here for an interesting reading about the topic and a script by Moluapple

http://forums.adobe.com/message/4402921#4402921

and here another script by me

http://forums.adobe.com/message/4820011#4820011

klemango
klemangoAuthor
Inspiring
November 12, 2012

Thanks!  I'll check it out.  I'm not sure I'll understand that much depth into coding the UI, but I'll definitely give it a shot!

One question before I dive into it so I don't have to ask later.  Does/do those method(s) allow you to load external scripts, say from a server, so you could make one change to the script and have it propogate down to all users?

Thanks.

CarlosCanto
Community Expert
Community Expert
November 12, 2012

kemango, your existing functions and scripts will certainly not work, you'll have tweak them a little bit to make them BridgeTalk ready.