177

I want to launch an app on OSX from a script. I need to pass some 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?

3
  • I can't follow the question. "open" doesn't accept command-line args? What's "open"? Is this the AppleScript open? How does a nohup & solve the problem?
    – S.Lott
    Aug 20, 2009 at 21:03
  • 5
    man open(1) - OS X command to launch files or apps
    – Ned Deily
    Aug 20, 2009 at 21:22
  • 5
    This should be on superuser.com Oct 18, 2009 at 2:04

14 Answers 14

154

As was mentioned in the question here, the open command in 10.6 now has an args flag, so you can call:

open -n ./AppName.app --args -AppCommandLineArg

5
  • Working exactly the way it was required. Thanks
    – DTdev
    Nov 4, 2013 at 10:08
  • This worked and allowed me to set a zsh alias for Chromium. Thanks
    – james-see
    Sep 3, 2015 at 0:47
  • This answer is great. Just wanted to add that when you first open Terminal you will be in your home directory, so type it this way so you don't need to switch to the Application directory. I wanted to run Gimp without loading the fonts, which takes forever, so I type this with the -f switch to omit the fonts: open -n /Applications/GIMP.app --args -f Nov 21, 2015 at 15:38
  • Suppose my AppName executable was a shell script... how do I access -AppCommandLineArg within it? also I want to accept file drag and drop on my application icon. I searched a lot for this but couldn't find a helpful solution Mar 24, 2022 at 6:32
  • been searching for this answer for a few months. limping along with calling the full path to the executable directy from the command line. this is much more elegant. even allows me to start a new instance exactly as i wanted. kudos
    – horace
    May 18 at 20:07
112

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.

11
  • 1
    My kludgey approach is to invoke the executable directly with command line args using nohup - if the environment-vars-kludge less kludgey?
    – psychotik
    Aug 20, 2009 at 21:31
  • 2
    Probably not. If it works for you, go with it. The larger point is that open(1) is the cli equivalent of what happens when a user does a double-click or an "Open" in the Finder and none of those mechanisms support conventional command line arguments.
    – Ned Deily
    Aug 20, 2009 at 21:52
  • +1: You shouldn't be using command-line arguments for Mac OS X GUI applications.
    – S.Lott
    Aug 21, 2009 at 1:53
  • 2
    tell google not to use command line args, Applications/Google Chrome.app/Contents/Versions/6.0.472.53/Google Chrome Helper.app/Contents/MacOS/Google Chrome Helper --type=renderer --lang=en-US --force-fieldtest Dec 3, 2010 at 5:47
  • @Anurag Uniyal. "Why?" Did you read the answer? "the conventional mechanism is to use Apple Events for files like here for Cocoa apps or here for Carbon apps." That's the way Mac OS X is designed. Yes. Chrome breaks the rules. So does every single one of the Linux command-line utilities. But the GUI applications that are opened with open are -- as the answer says -- not designed to work from the command line. "Why?" Because that's the way Mac OS X is.
    – S.Lott
    Dec 3, 2010 at 11:13
59

An application bundle (.app file) is actually a directory. Instead of using open and the .app filename, you can move into the app's directory and start the actual machine code program located inside. For instance:

$ cd /Applications/LittleSnapper.app/
$ ls
Contents
$ cd Contents/MacOS/
$ ./LittleSnapper

That is the actual binary executable that might accept arguments (or not, in LittleSnapper's case).

7
  • 3
    The binary accepts arguments, I am sure. I wrote it. Invoking 'open' doesn't allow passing arguments. I am pretty sure about this. And yes, my script is calling 'open binary' directly, not trying to open the bundle.
    – psychotik
    Aug 20, 2009 at 21:05
  • 1
    @psychotik: He's not saying to call "open binary." He's saying to directly execute the binary.
    – Chuck
    Aug 20, 2009 at 21:22
  • That won't work for me. Directly executing the binary results in it being a child process of my script, which I definitely do not want. Hence, the need to use open/nohup
    – psychotik
    Aug 20, 2009 at 21:24
  • 1
    If it is a shell script, use 'exec' to replace the script process with the actual executable. Aug 20, 2009 at 21:35
  • 4
    This approach has a major drawback: if the bundle contains custom libraries and frameworks, you'll need to fiddle with DYLD_LIBRARY_PATH, DYLD_FRAMEWORK_PATH.
    – ulidtko
    Jun 19, 2012 at 14:11
19

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
2
  • This one is exactly what I was looking for! My instantiation, for example: open something.py -a Eclipse.app
    – Eric
    Sep 12, 2014 at 21:52
  • This answer works for me. My case is to use Numbers to open a file: open -a numbers filenanme.tsv
    – mathsyouth
    Nov 1, 2021 at 4:12
18

You can launch apps using open:

open -a APP_YOU_WANT

This should open the application that you want.

1
  • What if I want to process 'ls' command. Running "open -a ls" is giving me an error saying - Unable to find application named 'ls'. What to do in this when app_you_want is not a file or application.
    – Sanya
    Aug 4, 2021 at 8:01
6

open also has an -a flag, that you can use to open up an app from within the Applications folder by it's name (or by bundle identifier with -b flag). You can combine this with the --args option to achieve the result you want:

open -a APP_NAME --args ARGS

To open up a video in VLC player that should scale with a factor 2x and loop you would for example exectute:

open -a VLC --args -L --fullscreen

Note that I could not get the output of the commands to the terminal. (although I didn't try anything to resolve that)

2
  • Did you repeat the open -a intentionally?? In any case, open -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --user-data-dir=/Users/an/userll doesn't work for me.
    – Pacerier
    Dec 15, 2017 at 16:17
  • Pardon me, my syntax is completely off for some reason. I'll update now.
    – rien333
    Dec 15, 2017 at 18:00
4

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

1
  • @Pacerier Add a ` &` to the end of this command. It will launch in the background. You can then close your terminal window and the application will likely still remain open.
    – Paul Irish
    Dec 19, 2017 at 1:01
4

Lots of complex answers when you can simply access Applications folder and type:

open -a [APP NAME]

This is it!

2

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 altChrome:

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 --user-data-dir parameter).

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.

0
2

Beginning with OS X Yosemite, we can now use AppleScript and Automator to automate complex tasks. JavaScript for automation can now be used as the scripting language.

This page gives a good example example script that can be written at the command line using bash and osascript interactive mode. It opens a Safari tab and navigates to example.com.

http://developer.telerik.com/featured/javascript-os-x-automation-example/
osascript -l JavaScript -i
Safari = Application("Safari");
window = Safari.windows[0];
window.name();
tab = Safari.Tab({url:"http://www.example.com"});
window.tabs.push(tab); 
window.currentTab = tab;
2
  • check Script Editor / Window / Library for a list of apps that have applescript automation available. I was looking for Vysor, based on Chrome, and I think I need a different way. Oct 18, 2016 at 16:59
  • More complexity ftw !
    – Pacerier
    Dec 15, 2017 at 16:15
1

Simple, here replace the "APP" by name of the app you want to launch.

export APP_HOME=/Applications/APP.app/Contents/MacOS
export PATH=$PATH:$APP_HOME

Thanks me later.

1

To Create a New Text File OR open an existing one, in any folder, using a Text/Code Editor like the Free TextMate app on MACOSX, use this command on Terminal:

open -n /Applications/TextMate.app --args "$PWD/some file.txt"

Instead of a Text File, you can use any file type, based on your app's requirements and its support for this syntax.

This command also simulates the New Text Document Here Command on Windows and has been tested on MacBook Pro 2021 and Monterey 12.2.1 successfully.

0

With applescript:

tell application "Firefox" to activate
2
  • How do you pass arguments to Firefox? E.g., -no-remote -P "Another Profile". When developing add-ons and such, it is convenient to have two separate Firefox processes using two separate profiles. Your normal profile and a sandbox in which to debug your extension.
    – George
    Jan 29, 2014 at 0:27
  • I believe you are referring to an AppleScript directive, but the question is about opening from the command line ...
    – conny
    Jun 7, 2017 at 8:43
0

Why not just set add path to to the bin of the app. For MacVim, I did the following.

export PATH=/Applications/MacVim.app/Contents/bin:$PATH

An alias, is another option I tried.

alias mvim='/Applications/MacVim.app/Contents/bin/mvim'
alias gvim=mvim 

With the export PATH I can call all of the commands in the app. Arguments passed well for my test with MacVim. Whereas the alias, I had to alias each command in the bin.

mvim README.txt
gvim Anotherfile.txt

Enjoy the power of alias and PATH. However, you do need to monitor changes when the OS is upgraded.

3
  • "Remember MacOS is linux based". No, no it's not, macOS and Linux don't share a single line of code. MacOS is a UNIX with a kernel based on FreeBSD.
    – Thomas
    Feb 11, 2019 at 13:46
  • Will change. I do not want to create a religious war. The goal was to let the community know there are some powerful options that MacOS users can utilize.
    – zerocog
    Feb 12, 2019 at 16:58
  • Sure, no problem! :)
    – Thomas
    Feb 12, 2019 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.