Skip navigation
Currently Being Moderated

native process execution problem in mac os

May 7, 2012 1:11 AM

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.bytesAvai lable,"");       

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

            }

 

 

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

 

Message was edited by: aswathykrishnan

 
Replies
  • Currently Being Moderated
    May 7, 2012 5:31 AM   in reply to aswathykrishnan

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 15, 2012 5:22 AM   in reply to aswathykrishnan

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 17, 2013 6:16 AM   in reply to Santanu Karar

    Hi aSuitableBoy!

     

    I'm having the same problem.

    How do you have solved?

     

    Thanks

     

    Ivan

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 23, 2013 9:53 PM   in reply to fivan

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 24, 2013 12:17 AM   in reply to Santanu Karar

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 24, 2013 12:33 AM   in reply to fivan

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 24, 2013 1:00 AM   in reply to Santanu Karar

    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..

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 24, 2013 1:21 AM   in reply to fivan

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 24, 2013 1:50 AM   in reply to Santanu Karar

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 24, 2013 1:58 AM   in reply to fivan

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 24, 2013 2:35 AM   in reply to Santanu Karar

    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...

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points