7 Replies Latest reply: Aug 13, 2014 11:15 PM by Arie Stavchansky RSS

    Run applescripts from AIR

    Loic.Aigon Community Member

      Hi all,

       

      I am using NativeProcess to have some applescrit ran in the background. So far what I could achieved is to run applescript like this :

      1) Set osascript as the nativeProcess executable

      2) supply -e & "tell ..." as arguments

      The applescript runs fine.

       

      However, teh script I intend to run can be quite long. So I was more thinking about running osascript with a reference to the applescript file itself. So I put my applescript snippet ithin my src folder and try to pass it as the osascript command argument. It doesn't work not really surprisingly but fact is I don't see how to solve that.

       

      Do I have to copy my embedded applescript file to the local system and then call this specific instance ? Or should I decompose my script as a big array of lines and feed them to the osascript arguments (-e, line 1, -e, line 2 ...?). Second solution seems exhausting.

       

      So what is the way to call my applescript script ? I wish to keep a simple air app and not a bunch of separate files.

       

      Thanks for your help !

       

      Loic

        • 1. Re: Run applescripts from AIR
          Peter Blazejewicz Community Member

          Hi,

           

          I would advice to search the web for AppleScript discussions. When script is run (it is invoked with "on run") it could take numbered (finited) list of arguments or blanket, arrray like object of arguments in the way described below:

           

          on run args
                    set firstname to item 1 of args as string
                    set lastname to item 2 of args as string
                    return "Hello " & firstname & space & lastname
          end run
          

           

          that could be executed from terminal (using osascript as you already done):

           

          osascript MyScriplet.scpt Peter Blazejewicz
          

          which is expected to display (and actuall does):

           

          Hello Peter Blazejewicz
          

           

          This is a way you could pass arguments in post 10.4.* OS X command line. You would have to incorporate "if  then" conditions and "as string", "as boolean" AppleScript sugars in your script to check number of arguments and to use only passed ones and to convert them into required data type in AppleScript,

           

          hth,

          regards,

          Peter

          • 2. Re: Run applescripts from AIR
            Loic.Aigon Community Member

            Hi Peter,

             

            Your response is really helpful. I finally got the applescript file to be ran but passing arguments was a concern to which you answered.

             

            Thanks a lot

             

            Loic

            • 3. Re: Run applescripts from AIR
              thx1138 Community Member

              How are you using the osascript process? Can you post some code?    

              • 4. Re: Run applescripts from AIR
                Loic.Aigon Community Member

                Hi

                 

                Sorry for the delay. Technical issues with my yahoo mail :

                 

                Here is the code :

                 

                 

                do

                                                        {

                                                                  if (!NativeProcess.isSupported)

                                                                  {

                                                                            break;

                                                                  }

                                                                  var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();

                                                                  var cmdFile:File = File.documentsDirectory.resolvePath("/usr/bin/osascript");

                                                                  var scriptFile:File =File.documentsDirectory.resolvePath(File.applicationDirectory.nativePath+"/assets/test.scpt");

                                                                  if(! cmdFile.exists)

                                                                  {

                                                                            break;

                                                                  }

                                                                  if(! scriptFile.exists)

                                                                  {

                                                                            break;

                                                                  }

                                                                  nativeProcessStartupInfo.executable = cmdFile

                                                                  var processArgs:Vector.<String> = new Vector.<String>();

                                                                  processArgs.push(scriptFile.nativePath)

                                                                  nativeProcessStartupInfo.arguments = processArgs;

                                                                  nativeProcess = new NativeProcess();

                                                                  nativeProcess.start(nativeProcessStartupInfo);

                                                        }

                  while (false)

                 

                 

                Loic

                • 5. Re: Run applescripts from AIR
                  thx1138 Community Member

                  Great! Thank you Loic

                  • 6. Re: Run applescripts from AIR
                    Santanu Karar Community Member

                    Hi, presently I'm also put into this learning loop of communication between AIR and AppeScript. I yet not figured out how can I pass a command line argument to the AppleScipt (considering the AppleScript triggers Terminal app and run the command thrown by Flex as an argument). Did you through such scenario also? Can you help please?

                     

                     

                    Thank you.

                    • 7. Re: Run applescripts from AIR
                      Arie Stavchansky Community Member

                      Thanks for this code.  I have similar code that that I want to launch an Applescript via osascript.  I have a button that essentially starts the native process objects after I setup its StartupInfo object.  I have the executable set to an air File object type, and the nativePath property logs as "/usr/bin/osascript"

                       

                      The problem is that nothing happens when I click the button--I mean I know the event handler is being called because I have a simple log statement in there. But there are no errors that come up--it's silent.  How might I be able to investigate what is going on with the Native Process after the .start(StartupInfo) method is called.  Are there any listeners I can use?

                       

                      Thanks!