那么,这是我需要的:
假设我有一个index.html
文件。
如何告诉终端使用默认浏览器打开它?
(使用AppleScript、BASH等?)
那么,这是我需要的:
假设我有一个index.html
文件。
如何告诉终端使用默认浏览器打开它?
(使用AppleScript、BASH等?)
从包含index.html的目录中尝试...
open ./index.html
open命令可打开文件(或目录、URL)。MacOSx已包含了open。您可以使用具体选项来查找open的详细信息。
man open
注意:默认应用程序是通过LaunchServices确定的。
您可以使用带有 -a 标志的 open 命令在 Chrome(或任何目标应用程序)中打开文件或位置:
open -a "Google Chrome" index.html
这也适用于 URL,即 open -a "Google Chrome" http://www.apple.com。
---> 我在 stack exchange 上找到了这个答案,感谢用户“robmathers”。
实际上,这并不像看起来的那么简单。正如其他答案所建议的,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"
file:///
有什么问题吗? - tresfsmb:
或afp:
)的文件路径。 - kopischkeopen
会使用默认浏览器打开 html。不过你得到了我的投票。 - Timo要在默认浏览器中打开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
google-chrome-stable ./index.html
goo ./index.html
这只是我的经验,第一次回应
在终端中,你可以运行 open index.html 命令。
您还可以使用Perl获取默认浏览器:open http://example.com -a "$(VERSIONER_PERL_PREFER_32_BIT=true perl -MMac::InternetConfig -le 'print +(GetICHelper "http")[1]')"
。
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
"
这在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
但是这个不起作用