84

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

DorBB
  • 841
  • 1
  • 6
  • 4

10 Answers10

107

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.

kevlarkevin
  • 1,211
  • 1
  • 7
  • 6
  • 3
    That will open the HTML file in the default **editor or viewer**, which is not guaranteed to be the browser. See [my answer](http://stackoverflow.com/a/10250717/990363) for a solution that works no matter what. – kopischke Apr 20 '12 at 17:24
  • 2
    this was the simplest answer that is also correct, in case anyone stumbles upon this. – qodeninja Mar 04 '19 at 20:58
33

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"

Community
  • 1
  • 1
moomark
  • 339
  • 3
  • 2
21

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"
kopischke
  • 3,393
  • 1
  • 21
  • 40
  • 1
    Very nice solution. What is wrong with simply putting `file:///` in front of the path? – tresf Aug 30 '15 at 03:24
  • 1
    @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 Aug 30 '15 at 09:01
  • I would go with the top voted answer because most of us do not change the default settings regarding terminal settings - or whatever setting is responsible here. The default is - as you pointed out correctly - that `open` opens a html with the default browser. You get my vote though – Timo Jan 12 '23 at 20:43
6

To open the filename.html in the default browser use :

open filename.html

open is a very good command as well as a feature of Mac OS that makes me fall in love with it more deeper. It automatically chooses the appropriate default app to open the file.

And in case you want to open a file in your desired app rather then default :

open -a /Applications/Google\ Chrome.app filename.html

The backslash \ after Google is used to escape the space character.

Alternatively you can write :

open -a "/Applications/Google Chrome.app" filename.html

Hope this helps you ( I know I am very late ) and others !!!.

Vandit Shah
  • 100
  • 1
  • 6
  • 1
    This is already shown in other answers. Please don't add new answers that just repeat the content from other answers again. – wim Jun 03 '20 at 14:41
  • Honestly I haven't referred any content below. If you felt so I am sorry for that. I am a consistent user of terminal and I already know above commands, so there is no point in copying answers. Hope you are safe and healthy. #StayHomeStaySafe – Vandit Shah Jun 04 '20 at 19:31
  • @wim also **can you point out who has written the code with a backslash escape ?** If there is a question with a narrow scope wrt to scope of knowledge that is going to be used, then they are bound to be similar. The thing that matters is from which answer you understand the most and also which is well formed. Thanks for your opinion :) – Vandit Shah Jun 04 '20 at 19:37
4

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]')".

Lri
  • 26,768
  • 8
  • 84
  • 82
4

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

Nameless477
  • 51
  • 1
  • 6
4

In terminal you can run open index.html

0

Install defaultbrowser with homebrew:

brew install defaultbrowser

And then use defaultbrowser to get the correct binary name, eg:

"$(defaultbrowser | grep '*' | awk '{ print $2 }')" https://google.com
Shelvacu
  • 4,245
  • 25
  • 44
0

AsObjC soluition:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theHTML to (choose file of type "public.html") as text

set defaultBrowserID to getDefaultBrowserID()
tell application id defaultBrowserID to open theHTML

on getDefaultBrowserID()
    set sharedWorkspace to (current application's NSWorkspace)'s sharedWorkspace()
    set urlNSString to (current application's NSString)'s stringWithString:"http://"
    set testURL to (current application's |NSURL|)'s URLWithString:urlNSString
    set theURL to sharedWorkspace's URLForApplicationToOpenURL:testURL
    set defaultBrowserID to id of application (theURL's |path|() as string)
end getDefaultBrowserID

Shell implementation:

osascript -e "use AppleScript version \"2.4\" -- Yosemite (10.10) or later
use framework \"Foundation\"
use scripting additions

set theHTML to (choose file of type \"public.html\") as text

set defaultBrowserID to getDefaultBrowserID()
tell application id defaultBrowserID to open theHTML

on getDefaultBrowserID()
    set sharedWorkspace to (current application's NSWorkspace)'s sharedWorkspace()
    set urlNSString to (current application's NSString)'s stringWithString:\"http://\"
    set testURL to (current application's |NSURL|)'s URLWithString:urlNSString
    set theURL to sharedWorkspace's URLForApplicationToOpenURL:testURL
    set defaultBrowserID to id of application (theURL's |path|() as string)
end getDefaultBrowserID
"
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
-1

this works on linux, should also work on mac

#!/bin/sh

# open a html file in default browser, not text editor,
# when text editor is set as default app for html files

url=file:///path/to/file.html

protocol=http

app=$(xdg-mime query default x-scheme-handler/$protocol)
# example: chromium-browser.desktop

[ -z "$app" ] && {
  echo "error: xdg-mime could not find default app for protocol $protocol"
  exit 1
}

app=$(basename $app .desktop)

gtk-launch $app "$url"

ideally i could just say

xdg-open http+file:///path/to/file.html

but this is not working

milahu
  • 2,447
  • 1
  • 18
  • 25