Kurt Peek
Kurt Peek

Reputation: 57771

Defining a shortcut to open Chrome from the command line on MacOS

I'd like to open .html and .xml files from the command line using Google Chrome on a Mac. Usually I just used the open command, but for .xml files I've noticed that the default application is XCode, so I'd like to specify the application with the -a argument.

The following command works:

Kurts-MacBook-Pro:MacOS kurtpeek$ open -a "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ~/Documents/Seattle/comparables.xml

Here I've enclosed the path to Google Chrome - which has spaces in it - in quotes. In order to make this easier to type in the future, I'd like to define an environment variable $chrome in by .bash_profile containing this path, which I did as follows:

export chrome="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"

The echo command does print this out:

Kurts-MacBook-Pro:~ kurtpeek$ echo $chrome
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome

However, if I try to rerun the open -a with this environment variable to specify the application, I get the following error:

Kurts-MacBook-Pro:~ kurtpeek$ open -a $chrome Documents/Seattle/comparables.xml
The files /Users/kurtpeek/Chrome.app/Contents/MacOS/Google and /Users/kurtpeek/Chrome do not exist.

Apparently, in this case, the Bash shell does not 'recognize' the quotation marks I put in the definition of chrome. How can I make this work?

Upvotes: 1

Views: 955

Answers (1)

Kurt Peek
Kurt Peek

Reputation: 57771

Following Set environment variable with having space linux, this can be done by enclosing $chrome in quotes:

Kurts-MacBook-Pro:~ kurtpeek$ open -a "$chrome" Documents/Seattle/comparables.xml
Kurts-MacBook-Pro:~ kurtpeek$ 

and the XML document gets opened in Chrome:

enter image description here

Update

It would appear (from https://superuser.com/questions/157484/start-google-chrome-on-mac-with-command-line-switches/157486#157486) that the open command looks in the Applications directory by default. So it suffices to just pass "Google Chrome" as an -a argument:

Kurts-MacBook-Pro:~ kurtpeek$ open -a "Google Chrome" Documents/Seattle/comparables.xml
Kurts-MacBook-Pro:~ kurtpeek$ 

Upvotes: 1

Related Questions