macos 打开 Safari 的终端命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5721528/
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
Terminal command to open Safari
提问by Giannis
Is there a way to open a new Safari tab with a given URL using a terminal command ? Im only interested for it to work on OSX at this point . If someone knows how to do it please let me know as i couldnt find the answer by browsing the web.I want to use that command to open a new tab from a java application. Thanks !
有没有办法使用终端命令打开带有给定 URL 的新 Safari 选项卡?我现在只对它在 OSX 上工作感兴趣。如果有人知道怎么做,请告诉我,因为我无法通过浏览网页找到答案。我想使用该命令从 Java 应用程序打开一个新选项卡。谢谢 !
回答by Kai Sternad
If Safari is your default browser, you just use
如果 Safari 是您的默认浏览器,您只需使用
open <url>
for example:
例如:
open http://www.google.com
You can also use this command to open any file with its registered application.
您还可以使用此命令打开任何带有注册应用程序的文件。
回答by Moritz
There are several ways to do this. The simplest way would be to use /usr/bin/open
:
有几种方法可以做到这一点。最简单的方法是使用/usr/bin/open
:
/usr/bin/open http://stackoverflow.com/
will open the URL in the standard browser or
将在标准浏览器中打开 URL 或
/usr/bin/open -a Safari http://stackoverflow.com/
will explicitly use Safari.
将明确使用 Safari。
You can also use AppleScript via the javax.script
API (JSR 223) but I do not know the syntax right now.
您也可以通过javax.script
API (JSR 223)使用 AppleScript,但我现在不知道语法。
回答by Antonio1996
You could use the open
command on MacOS. (see man open
for more information about this command).
您可以open
在 MacOS 上使用该命令。(man open
有关此命令的更多信息,请参见)。
In your case you can write something like that: open -a Safari https://www.stackoverflow.com
and it works. (NB: Remember to specify the protocol!)
在您的情况下,您可以编写类似的内容:open -a Safari https://www.stackoverflow.com
并且它有效。(注意:记得指定协议!)
In addition, I wrote an useful script to interact with Safari from MacOS and one of the options of this script is exactly what you want, see it on GitHub here https://github.com/antoniolanza1996/openSafariFromTerminal
此外,我编写了一个有用的脚本来与 MacOS 中的 Safari 交互,该脚本的选项之一正是您想要的,请在此处在 GitHub 上查看https://github.com/antoniolanza1996/openSafariFromTerminal