使用AppleScript在新的Safari选项卡中打开URL

Mar*_*ski 34 safari macos applescript

是否可以使用AppleScript在Safari的新标签页中打开链接?

小智 39

这将有效:

tell application "Safari"
    tell window 1
        set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

  • 在5.1中不起作用,[applescript - 脚本Safari 5.1打开选项卡 - Stack Overflow](http://stackoverflow.com/questions/6941630/script-safari-5-1-to-open-a-tab) (2认同)

rie*_*333 24

我认为这也符合您的要求,但它更短,并且不像浏览器那样:

do shell script "open http://www.webpagehere.com"
Run Code Online (Sandbox Code Playgroud)

这将在默认浏览器中打开指定的URL.如果您明确要在Safari中打开它,请使用以下命令:

do shell script "open -a Safari 'http://www.webpagehere.com'"
Run Code Online (Sandbox Code Playgroud)


Lri*_*Lri 8

这应该通常创建一个新选项卡并将其聚焦(或者如果URL已经打开,则聚焦现有选项卡):

tell application "Safari"
    open location "http://stackoverflow.com"
    activate
end tell
Run Code Online (Sandbox Code Playgroud)

如果"将选项卡中的打开页面而不是窗口"设置为"永不",则会打开一个新窗口.

tell application "System Events" to open location 不适用于包含非ASCII字符的某些URL:

set u to "http://ja.wikipedia.org/wiki/??"
tell application "System Events" to open location u
--tell application "Safari" to open location u
--do shell script "open " & quoted form of u
Run Code Online (Sandbox Code Playgroud)

即使将新页面设置为在Windows中打开,也会打开一个新选项卡:

tell application "Safari"
    activate
    reopen
    tell (window 1 where (its document is not missing value))
        if name of its document is not "Untitled" then set current tab to (make new tab)
        set index to 1
    end tell
    set URL of document 1 to "http://stackoverflow.com"
end tell
tell application "System Events" to tell process "Safari"
    perform action "AXRaise" of window 1
end tell
Run Code Online (Sandbox Code Playgroud)

set index to 1不提升窗口,但它使窗口显示为window 1系统事件,它可以AXRaise.


Pas*_*cal 5

我一直在使用以下脚本在单个窗口中将数百个文档打开到选项卡中。

tell application "Safari"
    tell window 1
        make new tab with properties {URL:"http://foo.com/bar"}
        make new tab with properties {URL:"http://foo.com/baz"}
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

但这在 Lion 上的 Safari 5.1 中不再适用。它会打开新选项卡,但不会加载属性全局中提供的 URL。我将其修改为以下内容,现在可以使用了:

tell application "Safari"
    tell window 1
        set URL of (make new tab) to "http://foo.com/bar"
        set make new tab to "http://foo.com/baz"
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)