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

前端 未结 8 1634
陌清茗
陌清茗 2020-12-13 17:17

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

相关标签:
8条回答
  • 2020-12-13 17:41

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

    0 讨论(0)
  • 2020-12-13 17:49

    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"
    
    0 讨论(0)
提交回复
热议问题