【问题标题】:Terminal command to open new Safari window终端命令打开新的 Safari 窗口
【发布时间】:2013-12-25 01:03:38
【问题描述】:

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

我想知道您是否可以在 Mac 上使用 Safari 进行同样的操作? open "http://stackoverflow.com" 如果已经打开了一个 Safari 窗口,则只打开一个新选项卡。
我知道可以让您在“在新标签页中打开”和“在新窗口中打开”之间切换的应用程序。 URL 是否有一个特殊的前缀来告诉 Safari 打开一个新窗口?还是只能通过 Objective-C 代码实现?

【问题讨论】:

    标签: objective-c macos shell safari terminal


    【解决方案1】:

    这使用了不同的方法(osascript 与终端),但它可以满足我的要求:

    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}
        activate
    end tell"
    

    【讨论】:

      【解决方案2】:

      您可以将以下函数与 osascript 代码放在您的 .profile 中

      代码:

      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 键。

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

      y 键表示是。

      您现在将被要求使用当前名称 .profile 保存它

      只需按 Enter 键即可保存。

      运行:

        . ~/.profile
      

      这将更新环境。

      现在你可以运行了:

      Safari  "http://stackoverflow.com"
      

      注意 Safari 中的大写“S”。

      【讨论】:

      • 谢谢!我正在使用vim,但无论如何感谢您的解释:-)
      猜你喜欢
      • 2011-08-08
      • 2010-12-20
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多