Open an .html file with default browser using Bash on Mac
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,...?)
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.
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"
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"
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]')"
.
In terminal you can run open index.html