Open an .html file with default browser using Bash on Mac

Go To StackoverFlow.com

52

So, this is what I need :

Let's say I have an index.html file.

How do I tell the terminal to open it using the default browser?

(Using AppleScript, BASH,...?)

2012-04-04 07:51
by DorBB


53

from the directory containing index.html, try...

open ./index.html

the open command opens a file (or directory, or URL). open is included with MacOSx. specifics and options can be found using

man open

note: default application is determined via LaunchServices.

2012-04-04 08:38
by kevlarkevin
That will open the HTML file in the default editor or viewer, which is not guaranteed to be the browser. See my answer for a solution that works no matter what - kopischke 2012-04-20 17:24


23

You can use the open command with the -a flag to open a file or location in Chrome (or any target application):

open -a "Google Chrome" index.html

This also works with URLs, i.e. open -a "Google Chrome" http://www.apple.com.

---> I found this answer @ stack exchange, thanks to user "robmathers"

2014-09-08 19:27
by moomark
The requirement was to use the Default browser, so by specifying Google Chrome, you have failed to meet the requirements - tresf 2015-08-30 03:25


14

Actually, this is not quite as straightforward as it looks. As suggested by the other answers, OS X provides the open utility to launch applications matching a file type from the shell. However, in the case of a HTML file, that is the application registered with Launch Services for the file type public.html, which can, but need not be, your default browser (I think it is on a pristine install) – or whatever editor registers as able to edit HTML (not an uncommon occurrence on a dev system). And while the default browser is registered for the URL protocol http no matter what, there is no way to access that protocol handler to open a file with open.

To compound the issue, although the handlers are stored in the com.apple.LaunchServices.plist preferences accessible via the defaults command, the structure of the information (a dictionary with two same level entries, one denoting the protocol, one the handler) makes it non-trivial to parse with defaults.

The good news is somebody already solved that problem: HAMsoft Engineering offers the DefaultApplication shell utility. Download it and save it somewhere where it is accessible to the shell (typically /usr/local/bin, although that is not in the default path for shells on some OS X versions – check the contents of /etc/paths to be sure). That available, the following command will open a HTML file in the default browser, whatever editor / viewer might be registered otherwise:

open -a "$(/usr/local/bin/DefaultApplication -url 'http:')" "/path/to/your/document.html"
2012-04-20 17:23
by kopischke
Very nice solution. What is wrong with simply putting file:/// in front of the path - tresf 2015-08-30 03:24
@QZSupport funny you should ask, as that was my first idea when I set out to answer this, but there are two stumbling stones: 1. needs URL encoding to handle things like spaces in file names and 2. doesn’t work for file paths with a network protocol like smb: or afp: - kopischke 2015-08-30 09:01


3

You can also get the default browser with Perl: open http://example.com -a "$(VERSIONER_PERL_PREFER_32_BIT=true perl -MMac::InternetConfig -le 'print +(GetICHelper "http")[1]')".

2012-09-12 21:22
by Lri


1

i managed to open the html file with chrome by placing the file after the browser command. so,

google-chrome-stable ./index.html

although im not sure what the call would be to the default browser, if you knew it you could put it as an alias in your .bashrc and from then on, use whatever you called your alias, plus the file.

goo ./index.html

just my experience, first response

2016-11-18 15:52
by Nameless477


1

In terminal you can run open index.html

2016-11-26 18:45
by Zabe Sangary


-2

One simple method worked for me is firefox ./index.html

2016-10-16 11:54
by Sajjad Ahmed
Ads