终端命令打开新的Safari窗口

时间:2013-12-07 18:26:07

标签: objective-c macos shell safari terminal

从终端在Firefox中打开链接非常简单,firefox command提供了许多选项。例如,您可以firefox -new-window "http://stackoverflow.com"在新窗口中打开链接。

我想知道你是否可以在Mac上使用Safari做同样的事情?如果已经打开Safari窗口,open "http://stackoverflow.com"只会打开一个新标签 我知道可以让你在“在新标签页中打开”和“在新窗口中打开”之间切换的应用程序。是否有一个特殊的URL前缀告诉Safari打开一个新窗口?或者它是否可以通过Objective-C代码?

2 个答案:

答案 0 :(得分:7)

您可以使用.profile

中的osascript代码放置以下函数

代码:

function Safari {
  # Will open a New Safari window with argument 1.

osascript <<EOD
tell application "Safari" to make new document with properties {URL:"$1"}
return
EOD

}

在终端:

  • cd到您的主目录。
  • 运行:nano .profile

如果你已经有一个.profile文件,它将会打开,并且那里会有一些代码。

  • 在其他任何代码下面输入代码。

如果您没有,则会打开一个空白.profile文件。

  • 输入代码。

------输入代码----

按住 Ctrl 并按 x 键。

在窗口底部,系统会要求您保存更改。

enter image description here

点击 y 键获取是。

现在将要求您使用当前名称.profile

保存它

enter image description here

只需按 Enter 键即可保存。

运行:

  . ~/.profile

这将更新环境。

现在你可以运行:

Safari  "http://stackoverflow.com"

注意Safari中的Cap“S”。

答案 1 :(得分:1)

这使用了一种不同的方法(osascript vs open)但是我认为你要求的是:

osascript -e "tell application \"Safari\"
    make new document
    set URL of document 1 to \"http://www.apple.com/\"
    set bounds of window 1 to {500, 500, 800, 800}
end tell"