1 2 Previous Next 61 Replies Latest reply: Jan 19, 2013 12:16 AM by KennethWebb RSS

    Calling functions from UI palette

    klemango Community Member

      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!

        • 1. Re: Calling functions from UI palette
          CarlosCanto MVP

          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

          • 2. Re: Calling functions from UI palette
            klemango Community Member

            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.

            • 3. Re: Calling functions from UI palette
              Muppet Mark Community Member

              Broken… Im not sure on that… Dialog is modal so it blocks out focus until cancel or ok… The problem with palette is getting current document/app status and the only way is through bridgetalk messaging… In your case the called script should be able to do this… Also look at the bridgetalk class cross DOM functions like run script… These should read and bridgetalk for you but I have heard of issues with these…

              • 4. Re: Calling functions from UI palette
                klemango Community Member

                Will do...  Thanks for the help and the pointers in the right direction.  I can feel the skullcramps already.

                • 5. Re: Calling functions from UI palette
                  CarlosCanto MVP

                  The problem with palette is getting current document/app status

                  that sounds like broken ,  in the sence that it can not communicate with the application directly, it has to BridgeTalk to itself.

                  • 6. Re: Calling functions from UI palette
                    CarlosCanto MVP

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

                    • 7. Re: Calling functions from UI palette
                      Muppet Mark Community Member

                      In PS it won't even persist… Now that be not playing ball at all…

                      • 8. Re: Calling functions from UI palette
                        CarlosCanto MVP

                        not at all Mark, Adobe doesn't play ball, from the guide:

                         

                        Floating palette: Also called modeless dialog, allows activity in other application windows. (Adobe

                        Photoshop® does not support script creation of palette windows.)

                        • 9. Re: Calling functions from UI palette
                          klemango Community Member

                          Thanks for the info, gentlemen....

                           

                          I'm sure I'll be back for more help.  Not sure if it's worth all the trouble when they are perfectly content with using the current method of running the scripts.  It's just I have to know how this crazy s**t works.  When I finally do figure it out, I'm sure it will be time for the next version to come out and things will change.  It's a shame that such a powerful tool (I write scripts for workflow automation in a production art department) doesn't get more attention where needed.  But, I guess that's why they pay us, right?

                          • 10. Re: Calling functions from UI palette
                            W_J_T Community Member

                            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?

                            • 11. Re: Calling functions from UI palette
                              klemango Community Member

                              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

                              • 12. Re: Calling functions from UI palette
                                klemango Community Member

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

                                • 13. Re: Calling functions from UI palette
                                  W_J_T Community Member

                                  Ok, I see. Well I wont try and talk above my current comprehension (still learning myself) and your already in good hands with Muppet Mark and CarlosCanto.

                                   

                                  klemango wrote:

                                   

                                  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.

                                   

                                  That sounds weird that it would execute before the onClick is called? I will say that I have scripted window palettes using onClick() which then reference an #include to another JS file to execute and it works fine, I have also nested other #include's inside these files being called as well. So I guess you could build a palette window which executes scripts on user interaction (click a button) and even daisy chains scripts together using #include's? But then thats why I suggested ScriptBay as it already eloquently does this. How one gets to building something like ScriptBay is beyond me however, it's really impressive by Harbs.

                                   

                                  klemango wrote:

                                   

                                  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.

                                  Hmmm... that sounds perfect for Scriptbay and what it was intended for.

                                   

                                  ----

                                   

                                  I gotta be honest though I might not be accurately understanding the full scope of what your trying to accomplish (plus obviously your trying to just learn as well), and if your wanting to get into BridgeTalk then thats beyond myself at this point. So again I refer you back to Muppet Mark, CarlosCanto and others for sound advice. ;-)

                                   

                                  Sorry if I added any confusion and noise to your thread, I just found it to be an interesting topic. I am probably approaching it from a too basic level however, sorry if I misunderstood.

                                  • 14. Re: Calling functions from UI palette
                                    klemango Community Member

                                    No, not at all!  After reading part of the tools guide, I'm wondering if I can just include the #include statement inside the function I'm calling, then execute the script from outside the window?  Something like this?:

                                     

                                    var bt = new BridgeTalk;

                                    bt.target = "illustrator";

                                    fileobj.execute()

                                     

                                    ***where fileobj is the external script.  However, when using execute() from within the palette, the scripts native program, which in this case is the ESToolkit, is the one that opens the program.  I need to learn how to tell Illustrator itself to run the script as it would from the File-Scripts menu.  That is, if it's even possible to do that.

                                     

                                    What I'm trying to do in a nutshell is run separate, already written scripts (they are run from the "File-Scripts" menu in illustrator now) from a palette window by just selecting a button within the palette.  I was able to do this with Applescripts by just including the code within the X-Code script itself.  Pretty simple when I look back on it now......

                                     

                                    But, from what I understand, because the Adobe Suite has individual programs, with individual scripting capibilites, I've somehow got to not only tell the palette button what I want it to do, but give the receiving application what it needs to do it's job.  In this case, I believe that means I need to send illustrator the script it needs to run, or somehow reference a location to a script that it can load, then run.  Once it get it what it needs, Illustrator will (or should) then run in its native environment and fulfill the task the script tells it to perform.

                                     

                                    My problem lies in the middle between the palette and Illustrator.  And, if I'm digesting what Carlos and Mark are telling me correctly, this means I need to use the Bridgetalk API to perform this function.  That's what I'm trying to get a grasp on now.  If I'm correct in my thinking, the palette runs in its own persistance engine and uses the API to  send requests and replies between different/separate applications.  He's the middleman who needs to be given the properly formatted request to give to any particular app.  I get the theory, I just don't know the proper procedure and/or information I need to include inside the palette code to get it to do what I need.  I'll keep reading!

                                     

                                    Thanks for all your posts!  Keep 'em coming! 

                                    • 15. Re: Calling functions from UI palette
                                      klemango Community Member

                                      Also, just a note:  I don't need to worry about any return values to deal with.  Each script in Illustrator performs a specific function unto itself.  They stand alone, so to speak.  So, I just need a way to invoke them.

                                       

                                      Thanks.

                                      • 16. Re: Calling functions from UI palette
                                        Larry G. Schneider MVP

                                        To force Illustrator as the operative app, put #target illustrator at the start of the script.

                                        • 17. Re: Calling functions from UI palette
                                          klemango Community Member

                                          Hi Larry, thanks for the info.  In this case, does #target illustrator need to be at the beginning of the entire script?  Or, within and at the beginning of the function code?

                                          Also, I can't seem to get this to work:

                                           

                                          illustrator.executeScript(scriptfile);

                                           

                                          And, I can't seem to find a working example, either.

                                           

                                          You guys should get together and write a book.  One that has real world problems and examples in it.  Downloadable PDF.  I'd buy it.

                                          • 18. Re: Calling functions from UI palette
                                            CarlosCanto MVP

                                            kemango, I like what you're doing, we've told you since the beginning regular calls don't work, and still you're trying to find out how to make it work. I did the exact same thing, I thought "hmm...maybe there's something so simple that everyone before me just missed", but no, I couldn't find anything...but at the end, it's all good, you'll learn a lot by trying a bunch of different things, and who knows, you might find a way.

                                             

                                            in the mean time, look at my code in the selectLayers script, that's as basic as it gets, it does not return values, it just sends a request to select items in a layer.

                                             

                                            ...ok that code is not the most basic, let me make you a similar sample with a basic call..

                                            • 19. Re: Calling functions from UI palette
                                              CarlosCanto MVP

                                              here a simple script to select the top object

                                               

                                              #target Illustrator
                                              #targetengine main
                                              
                                              
                                              var win = new Window('palette', 'Select Top Object');
                                              var btnSelect = win.add('button', undefined, 'Select');
                                              
                                              
                                              btnSelect.onClick = function(){
                                                  var bt = new BridgeTalk;
                                                  bt.target = "illustrator"; 
                                                      var script = "\n" +
                                                      "app.activeDocument.pageItems[0].selected = true;\n" +
                                                      "app.refresh();\n" 
                                              
                                              
                                                  bt.body = script;
                                                  bt.send(); 
                                              }
                                              
                                              
                                              win.center();
                                              win.show();
                                              
                                              • 20. Re: Calling functions from UI palette
                                                klemango Community Member

                                                Thanks Carlos.  I think I'm this way because I've never had any formal training and my results have always come by the way of trial/error.  But, you're right, I have to exhaust myself trying 1,000 ways to do something before it clicks.  It's kinda like putting together a 5,000 piece puzzle that's a single solid color.  Torture to say the least, but worth it in the end.  When I read your statement calling them "regular" calls, that got me thinking about this:

                                                 

                                                (I've read yours and Moluapple's code and I think I need to correct myself on something....)

                                                 

                                                I was under the impression the palette itself was a completely separate entity.  Living on it's own within it's own engine, generated by the Toolkit. Acting as a sort of a traffic cop between apps. Do I now understand that when you #target an app that the window is generated within it's own engine, within and inclusive to that app?

                                                 

                                                If it is within the app, is my thinking that the palette is not a third party, but part of the app correct?  And, as such, does it now have a direct relationship with the app?  If so, are the function calls now considered a somewhat "local" ,but not in the same space, call in relation to the app?  Even though it's still an API?   I'm probably overthinking this, but it helps me to understand the "big" picture.

                                                • 21. Re: Calling functions from UI palette
                                                  klemango Community Member

                                                  Sorry, was typing that while you replied.  I see now the #target and engine are IN the function.  That was one of my earlier questions and where my though processes were going.  I thought that since functions were local code, that I might have to have the #include inside the function.  But, I didn't know since I've never used #target before.  I'm going to give this a try.

                                                   

                                                  Now, is there a way to load a script from an external library and run it (to keep the main code to a minimum)?

                                                  • 22. Re: Calling functions from UI palette
                                                    CarlosCanto MVP

                                                    #target Illustrator - if you run from the ESTK or double click on the file, #target tells the appropriate app to run the file, ignored if you run the script directly from Illustrator

                                                     

                                                    #targetengine - don't have a proper technical definition but without it a 'palette' window won't persist.

                                                     

                                                    as you see in the example, the script needs to be serialized as a text string and sent to illustrator. Illustrator receives the text string, puts it back into code (probably using eval() internally) and executed.

                                                     

                                                    Now, is there a way to load a script from an external library and run it (to keep the main code to a minimum)?

                                                    I would say yes, as long as it gets properly serialized before it gets sent to illustrator.

                                                    • 23. Re: Calling functions from UI palette
                                                      CarlosCanto MVP

                                                      ....as you can see in my selectLayers script I have a separate function that gets sent to illustrator, I think that function could live in a separate file and get loaded with #include

                                                      • 24. Re: Calling functions from UI palette
                                                        klemango Community Member

                                                        That didn't make any sense, did it?  I was typing the above while thinking of to many things at once.  Please disregard the one just above.

                                                         

                                                        By looking at your code, I see that the statements are stored in the variable script, which is assigned to the body, then sent.  But, I'm also seeing each statement encased in quotes and with + in between.  These then, are what's being sent to illustrator to execute?

                                                         

                                                        Is that what is needed for each line of the original script?

                                                        • 25. Re: Calling functions from UI palette
                                                          klemango Community Member

                                                          Wow, this is starting to make sense now.  Do you know if each line is sent to illustrator in sequence or as a package deal?  The reason I ask, and you may have already answered this, is if you have  code such as yours (single statements, in order) and your code has other functions below the main body of the script, will those functions be called or will they fail?

                                                          • 26. Re: Calling functions from UI palette
                                                            CarlosCanto MVP

                                                            don't worry,

                                                             

                                                            the + is the concatenation operand, "a" + "b" = "ab";

                                                             

                                                            I just added them at the end of each line, to make the code somewhat readable, same as this

                                                             

                                                            "app.activeDocument.pageItems[0].selected = true; app.refresh();"

                                                            • 27. Re: Calling functions from UI palette
                                                              CarlosCanto MVP

                                                              the message is sent as one string, as long as it may be, functions and everything in between, and just like a regular script, functions will get executed as they are called.

                                                              • 28. Re: Calling functions from UI palette
                                                                klemango Community Member

                                                                Yep, didn't know if that was something specific to the call or not.

                                                                • 29. Re: Calling functions from UI palette
                                                                  klemango Community Member

                                                                  So, to serialize:   Just a beginning and ending quote for the entire script?

                                                                  • 30. Re: Calling functions from UI palette
                                                                    klemango Community Member

                                                                    I give up!  If I change that single statement in your script Carlos, nothing happens.  I can't even add a new layer.  If anyone get get this simple script to work, I'll modify it to work for the others.  I'll check back in after they let me out of my rubber room:

                                                                     

                                                                     

                                                                    var thisDoc = app.activeDocument;

                                                                    var tempSel = thisDoc.selection;

                                                                     

                                                                        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 artSel = thisDoc.selection;

                                                                        var selLen = artSel.length;

                                                                     

                                                                     

                                                                        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[i].moveToEnd(artGroup);

                                                                     

                                                                        var artGroupWidth = artGroup.width;

                                                                        var artGroupHeight = artGroup.height;

                                                                       

                                                                        drawBasicDie(brown,black);

                                                                        

                                                                    }//end IF

                                                                     

                                                                    else

                                                                        alert("Please select the artwork.");

                                                                       

                                                                     

                                                                     

                                                                     

                                                                     

                                                                    function drawBasicDie(brown,black)

                                                                    {

                                                                       var artWidth = artGroup.width/2;

                                                                        var artHeight = artGroup.height/2;

                                                                        artGroup.position = [0-artWidth,0+artHeight];

                                                                     

                                                                     

                                                                        var rec_group1 = thisDoc.groupItems.add();

                                                                            rec_group1.name = "1";

                                                                            artGroup.moveToBeginning(rec_group1);

                                                                     

                                                                     

                                                                        var DieStar = (artHeight + 10);

                                                                     

                                                                        var star1 = rec_group1.pathItems.star(-76.5, DieStar,1,4,11);

                                                                            star1.filled =true;

                                                                            star1.stroked =false;

                                                                             star1.fillColor =black;

                                                                       

                                                                        var star2 = rec_group1.pathItems.star(-76.5, -DieStar,1,4,11);

                                                                            star2.filled = true;

                                                                            star2.stroked =false;

                                                                            star2.fillColor = black;

                                                                       

                                                                        var star3 = rec_group1.pathItems.star(69, DieStar,1,4,11);

                                                                            star3.filled =true;

                                                                            star3.stroked =false;

                                                                             star3.fillColor = black;

                                                                       

                                                                        var star4 = rec_group1.pathItems.star(69,-DieStar,1,4,11);

                                                                            star4.filled = true;

                                                                            star4.stroked =false;

                                                                            star4.fillColor = black;

                                                                       

                                                                        var DieStar = (artGroup.height/2+22);

                                                                        var DieCord1 = (rec_group1.width/2+10);

                                                                        var DieCord2 = (rec_group1.height/2+10);

                                                                     

                                                                        rec_group1.selected = true;

                                                                     

                                                                        x = app.activeDocument.selection[0].position[0];

                                                                        y = app.activeDocument.selection[0].position[1];

                                                                     

                                                                        var DieBoxW= (rec_group1.width/2);

                                                                        var DieBoxH = (rec_group1.height/2);

                                                                     

                                                                        var LeftCord = (DieBoxW-(DieBoxW*2)-15);

                                                                        var RtCord = (DieBoxH + 13);

                                                                     

                                                                        var BoxW = (rec_group1.width+25);

                                                                        var BoxH = (rec_group1.height+25);

                                                                     

                                                                        var rect_1 = rec_group1.pathItems.rectangle (RtCord, LeftCord,BoxW,BoxH);

                                                                            rect_1.filled = false;

                                                                            rect_1.stroked = true;

                                                                            rect_1.strokeColor = brown;

                                                                     

                                                                        var filename = activeDocument.fullName;

                                                                        var filename = filename.name;

                                                                        var filename = filename.substr(0,7);

                                                                     

                                                                        var die_num = thisDoc.textFrames.add();

                                                                            die_num.contents = filename + ".01";

                                                                            die_num.moveToBeginning(rec_group1);

                                                                       

                                                                        var myParagraph = die_num.paragraphs[0];

                                                                            myParagraph.characterAttributes.size = 12;

                                                                            myParagraph.characterAttributes.textFont=app.textFonts.getByName("MyriadPro-Regular");

                                                                            myParagraph.characterAttributes.fillColor = brown;

                                                                     

                                                                        die_num.top = rec_group1.height/2-5;

                                                                        die_num.left = -(die_num.width/2);

                                                                        die_num.filled = true;

                                                                     

                                                                        rec_group1.selected = true;

                                                                        rec_group1.position=[(pageWidth-(rec_group1.width/2)),(pageHeight+(rec_group1.height/2))] ;

                                                                      

                                                                    } //  end

                                                                    • 31. Re: Calling functions from UI palette
                                                                      Larry G. Schneider MVP

                                                                      You mean like this

                                                                       

                                                                      Screen shot 2012-11-13 at 3.20.00 PM.png

                                                                       

                                                                      Corrected the two extra spaces and added the #target illustrator at the head of the script to get the ESTK to force it into Illustrator. In back is the original selection I made.

                                                                      • 32. Re: Calling functions from UI palette
                                                                        CarlosCanto MVP

                                                                        do baby steps...start by making a small script to add new a layer and reame that layer...show us your bridge talk code, we'll help you make it work, once you get the hang of it, you can go on to bigger things.

                                                                        • 33. Re: Calling functions from UI palette
                                                                          klemango Community Member

                                                                          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[i].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();

                                                                          • 34. Re: Calling functions from UI palette
                                                                            klemango Community Member

                                                                            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();

                                                                            • 35. Re: Calling functions from UI palette
                                                                              CarlosCanto MVP

                                                                              thanks for the link, it'll come handy, just for educational purposes here's your script, I ended up moving the function to the beginning, also the function is still a string, it needs to be converted back into a function with 'eval()'

                                                                               

                                                                              #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 =
                                                                                  "main();" +
                                                                              "function main() {" +
                                                                              
                                                                               "var drawBasicDie = function (textstring){"+
                                                                                  "alert('hola '+textstring);" +
                                                                                     "alert('Hello');" +
                                                                                "}" + 
                                                                              
                                                                                      "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[i].moveToEnd(artGroup);" +
                                                                                              "}"+
                                                                                              "var artGroupWidth = artGroup.width;"+
                                                                                              "var artGroupHeight = artGroup.height;"+
                                                                                              "var funct = eval(drawBasicDie);" +
                                                                                              "funct('Carlos');" +
                                                                              
                                                                                       "}" +
                                                                              
                                                                                      "else {" +
                                                                                          "alert('Please select the artwork');}" +
                                                                              
                                                                              
                                                                              
                                                                              "}"
                                                                              
                                                                              
                                                                                       bt.body = script;
                                                                              
                                                                                  bt.send();
                                                                              
                                                                                     }// end function
                                                                              
                                                                              
                                                                              win.center();
                                                                              
                                                                              win.show();
                                                                              
                                                                              • 36. Re: Calling functions from UI palette
                                                                                klemango Community Member

                                                                                Thanks for all your help, Carlos.  I really appreaciate your time and patience (you have no idea).  This BT thing is finally beginning to click and the Tools Guide for the UI section is finally starting to make sense.

                                                                                 

                                                                                I have one other question with regards to your script above (with a little back story...).  Way back in the early 90's (Pascal, anyone?) when I had my first programming class, we were taught to keep all functions below the function call.  I'm not sure if this was just the instructor's preference or mandatory, but I've always tried to keep the functions at the bottom of my scripts, and in the order (if possible) they were called.  I notice many scripts on the net now that have them as you do, at the top.  Is there a fundamental programming reason for this?  I'm just wondering if I need to change my flow now with JS, and if some of the problems I've had in the past were as result of my layout.

                                                                                 

                                                                                ** Thanks for correcting the "else" statement, too.  I see now that I was missing the {}'s around the statement.

                                                                                 

                                                                                Klemango

                                                                                • 37. Re: Calling functions from UI palette
                                                                                  CarlosCanto MVP

                                                                                  you're welcome

                                                                                  yeah, BT is a little bit obscure at first, once you get one or two scripts going, it starts to make sense.

                                                                                   

                                                                                  I don't have formal training, all by experimentation, I noticed when you call your function it returned 'undefined' when it is at the bottom, as you noticed this is not a problem if you run your script normally in the ESTK, I tried a couple of things and moving it to to top worked. I kind of remember that in C++ one has to 'declare' a function so it registers before it is called, otherwise the compiler doesn't know it exist.

                                                                                   

                                                                                  all that must be some weird BT implementation, you also noticed for (....) alert(...) does not work as it should, same with else alert(...), one liners must be enclosed in {}

                                                                                   

                                                                                  ...so, I keep my functions at the bottom too, as much as I'm allowed.

                                                                                  • 38. Re: Calling functions from UI palette
                                                                                    W_J_T Community Member

                                                                                    Great thread/work everyone, this has been an interesting and enlightening thread.

                                                                                     

                                                                                    I was curious are ScriptUI window palettes able to be saved as part of a workspace and thus dockable so with each start they will be part of the users workspace? If not then what is the best way to execute or initialize such scripts, as startup scripts? I am just trying to get my head around this as klemango mentioned a bunch of artists/designers using this and I was curious in such a case how it would then be implemented for use and deployment as part of their workflows?

                                                                                    • 39. Re: Calling functions from UI palette
                                                                                      CarlosCanto MVP

                                                                                      very interesting topic

                                                                                       

                                                                                      are ScriptUI window palettes able to be saved as part of a workspace and thus dockable so with each start they will be part of the users workspace?

                                                                                      No

                                                                                       

                                                                                      startup scripts?

                                                                                      I would say yes, but if you have several runing at the same time, I'm not sure how resource demanding that would be. The other option would be to start them up on demand as they're needed, just like other window-less scripts.

                                                                                      1 2 Previous Next