在 Mac OS X 上从命令行打开多个 Safari 选项卡

pup*_*eno 4 mac safari tabs command-line macos

我目前正在使用这个简单的命令从命令行打开网站:

open http://example.com
Run Code Online (Sandbox Code Playgroud)

但是当我打开两个时,甚至在做

open http://example.com http://example.org
Run Code Online (Sandbox Code Playgroud)

它们在单独的 Safari 窗口中打开。如何在不以任何方式更改 Safari 设置的情况下将它们与两个选项卡在同一个窗口中打开?

Law*_*uez 5

以下是更改 Safari 的常规设置以在新选项卡中打开新 URL 的方法: 使 Safari 将现有窗口中的新链接作为选项卡而不是新窗口打开

我不知道是否有办法通过在命令行中指定一个选项来做到这一点。

编辑:如果您出于某种原因不想更改您的 Safari 首选项,但仍希望能够从命令行在选项卡中打开新 URL,您可以创建一个 AppleScript,如下所示:

-- ~/Library/Scripts/newtab.scpt (or whatever name you'd like)
-- _argv will be the URLs given at the command line
on run _argv
    try
        tell application "Safari" to activate

        --repeat for each URL
        repeat with _i from 1 to length of _argv
            -- Copy URL to clipboard
            tell application "Safari" to set the clipboard to item _i of _argv

            -- Tell Safari to open a new tab, paste the URL, and "hit" Return
            tell application "System Events"
                tell process "Safari"
                    tell menu bar 1 to click menu item "New Tab" of menu "File" of menu bar item "File"
                    tell menu bar 1 to click menu item "Open Location…" of menu "File" of menu bar item "File"
                    tell menu bar 1 to click menu item "Paste" of menu "Edit" of menu bar item "Edit"
                    key code 36
                end tell
            end tell
        end repeat
    end try
end run
Run Code Online (Sandbox Code Playgroud)

并定义一个别名(或 shell 函数,或 shell 脚本,或其他),如下所示:

alias openurl="osascript ${HOME}/Library/Scripts/newtab.scpt"
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

openurl superuser.com stackoverflow.com serverfault.com
Run Code Online (Sandbox Code Playgroud)

这有点难看,但它应该可以完成工作。我认为。除非你真的被open.