在Mac上使用Bash打开默认浏览器的.html文件

87

那么,这是我需要的:

假设我有一个index.html文件。

如何告诉终端使用默认浏览器打开它?

(使用AppleScript、BASH等?)

10个回答

111

从包含index.html的目录中尝试...

open ./index.html

open命令可打开文件(或目录、URL)。MacOSx已包含了open。您可以使用具体选项来查找open的详细信息。

man open

注意:默认应用程序是通过LaunchServices确定的。


3
这将在默认的编辑器或查看器中打开HTML文件,但不能保证是浏览器。请参阅我的答案,其中提供了无论如何都能工作的解决方案。 - kopischke
2
这是最简单的答案,也是正确的,以防有人偶然遇到这个问题。 - qodeninja
最好在本地服务器上提供服务,然后: 打开 http://localhost:xxxx => ( xxxx = 任意端口号 9000 / 7000) - undefined

35

您可以使用带有 -a 标志的 open 命令在 Chrome(或任何目标应用程序)中打开文件或位置:

open -a "Google Chrome" index.html

这也适用于 URL,即 open -a "Google Chrome" http://www.apple.com

---> 我在 stack exchange 上找到了这个答案,感谢用户“robmathers”。


7
要求使用“默认浏览器”,因此通过指定Google Chrome,您未能满足要求。 - tresf
我同意,但这些信息是相关而深入的,即使它们也是次要的。 - Aaron Gorman
如果你有本地服务器,那就用: 打开 http://localhost:xxxx xxxx = 任何端口,比如9000或8000或其他 - undefined

22

实际上,这并不像看起来的那么简单。正如其他答案所建议的,OS X提供了open实用程序,以从shell启动与文件类型匹配的应用程序。但是,在HTML文件的情况下,它是注册为文件类型public.html的Launch Services应用程序,它可以是您的默认浏览器(我认为它在原始安装时是默认的),也可以是任何注册为能够编辑HTML的编辑器(在开发系统中并不少见)。而且,虽然无论如何默认浏览器都会为URL协议http注册,但没有办法访问该协议处理程序以使用open打开文件。

更棘手的是,尽管处理程序存储在可通过defaults命令访问的com.apple.LaunchServices.plist首选项中,但信息的结构(一个带有两个相同级别条目的字典,一个表示协议,一个表示处理程序)使得使用defaults进行解析变得复杂。

好消息是,已经有人解决了这个问题:HAMsoft Engineering提供了DefaultApplication shell实用程序。 下载它并将其保存在shell可以访问的某个地方(通常是/usr/local/bin,尽管这不是某些OS X版本上默认路径中的内容-请检查/etc/paths的内容以确保)。有了这个工具后,以下命令将在默认浏览器中打开HTML文件,无论其他编辑器/查看器是否注册:

open -a "$(/usr/local/bin/DefaultApplication -url 'http:')" "/path/to/your/document.html"

1
非常好的解决方案。在路径前面简单地放置 file:/// 有什么问题吗? - tresf
1
很有趣,你提出这个问题,因为这正是我开始回答时的第一个想法,但是有两个障碍:1.需要URL编码来处理文件名中的空格等内容;2.不适用于带有网络协议(如smb:afp:)的文件路径。 - kopischke
我会选择得票最高的答案,因为我们大多数人不会更改终端设置的默认设置 - 或者是负责此处的任何设置。默认设置是 - 正如您正确指出的那样 - open 会使用默认浏览器打开 html。不过你得到了我的投票。 - Timo

7

要在默认浏览器中打开filename.html文件,请使用:

open filename.html

Open是Mac OS的一个命令和功能,让我更加深爱它。 它可以自动选择适当的默认应用程序打开文件。

如果您想在所需的应用程序中打开文件而不是默认应用程序:

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

在Google后面使用反斜杠\来转义空格字符。

另一种写法:

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

希望这能对你(我知道我回复晚了)和其他人有所帮助!!!

1
这已经在其他答案中展示过了。请不要添加新的答案,只是再次重复其他答案中的内容。 - wim
老实说,我没有参考以下任何内容。如果你觉得有的话,我很抱歉。我是一个经常使用终端的用户,我已经知道上面的命令,所以复制答案没有意义。希望你安全健康。 #待在家里,保持安全 - Vandit Shah
@wim,你能指出谁用反斜杠转义写了这段代码吗?如果有一个问题与将要使用的知识范围有着狭窄的关系,那么它们注定会相似。重要的是你从哪个答案中理解最多,以及哪个答案形式更好。 感谢您的意见 :) - Vandit Shah

4
我通过在浏览器命令之后放置该文件来成功使用Chrome打开了HTML文件。因此,
google-chrome-stable ./index.html

尽管我不确定调用默认浏览器的命令是什么,但如果你知道它,你可以将其作为别名放在你的 .bashrc 文件中,从此以后,就可以使用你定义的别名加上文件名。
goo ./index.html

这只是我的经验,第一次回应


4

在终端中,你可以运行 open index.html 命令。


4

您还可以使用Perl获取默认浏览器open http://example.com -a "$(VERSIONER_PERL_PREFER_32_BIT=true perl -MMac::InternetConfig -le 'print +(GetICHelper "http")[1]')"


0

使用Homebrew安装默认浏览器:

brew install defaultbrowser

然后使用defaultbrowser来获取正确的二进制文件名,例如:

"$(defaultbrowser | grep '*' | awk '{ print $2 }')" https://google.com

0

ObjC解决方案:

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实现:

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
"

-1

这在Linux上可以运行,应该在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"

理想情况下,我只需要说。
xdg-open http+file:///path/to/file.html

但是这个不起作用


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接