7 Replies Latest reply on Mar 9, 2010 7:06 AM by Shonkyin

    [AS] doScript with arguments

    r_olsen Level 1

      Hi,

       

      I'm trying to execute a js script through applescript but can't get the syntax right.

      I'd like to parse var myFolder to do script. Any hints?

       

      Thanks

       

      // execute js file
      set myFile to choose file with prompt "Choose a script"
      set myFolder to choose folder with prompt "Choose a folder"
      display dialog "myFolder is " & myFolder
      tell application "Finder"
           set myFileExt to name extension of myFile
      end tell
      tell application "InDesignServer"
           if myFileExt is "jsx" then
                      --call this method with argument myFolder?
                do script myFile language javascript
           else
                     --call this method with argument myFolder?          
                     do script myFile language applescript language
           end if
      end tell

       

      // js
      // print arguments
      i = 0;
      while (i < withArguments.length) {
           app.consoleout(withArguments[i]);
           i++;
      }
      
        • 1. Re: [AS] doScript with arguments
          Shonkyin Level 3

          Here below is example for doscript with arguments. Hope it will help you.

           

          --DoScriptReturnValues.applescript

          --An InDesign CS4 AppleScript

          --

          --Shows how to get values back from a script run using

          --the do script command.

          main()

          on main()

               mySetup()

               mySnippet()

               myTeardown()

          end main

          on mySetup()

          end mySetup

          on mySnippet()

               tell application "Adobe InDesign CS4"

                    --<fragment>

                    set nameA to "ScriptArgumentA"

                    set nameB to "ScriptArgumentB"

                    set nAc to nameA & ": "

                    set nBc to nameB & ": "

                    --Create a string to be run as an AppleScript.

                    set p1 to "tell application \"Adobe InDesign CS4\"" & return

                    set p2 to "tell script args" & return

                    set p3 to "set value name \"ScriptArgumentA\" value "

                    set p4 to "\"This is the first AppleScript script argument value.\""

                    set p5 to "set value name \"ScriptArgumentB\" value "

                    set p6 to "\"This is the second AppleScript script argument value.\""

                    set p7 to "end tell" & return

                    set p8 to "end tell"

                    set myAppleScript to p1 & p2 & p3 & p4 & return & p5 & p6 & return & p7 & p8

                    --Run the AppleScript string.

                    do script myAppleScript language applescript language

                    --Retrieve the script argument values set by the script.

                    tell script args

                         set myScriptArgumentA to get value name nameA

                         set myScriptArgumentB to get value name nameB

                    end tell

                    --Display the script argument values in a dialog box.

                    display dialog nAc & myScriptArgumentA & return & nBc & myScriptArgumentB

                    --Create a string to be run as a JavaScript.

                    set p1 to "app.scriptArgs.setValue(\"ScriptArgumentA\", "

                    set p2 to "\"This is the first JavaScript script argument value.\");" & return

                    set p3 to "app.scriptArgs.setValue(\"ScriptArgumentB\", "

                    set p4 to "\"This is the second JavaScript script argument value.\");" & return

                    set myJavaScript to p1 & p2 & p3 & p4

                    --Run the JavaScript string.

                    do script myJavaScript language javascript

                    --Retrieve the script argument values set by the script.

                    tell script args

                         set myScriptArgumentA to get value name nameA

                         set myScriptArgumentB to get value name nameB

                    end tell

                    --Display the script argument values in a dialog box.

                    display dialog nAc & myScriptArgumentA & return & nBc & myScriptArgumentB

                    --</fragment>

               end tell

          end mySnippet

          on myTeardown()

          end myTeardown

           

           

          Shonky

          1 person found this helpful
          • 2. Re: [AS] doScript with arguments
            r_olsen Level 1

            Was this taken from the InDesign sample scripts? In that case I'm sorry for the ignorance

             

            Will give it another go - thanks!

             

            Rasmus

            • 3. Re: [AS] doScript with arguments
              r_olsen Level 1

              Hi again,

               

              This works:

               

              tell application "Adobe InDesign CS4"
                   set line1 to "var s = \"some string\";" & return
                   set line2 to "alert(s);"
              set myJavaScript to line1 & line2
              do script myJavaScript language javascript
              

               

              But this doesn't:

               

              tell application "Adobe InDesign CS4"
                   set line1 to "scriptArgs.setValue(\"arg1\",\"myValue\");" & return
                   set line2 to "var value = scriptArgs.getValue(\"arg1\");" & return
                   set line3 to "alert(value);"
              set myJavaScript to line1 & line2 & line3
              do script myJavaScript language javascript
              
              

               

              A little help?

               

              Thanks,

              Rasmus

              • 4. Re: [AS] doScript with arguments
                Shonkyin Level 3

                Try below one

                tell application "Adobe InDesign CS4"
                     set line1 to "app.scriptArgs.setValue(\"arg1\",\"myValue\");" & return
                     set line2 to "var value = app.scriptArgs.getValue(\"arg1\");" & return
                     set line3 to "alert(value);"
                set myJavaScript to line1 & line2 & line3
                do script myJavaScript language javascript
                end tell

                 

                 

                Shonky

                • 5. Re: [AS] doScript with arguments
                  r_olsen Level 1

                  That did it - thanks

                   

                  • 6. Re: [AS] doScript with arguments
                    Harbs. Level 6

                    Shonky,

                     

                    When you post other people's scripts, please write a note that you are doing such.

                     

                    Thanks,

                    Harbs

                    • 7. Re: [AS] doScript with arguments
                      Shonkyin Level 3

                      I'll do this Harbs.

                       

                      Thanks,

                      Shonky