13 Replies Latest reply: Jul 24, 2013 2:35 AM by fivan RSS

    native process execution problem in mac os

    aswathykrishnan

      hi,

       

           In my flex application i have to get mac serial  number as a unique id for each system . On searching i got one terminal command to get serial number. For that i have  run terminal command through flex native process. I have set supportedprofile as <supportedProfiles>extendedDesktop desktop</supportedProfiles> in app xml. But when i install and run application on mac terminal is getting opened but command is not executing. I couldn't trace out the problem.  I could execute cmd in windows and that worked . Please help me to findout a solution................... The test code is adding below

       

       

       

      private var process:NativeProcess
      =new NativeProcess();

       

      protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void

                  {

                      // TODO Auto-generated method stub

       

       

       

                      if(NativeProcess.isSupported) {

       

                          Alert.show("Yes NativeProcess");

       

       

                          process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutput);

                          process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onIOError);

                          process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);

                          process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);

                          process.addEventListener(NativeProcessExitEvent.EXIT,onIOError);

       

                          var info:NativeProcessStartupInfo     =     new NativeProcessStartupInfo();

       

                          info.executable                               =     new File("/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal");

       

       

                           /*  var startupArgs:Vector.<String>    =     new Vector.<String>();

                               startupArgs[0]                              =    'system_profiler |grep "r (system)" '; 

                               info.arguments                              =    startupArgs;*/

       

                          //Alert.show(String(info.arguments.length));

       

                           process.start(info); 

                          process.standardInput.writeUTFBytes('system_profiler |grep "r (system)" '+ "\n");

       

                      }else

                      {

                          Alert.show("No");

                      }

       

                  }

       

                  protected function onIOError(event:IOErrorEvent):void

                  {

                      // TODO Auto-generated method stub

       

                      Alert.show("Error");

       

                  }

       

                  protected function onOutput(event:ProgressEvent):void

                  {

                      var processID:String            =    process.standardOutput.readMultiByte(process.standardOutput.bytesAvailable,"");       

                      Alert.show("Path : " + processID );

                  }

       

       

      I have tried to set arguments but didn't execute......

       

      Message was edited by: aswathykrishnan

        • 1. Re: native process execution problem in mac os
          Riccardo Moschetti Community Member

          Unfortunately I am at work with a PC, so I can't test on my mac.

          Where does the program stop? Does it go into the "Yes native" branch and then you get the "Error" alert? Or you get the "No" alert right away?

          • 2. Re: native process execution problem in mac os
            aswathykrishnan Community Member

            Thanks for your response.

             

                                   Yes,  Its going to 'Yes native process' branch, but not getting error message. The terminal is getting opened bt command is not executing ,thats the problem..

            • 3. Re: native process execution problem in mac os
              aswathykrishnan Community Member

              The problem is solved.Instead of terminal command i just executed an applescript.

               

               

               

                             thanks...............

              • 4. Re: native process execution problem in mac os
                Santanu Karar Community Member

                Hello Aswathy, these days I'm also coding an application which uses AIR NativeProcess and I required to open the MacOS terminal with some parameter/commands. I also eventually turn to the AppleScript, but I considerably new to the AppleScript, yet trying to findout a way to passing parameters (the command argument) to the AppleScript; the appleScript I managed to open but not passing the argument. Can you please help? here's my some probable codes for the application:

                 

                My appleScript is:

                 

                #!/bin/bash

                on run argv

                          tell application "Terminal"

                                    set currentTab to (do script) & item 1 of argv

                          end tell

                end run

                 

                I've packaged the appleScript as an Application (Contract.app) and here's my Flex code:

                 

                var npInfo : NativeProcessStartupInfo = new NativeProcessStartupInfo();

                npInfo.executable = new File( "/Applications/Contract.app/Contents/MacOS/applet" );

                 

                                              var arg:Vector.<String> = new Vector.<String>;

                                                  arg[0]="ssh santanu@172.25.1.22";

                 

                                              npInfo.arguments = arg;

                                              process = new NativeProcess();

                  process.start( npInfo );

                 

                 

                Can you please help?

                 

                Thank you.

                • 5. Re: native process execution problem in mac os
                  fivan Community Member

                  Hi aSuitableBoy!

                   

                  I'm having the same problem.

                  How do you have solved?

                   

                  Thanks

                   

                  Ivan

                  • 6. Re: native process execution problem in mac os
                    Santanu Karar Community Member

                    Keep the NativeProcess part as it is, following is basic codes in AppleScript to make it work:

                     

                    #!/bin/bash

                                on run argv

                               

                                    set username to (item 1 of argv) as string

                                    set sshurl to (item 2 of argv) as string

                                   

                                    tell application "Terminal"

                                       

                                        do script "clear"

                                        activate

                                        set currentTab to (selected tab of (get first window))

                                        set tabProcs to processes of currentTab

                                        set theProc to (end of tabProcs)

                                       

                                        do script <all your terminal command goes here inside a double quote, see next lines for example> in currentTab

                                        set scriptline to "ssh " & username & "@" & sshurl  in currentTab

                                        do script scriptline in currentTab

                               

                                    end tell

                               

                                end run

                    • 7. Re: native process execution problem in mac os
                      fivan Community Member

                      Still dont works.

                       

                      In argv i find "current application" and I can't get any item.

                       

                      Now I write my parameters in a txt file in the applicationStorageDirectory and then I read this file in Applescript.

                      It's just a workaround and I don't like it but I dont know where is the problem...

                       

                      Thanks anyway

                      • 8. Re: native process execution problem in mac os
                        Santanu Karar Community Member

                        My bad, the NativeProcess call should have be a bit different - please follow the call below:

                         

                         

                        var npInfo : NativeProcessStartupInfo = new NativeProcessStartupInfo();

                        var arg:Vector.<String>;

                         

                        // note the executable which is differnt from my earlier posting

                        npInfo.executable = File.documentsDirectory.resolvePath( "/usr/bin/osascript" );

                        arg = new Vector.<String>();

                        arg.push( <your appleScript file path - you may want to use any File reference here> );

                        // adding my 2 parameters as per my previous AppleScript code example

                        arg.push( "myUserName" );

                        arg.push( "170.2.0.0" );

                         

                        npInfo.arguments = arg;

                        process = new NativeProcess();

                        process.start( npInfo );

                         

                         

                         

                        Also, I made a slight change in AppleScript 'set scriptline' code as well, please check:

                         

                        [before]

                        set scriptline to "ssh " & username & "@" & sshurl  in currentTab

                        [change to]

                        set scriptline to "ssh " & username & "@" & sshurl

                        • 9. Re: native process execution problem in mac os
                          fivan Community Member

                          First you call "/usr/bin/osascript" and next the osascript calls the applescript.

                           

                          So there is no way to pass parameters to AppleScript directly. isn't it?

                           

                           

                          Finaly now I know it's not my fault..

                          • 10. Re: native process execution problem in mac os
                            Santanu Karar Community Member

                            Not sure about your usage, though you can opt for much simpler usage (if suits) other than using AppleScript -

                             

                            arg.push( "-e" );

                            arg.push( 'tell application "Terminal" to activate & do script "'+ command +'"'+'\n' );

                            arg.push( 'end tell' );

                            • 11. Re: native process execution problem in mac os
                              fivan Community Member

                              I have to change write permissions to a folder and, may be, I need administrator authorization

                              So in applescript it's easy and I can do that just with a single command line.

                               

                                   do shell script "sudo chmod 777 " & MyFolder with administrator privileges

                               

                              The system dialog appears and ask the password to the user and then do the "chmod" command

                               

                              But the problem is:

                              How can I tell ApplleScript the name of MyFolder?

                               

                              At first sight it seems NativeProcess is the right solution but it dont work.

                              The applescript starts correctly but I can't read arguments.

                               

                              Now any other solution is a good solution, also your suggestions are appreciated.

                               

                              Thanks again

                               

                              Ivan

                              • 12. Re: native process execution problem in mac os
                                Santanu Karar Community Member

                                I'm not sure why you cant read argument values with given appleScript example - NativeProcess transfers arguments very well to AppleScript though. Hope you didn't overlooked this line while calling -

                                 

                                arg.push( <your appleScript file path - you may want to use any File reference here> );

                                • 13. Re: native process execution problem in mac os
                                  fivan Community Member

                                  May be I dont understand..

                                   

                                  In your example, NativeProcess calls "osascript", not the applescript.

                                  The Applescript file is the first parameter passed to osascript.

                                   

                                       arg.push( <your appleScript file path - you may want to use any File reference here> );

                                   

                                  Next osascript calls  apllescript passing other parameters and it works fine.

                                   

                                  Is it correct?

                                   

                                  What I want to do is to call Applescrip directly from NativeProcess with parameters.

                                   

                                  ActionScript code pass arguments to osascrip without problems and osascritp send parameters to applescript correctly

                                  The same ActionScript code calls Applescript correctly but Applescrit can't read parameters..

                                   

                                  Probably i'm wrong but I found many other examples and all of them calls first osascript, just like you.

                                  And also who begins calling first Applescript, next, for many reasons, changes solution.

                                   

                                  Why?

                                  Noone knows...