Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Process.Start() or equivalent with Mono on a Mac and pass in arguments

Tags:

I am trying to write some c# code to start a browser using Process.Start(app,args); where apps is the path to the browser e.g. /Applications/Google Chrome.app/Contents/MacOS/Google Chrome and the args are --no-default-browser-check

If i do, which works on Windows and on Linux

Process.Start("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome","--no-first-run"); 

I get

open: unrecognized option `--no-first-run' Usage: open [-e] [-t] [-f] [-W] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] Help: Open opens files from a shell.       By default, opens each file using the default application for that file.         If the file is in the form of a URL, the file will be opened as a URL. Options:        -a                Opens with the specified application.       -b                Opens with the specified application bundle identifier.       -e                Opens with TextEdit.       -t                Opens with default text editor.       -f                Reads input from standard input and opens with TextEdit.       -W, --wait-apps   Blocks until the used applications are closed (even if they were already running).       -n, --new         Open a new instance of the application even if one is already running.       -g, --background  Does not bring the application to the foreground.       -h, --header      Searches header file locations for headers matching the given filenames, and opens them. 

I have also tried Monobjc to try run the code with

// spin up the objective-c runtime ObjectiveCRuntime.LoadFramework("Cocoa"); ObjectiveCRuntime.Initialize(); NSAutoreleasePool pool = new NSAutoreleasePool();  // Create our process NSTask task = new NSTask(); NSPipe standardOut = new NSPipe(); task.StandardOutput = standardOut; task.LaunchPath = @"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";  // add some arguments NSString argumentString = new NSString("--no-first-run"); NSArray arguments = NSArray.ArrayWithObject(argumentString); task.Arguments = arguments;  // We should have liftoff task.Launch();   // Parse the output and display it to the console NSData output = standardOut.FileHandleForReading.ReadDataToEndOfFile; NSString outString = new NSString(output,NSStringEncoding.NSUTF8StringEncoding); Console.WriteLine(outString);  // Dipose our objects, gotta love reference counting pool.Release(); 

But when I run my code using NUnit it causes NUnit to blow up.

I suspect that this is a bug but can't prove it. I appreciate any and all help!

like image 258
AutomatedTester Avatar asked Feb 12 '10 22:02

AutomatedTester


2 Answers

To make Process.Start use exec directly instead of using the OS' mechanism for opening files, you must set UseShellExecute to false. This is also true on Linux and Windows.

Process.Start(new ProcessStartInfo (     "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",     "--no-first-run")     { UseShellExecute = false }); 

Note that you can also use 'open' for your use case, to run the Chrome app bundle properly. Use the '-a' argument to force it to run a specific app, the '-n' argument to open a new instance, and '--args' to pass in arguments:

Process.Start(new ProcessStartInfo (     "open",     "-a '/Applications/Google Chrome.app' -n --args --no-first-run")     { UseShellExecute = false }); 
like image 104
Mikayla Hutchinson Avatar answered Oct 19 '22 03:10

Mikayla Hutchinson


Looks like Process uses the open command line utility to launch.

You should avoid calling the executable directly. If the application is already running, this would launch a second instance of it instead of activating the already running instance. That's probably not what you want, and not all applications can handle this anyway.

With open, the syntax to launch Chrome would be

open -a Chrome 

I don't know how the Process class works on MacOS X, but I assume that the parameters should be similar.

Note, if you just want to open a web page, you should not specify the executable; instead, just pass the URL, so that it will be opened in the user's default browser. This is valid for any platform.

Process.Start("http://www.google.com"); 
like image 42
oefe Avatar answered Oct 19 '22 04:10

oefe