20 Replies Latest reply: Nov 11, 2009 3:12 PM by mazmax RSS

    JSX to AppleScript

    mazmax Community Member

      Hello, I was wondering how to have a jsx PS script launch faster by creating an AppleScript application to put in the dock or maybe just something in Automator.

       

      Any ideas?

        • 1. Re: JSX to AppleScript
          c.pfaffenbichler Community Member

          I avoid AppleScript, so I can’t help You there.

           

          But if it’s just about avoiding having to scroll through the menus to trigger a Script, one can assign the Script a Keyboard Shortcut or record it into an Action.

          Or in CS4 put Scripts into Configurator Panels.

          • 2. Re: JSX to AppleScript
            Muppet Mark-QAl63s Community Member

            For an application to put in your dock you could do something like this very basic example. You just have AppleScript perform your JavaScript text return something when done. This should be saved from 'Script Editor' as 'Application' from the file formats (do NOT use the run only option as this means that the script can't be opened and edited a code protection thing)

             

            set JavaScript to "myTest();

            function myTest() {

            var docRef = app.activeDocument;

            var docWidth = docRef.width;

            var docHeight = docRef.height;

            var docSize = docWidth + ' x ' + docHeight

            return docSize

            }"

             

            tell application "Adobe Photoshop CS2"

            activate

            do javascript JavaScript ¬

            show debugger on runtime error

            set x to the result

            display dialog x giving up after 2

            end tell


            • 3. Re: JSX to AppleScript
              mazmax Community Member

              Hmmm can't understand what this would do

               

              Would it launch the jsx file into PS or is it possilbe to have an all-inclusive application that runs the code as well?

               

              Thanks

              • 4. Re: JSX to AppleScript
                mazmax Community Member

                Oh, you mean replace the content of the myTest function with all the jsx code? sry

                • 5. Re: JSX to AppleScript
                  Muppet Mark-QAl63s Community Member

                  In the above is a very basic piece of JavaScript which is nothing more than a string of text a far as AppleScript is concerned. This string is then played out by Photoshop as a script. It should return a value to AppleScript to display a dialog about the active documents height & width. The JavaScript can perform a whole process of commands if you want it to. All you are doing here is wrapping a JavaScript inside of an AppleScript application. If you save the above out to app then you can put it in the doc and it will run when clicked. (all-inclusive)

                  • 6. Re: JSX to AppleScript
                    Muppet Mark-QAl63s Community Member

                    Yes the JavaScript code can just sit inside of the AppleScript as a text variable in this case 'JavaScript' does that make any sense?

                    • 7. Re: JSX to AppleScript
                      Michael L Hale Community Member

                      Mark, I'm trying to follow this not knowing anything about AppleScript.

                       

                      It' looks like you are calling the js function, not the script( although in this case the entire js script is the function ). That is to say if I run that js scirpt in ESTK there would be no result as the js script does not call the function myTest(). For example if the script was just 'alert("Hello World");' could that be called from AppleScript?

                       

                      Which leads to my next question. Must the js return something?

                       

                      Both of those questions set aside the fact that it might be possible to do with AppleScript alone.

                       

                      Mike

                      • 8. Re: JSX to AppleScript
                        mazmax Community Member

                        I tried replacing my jsx code inside either the " " or the function curly brackets but it's returning errors like it's not recognizing the syntax.

                         

                        It stopped at a . for instance

                        • 9. Re: JSX to AppleScript
                          Muppet Mark-QAl63s Community Member

                          Mike the above snippet of JavaScript returns the document size '183.499998 mm x 133.749999 mm' to the JavaScript Console for me when run in ESTK. There is a function call there its just before the function itself. JavaScript need NOT return anything back to AppleScript although I would return something just so that I know it was successful. The normal result when performing JavaScript is that of 'undefined'. If I save the following to '.jsx' on my desktop.

                           

                          #target photoshop

                           

                          app.bringToFront();

                           

                          alert("Hello World");

                           

                          Then I can run this script as an external file too it's just read-in as text. Like this

                           

                          set JavaScript to choose file

                          --

                          tell application "Adobe Photoshop CS2"

                          activate

                          do javascript JavaScript show debugger on runtime error

                          end tell

                           

                          There is also the option to pass arguments too

                          • 10. Re: JSX to AppleScript
                            mazmax Community Member

                            Hmmm so any idea how to write the AppleScript?

                             

                            I tried pasting my code here

                             

                             

                            set JavaScript to "

                            <CODE PASTED HERE>

                            }"

                             

                            tell application "Adobe Photoshop CS4"

                            activate

                            do javascript JavaScript ¬

                            show debugger on runtime error

                            end tell

                             

                             

                             

                            or even

                             

                             

                             

                            set JavaScript to "myTest();

                            function myTest() {

                             

                            <CODE PASTED HERE>

                             

                            }"

                             

                            tell application "Adobe Photoshop CS4"

                            activate

                            do javascript JavaScript ¬

                            show debugger on runtime error

                            end tell

                             

                             

                             

                            But I get errors like these:

                             

                             

                            Syntax Error

                            A “,” can’t go after this “"”.

                             

                             

                            Syntax Error

                            A “"” can’t go after this “"”.

                             

                             

                            Syntax Error

                            A identifier can’t go after this “"”.

                            • 11. Re: JSX to AppleScript
                              MarkWalsh Community Member

                              Any quotation marks in your javascript will need to be escaped (or use single quotes for strings). Otherwise, Applescript will assume that quotation mark to be the end of the string you are setting into your variable, and will try to compile the rest of the Javascript code as if it were Applescript code.

                              • 12. Re: JSX to AppleScript
                                mazmax Community Member

                                Thanks Mark, what do you mean by "escaped" ?

                                 

                                I replaced all " with '   but it's giving

                                 

                                Syntax Error

                                Expected “"” but found unknown token.

                                • 13. Re: JSX to AppleScript
                                  mazmax Community Member

                                  Hmm still haven't sorted it out... any ideas?

                                  • 14. Re: JSX to AppleScript
                                    Muppet Mark-QAl63s Community Member

                                    Any chance of posting the .jsx file's code up here or e-mail it so we can see what we are dealing with. Did you also try the option of just running the file as an external file?

                                    • 15. Re: JSX to AppleScript
                                      Muppet Mark-QAl63s Community Member

                                      As you are looking at saving your script as an .app so that it can be added to the dock, finder window sidebar or toolbar you also have the following option that you may wish to try out. This way you should NOT need edit your JavaScript at all if it works just fine when executed.

                                       

                                      set JavaScript to (path to me as Unicode text) & "JS_Test.jsx" as alias

                                      --

                                      tell application "Adobe Photoshop CS2"

                                      activate

                                      do javascript JavaScript show debugger on runtime error

                                      end tell

                                       

                                      Here I just saved the above out as an application bundle. Once this is done control click on it to show package contents and pop your saved .jsx file inside the package. You will need to edit the name of the string in the first line to match the file in which you are placing.

                                      • 16. Re: JSX to AppleScript
                                        mazmax Community Member

                                        Oh that's cool, it works

                                         

                                        Any way to protect the app so the jsx is unaccessible/locked/password-protected?

                                        • 17. Re: JSX to AppleScript
                                          xbytor2 Community Member

                                          You can convert it to a binary format (jsxbin), but that just adds more complexity whether or not AS is involved.

                                          • 18. Re: JSX to AppleScript
                                            Muppet Mark-QAl63s Community Member

                                            As far I know the AppleScript 'main.scpt' can be protected by choosing the 'run only' option when saving the application bundle. Remember to ONLY check this box with a COPY of your script or you will NOT be able to edit later. I don't think that you can protect the 'resources' themselves but you could look into that. You can hide them by using shell to put them in your package but that does NOT offer any level of security just keeps some of the curious away. Im NOT familiar with X's solution but Im sure it would be the best way to go.

                                            • 19. Re: JSX to AppleScript
                                              mazmax Community Member

                                              Hmmm that used to work but is now giving

                                               

                                              Expected end of line, etc. but found identifier.

                                               

                                              and highlights the word javascript on 5th line

                                               

                                              can't find a reason why?

                                              • 20. Re: JSX to AppleScript
                                                mazmax Community Member

                                                How can I use something like

                                                 

                                                tell application "Adobe Photoshop CS3" or ""Adobe Photoshop CS4"

                                                 

                                                depending on which version the user has?