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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10006958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:32:38  来源:igfitidea点击:

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

htmlmacosbashbrowserapplescript

提问by DorBB

So, this is what I need :

所以,这就是我需要的:

Let's say I have an index.htmlfile.

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

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

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

(Using AppleScript, BASH,...?)

(使用 AppleScript、BASH、...?)

回答by kevlarkevin

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

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

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

open 命令打开一个文件(或目录或 URL)。MacOSx 中包含 open。可以使用找到细节和选项

man open

note: default application is determined via LaunchServices.

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

回答by moomark

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

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

open -a "Google Chrome" index.html

open -a "Google Chrome" index.html

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

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

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

---> 我在堆栈交换中找到了这个答案,感谢用户“robmathers”

回答by kopischke

Actually, this is not quite as straightforward as it looks. As suggested by the other answers, OS X provides the openutility 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 httpno matter what, there is no way to access that protocol handler to open a file with open.

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

To compound the issue, although the handlers are stored in the com.apple.LaunchServices.plistpreferences accessible via the defaultscommand, 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.

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

The good news is somebody already solved that problem: HAMsoft Engineering offers the DefaultApplicationshell utility. Download itand 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/pathsto be sure). That available, the following command will open a HTML file in the default browser, whatever editor / viewer might be registered otherwise:

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

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

回答by Lri

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

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

回答by Zabe Sangary

In terminal you can run open index.html

在终端中,您可以运行 open index.html

回答by Vandit Shah

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

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

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.

open 是一个非常好的命令,也是 Mac OS 的一个功能,让我更深入地爱上它。它会自动选择适当的默认应用程序来打开文件。

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.

\Google 后面的反斜杠用于转义空格字符。

Alternatively you can write :

或者你可以写:

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

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

希望这可以帮助你(我知道我很晚了)和其他人!!!。

回答by Nameless477

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

通过将文件放在浏览器命令之后,我设法用 chrome 打开了 html 文件。所以,

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.

虽然我不确定默认浏览器的调用是什么,但如果你知道它,你可以将它作为别名放在你的 .bashrc 中,从那时起,使用你称之为别名的任何东西,加上文件。

goo ./index.html

just my experience, first response

只是我的经验,第一反应

回答by Sajjad Ahmed

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

对我有用的一种简单方法是 firefox ./index.html