How to launch an app on OS X with command line - The best way
Asked Answered
S

15

184

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?

Shorn answered 20/8, 2009 at 20:56 Comment(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?Antifreeze
man open(1) - OS X command to launch files or appsCincinnati
This should be on superuser.comWashery
C
118

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.

Cincinnati answered 20/8, 2009 at 21:27 Comment(11)
My kludgey approach is to invoke the executable directly with command line args using nohup - if the environment-vars-kludge less kludgey?Shorn
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.Cincinnati
+1: You shouldn't be using command-line arguments for Mac OS X GUI applications.Antifreeze
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-fieldtestCadal
@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.Antifreeze
@Antifreeze but it can not be avoided in some cases, as in the case of chrome, and many apps are ported from other systems to Mac and they can't avoid this + Mac allowed command line args in 10.6Cadal
@Anurag Uniyal. "Chrome breaks the rules. So does every single one of the Linux command-line utilities". I think I already agreed with you. What's the point?Antifreeze
@Antifreeze point is you said "You shouldn't be using command-line arguments for Mac OS X GUI applications" and I said why not when I have no other easy option, + where is the rule written GUI app shouldn't use commandline?Cadal
@Anurag Uniyal. Are you aware of what "should" means? It doesn't mean "Must be physically restrained". It doesn't mean "involves a logical contradiction and does not exist". It doesn't mean "is illegal in many countries". It means you shouldn't. Some applications break the rules, however, and you can. What's the point? Why is this so important to keep repeating? Is it because the word "should" is confusing? What word would you like? What would "should" I use instead of "should"?Antifreeze
@Antifreeze , I think "shouldn't be" be replaced by "should avoid", it is era of internet activism, anyway sorry for stretching it so long, i shall delete my comments now.Cadal
How can I use the argument in my app?Abut
D
164

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

Devlin answered 20/8, 2009 at 20:56 Comment(4)
This worked and allowed me to set a zsh alias for Chromium. ThanksPretzel
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 -fCarabineer
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 solutionShirleenshirlene
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. kudosLofton
C
118

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.

Cincinnati answered 20/8, 2009 at 21:27 Comment(11)
My kludgey approach is to invoke the executable directly with command line args using nohup - if the environment-vars-kludge less kludgey?Shorn
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.Cincinnati
+1: You shouldn't be using command-line arguments for Mac OS X GUI applications.Antifreeze
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-fieldtestCadal
@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.Antifreeze
@Antifreeze but it can not be avoided in some cases, as in the case of chrome, and many apps are ported from other systems to Mac and they can't avoid this + Mac allowed command line args in 10.6Cadal
@Anurag Uniyal. "Chrome breaks the rules. So does every single one of the Linux command-line utilities". I think I already agreed with you. What's the point?Antifreeze
@Antifreeze point is you said "You shouldn't be using command-line arguments for Mac OS X GUI applications" and I said why not when I have no other easy option, + where is the rule written GUI app shouldn't use commandline?Cadal
@Anurag Uniyal. Are you aware of what "should" means? It doesn't mean "Must be physically restrained". It doesn't mean "involves a logical contradiction and does not exist". It doesn't mean "is illegal in many countries". It means you shouldn't. Some applications break the rules, however, and you can. What's the point? Why is this so important to keep repeating? Is it because the word "should" is confusing? What word would you like? What would "should" I use instead of "should"?Antifreeze
@Antifreeze , I think "shouldn't be" be replaced by "should avoid", it is era of internet activism, anyway sorry for stretching it so long, i shall delete my comments now.Cadal
How can I use the argument in my app?Abut
F
62

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

Fluting answered 20/8, 2009 at 21:1 Comment(7)
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.Shorn
@psychotik: He's not saying to call "open binary." He's saying to directly execute the binary.Houck
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/nohupShorn
If it is a shell script, use 'exec' to replace the script process with the actual executable.Visual
This is a python script invoking a shell command using os.system(). In any case, 'exec' launches my app as a child process so it's the same issue.Shorn
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.Authenticate
Too bad it doesn't load custom libraries, because it shows the console output.Ferromagnesian
R
21

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
Rollo answered 20/8, 2009 at 20:56 Comment(2)
This one is exactly what I was looking for! My instantiation, for example: open something.py -a Eclipse.appRecitativo
This answer works for me. My case is to use Numbers to open a file: open -a numbers filenanme.tsvFrostbite
Q
19

You can launch apps using open:

open -a APP_YOU_WANT

This should open the application that you want.

Quantifier answered 20/8, 2009 at 20:56 Comment(2)
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.Despotism
@Despotism what are you talking about? 'ls' is not an app. It does not need to be called with open -a ls, it's just lsYakka
L
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)

Lemmuela answered 20/8, 2009 at 20:56 Comment(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.Chaps
Pardon me, my syntax is completely off for some reason. I'll update now.Lemmuela
R
4

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

open -a [APP NAME]

This is it!

Rarefied answered 20/8, 2009 at 20:56 Comment(0)
G
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. :)

Gobbet answered 20/8, 2009 at 20:56 Comment(1)
@Chaps 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.Gobbet
B
3

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.

Banlieue answered 20/8, 2009 at 20:56 Comment(0)
S
3

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.

Sheaff answered 20/8, 2009 at 20:56 Comment(0)
S
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;
Shropshire answered 20/8, 2009 at 20:56 Comment(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.Rote
More complexity ftw !Chaps
M
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.

Magaretmagas answered 20/8, 2009 at 20:56 Comment(0)
N
0

first you should change the permission of that file. as follows :

chmod +rwx fileName

r: means read w: means write e: means execute

after that execute it with this :

./fileName

or

bash ./fileName

note that bash or zsh should be as path variable.

Natelson answered 20/8, 2009 at 20:56 Comment(0)
G
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.

Gladiator answered 20/8, 2009 at 20:56 Comment(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.Pecten
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.Gladiator
Sure, no problem! :)Pecten
I
0

With applescript:

tell application "Firefox" to activate
Iliad answered 20/8, 2009 at 20:56 Comment(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.Sheaff
I believe you are referring to an AppleScript directive, but the question is about opening from the command line ...Retrospect

© 2022 - 2024 — McMap. All rights reserved.