Launch an app on OS X with command line
I want to launch an app on OSX from a script. I need pass it command line arguments. Unfortunately,
open doesn't accept command line args.
The only option I can think of is to use
nohup myApp > /dev/null & to launch my app so it can exist independently of the script that launches it.
Any better suggestions?
In OS X 10.6, the
open command was enhanced to allow passing of arguments to the application:
open ./AppName.app --args -AppCommandLineArg
But for older versions of Mac OS X, and because app bundles aren't designed to be passed command line arguments, the conventional mechanism is to use Apple Events for files like here for Cocoa apps or here for Carbon apps. You could also probably do something kludgey by passing parameters in using environment variables.
Read more... Read less...
An application bundle (a
.app file) is actually a bunch of directories. Instead of using
open and the
.app name, you can actually move in to it and start the actual binary. For instance:
$ cd /Applications/LittleSnapper.app/ $ ls Contents $ cd Contents/MacOS/ $ ./LittleSnapper
That is the actual binary that might accept arguments (or not, in
In case your app needs to work on files (what you would normally expect to pass as:
./myApp *.jpg), you would do it like this:
open *.jpg -a myApp
I would recommend the technique that MathieuK offers. In my case, I needed to try it with Chromium:
> Chromium.app/Contents/MacOS/Chromium --enable-remote-fonts
I realize this doesn't solve the OP's problem, but hopefully it saves someone else's time. :)
I wanted to have two separate instances of Chrome running, each using its own profile. I wanted to be able to start them from Spotlight, as is my habit for starting Mac apps. In other words, I needed two regular Mac applications,
regChrome for normal browsing and
altChrome to use the special profile, to be easily started by keying ⌘-space to bring up Spotlight, then 'reg' or 'alt', then Enter.
I suppose the brute-force way to accomplish the above goal would be to make two copies of the Google Chrome application bundle under the respective names. But that's ugly and complicates updating.
What I ended up with was two AppleScript applications containing two commands each. Here is the one for
do shell script "cd /Applications/Google\\ Chrome.app/Contents/Resources/; rm app.icns; ln /Users/garbuck/local/chromeLaunchers/Chrome-swirl.icns app.icns" do shell script "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --user-data-dir=/Users/garbuck/altChrome >/dev/null 2>&1 &"
The second line starts Chrome with the alternate profile (the
The first line is an unsuccessful attempt to give the two applications distinct icons. Initially, it appears to work fine. However, sooner or later, Chrome rereads its icon file and gets the one corresponding to whichever of the two apps was started last, resulting in two running applications with the same icon. But I haven't bothered to try to fix it — I keep the two browsers on separate desktops, and navigating between them hasn't been a problem.