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

发布于 2024-09-02 14:09:39 字数 46 浏览 8 评论 0原文

是否可以使用 AppleScript 在 Safari 的新选项卡中打开链接?

Is it possible to use AppleScript to open a link in a new tab in Safari?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

云醉月微眠 2024-09-09 14:09: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

This will work:

tell application "Safari"
    tell window 1
        set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
    end tell
end tell
肤浅与狂妄 2024-09-09 14:09:39

我认为这也满足您的要求,但它更短,并且不太特定于浏览器:

do shell script "open http://www.webpagehere.com"

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

do shell script "open -a Safari 'http://www.webpagehere.com'"

I think this also does what you asked for, but it is much shorter and is less browser-specific:

do shell script "open http://www.webpagehere.com"

This will open the specified URL in your default browser. And if you explicitly want to open it in Safari, use this:

do shell script "open -a Safari 'http://www.webpagehere.com'"
望喜 2024-09-09 14:09:39

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

tell application "Safari"
    open location "http://stackoverflow.com"
    activate
end tell

如果“在选项卡而不是窗口中打开页面”设置为“从不”,则会打开一个新窗口。

告诉应用程序“系统事件”打开位置不适用于某些包含非 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

即使新页面设置为在 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

set索引为 1 不会提升窗口,但它使窗口在系统事件中显示为窗口 1,系统事件可以AXRaise 它。

This should usually create a new tab and focus it (or focus an existing tab if the URL is already open):

tell application "Safari"
    open location "http://stackoverflow.com"
    activate
end tell

It opens a new window if "Open pages in tabs instead of windows" is set to never though.

tell application "System Events" to open location doesn't work with some URLs that contain non-ASCII characters:

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

This opens a new tab even when new pages are set to open in 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

set index to 1 doesn't raise the window, but it makes the window appear as window 1 to System Events, which can AXRaise it.

暗地喜欢 2024-09-09 14:09:39

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

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

但这在 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

I've been using the following script to open hundreds of docs into tabs in a single window.

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

But that no longer works in Safari 5.1 on Lion. It would open the new tab, but it wouldn't load the URL provided in the properties glob. I modified it to the following, which now works:

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
能怎样 2024-09-09 14:09:39

自从在这里发布新答案以来已经有一段时间了。我认为这是实现这一目标的最佳方式。如果 Safari 未打开,它将打开它;如果没有打开窗口,它将创建一个新窗口,并将选项卡添加到当前(或新创建的)窗口。

tell application "Safari"
    activate
    try
        tell window 1 to set current tab to make new tab with properties {URL:theURL}
    on error
        open location theURL
    end try
end tell

It's been a while since a new answer's been posted here. I think this is the optimal way to do this. It will open Safari if it's not open, create a new window if there are no windows open, and add the tab to the current (or newly created) window.

tell application "Safari"
    activate
    try
        tell window 1 to set current tab to make new tab with properties {URL:theURL}
    on error
        open location theURL
    end try
end tell
两个我 2024-09-09 14:09:39

代码:

tell application "System Events"
tell application "Safari" to activate
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell

tell application "Safari"
set URL of document 1 to "http://www.stackoverflow.com/"
end tell

一个问题是,这仅在系统语言设置为英语时才有效。

Code:

tell application "System Events"
tell application "Safari" to activate
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell

tell application "Safari"
set URL of document 1 to "http://www.stackoverflow.com/"
end tell

One problem is that this only works if the system's language is set to English.

锦爱 2024-09-09 14:09:39

我无法发表评论 :-/ 所以我会回答说 Tim 的答案(上面)从 OS X 10.8.5 开始有效。他的脚本的这一行版本也有效:

tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})

啊——一行已经溢出了。这里没有代码标签:

告诉应用程序“Safari”的窗口 1 将当前选项卡设置为(使用属性 {URL:"http:// /www.stackoverflow.com"})

I can't comment :-/ so I will answer to say that Tim's answer (above) works as of OS X 10.8.5. This one-line version of his script also works:

tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})

Arrgh -- the one line is overflowing. Here it is without the code tags:

tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})

神仙妹妹 2024-09-09 14:09:39

我最终使用 automator 来完成此操作,这更容易并且有效。

I ended up using automator to do this which was much easier and it works.

当梦初醒 2024-09-09 14:09:39

您可以尝试以下方法:

//make Safari window active and topmost
tell application "Safari" to activate
//start communication with Safari
tell application "Safari"
    tell window 1
        //create new tab and open specified URL
        tab with properties {URL:"https://url.com"})
        //make tab active
        set visible to true
    end tell
end tell

您还可以在 FastScript 中结合使用 Apple 脚本(10 个快捷方式免费) )

要添加脚本 - 只需将脚本保存在 /Library/Scripts 中。之后您将能够为新脚本设置一些快捷方式。

如果您想打开新窗口而不是新选项卡,您可以在下一个中播放:

tell application "System Events"
    tell process "Safari"
        click menu item "New window" of menu "File" of menu bar 1
    end tell
end tell

注意:在这种情况下,您需要允许 AppleScript 在安全设置中使用特殊功能。

You can try following approach:

//make Safari window active and topmost
tell application "Safari" to activate
//start communication with Safari
tell application "Safari"
    tell window 1
        //create new tab and open specified URL
        tab with properties {URL:"https://url.com"})
        //make tab active
        set visible to true
    end tell
end tell

Also u can combine usage of Apple script within FastScript (free for 10 shortcut)

To add your script - just save script in /Library/Scripts. After you will be able to set some shortcut for new script.

If you want to open new Window than new tab u can play within next:

tell application "System Events"
    tell process "Safari"
        click menu item "New window" of menu "File" of menu bar 1
    end tell
end tell

Note: you need to allow AppleScript to use specialCapabilities in security settings in this case.

御弟哥哥 2024-09-09 14:09:39

这不是最短的解决方案,但也有效,而且不仅是英语......

tell application "Safari"
    activate
end tell

tell application "System Events"
    set frontmost of process "Safari" to true
    keystroke "t" using {command down}
end tell

set myURL to "anyurl.html"
delay 2
tell application "Safari" to set the URL of the front document to myURL

Not the shortest solution but also works, and not only in English ...

tell application "Safari"
    activate
end tell

tell application "System Events"
    set frontmost of process "Safari" to true
    keystroke "t" using {command down}
end tell

set myURL to "anyurl.html"
delay 2
tell application "Safari" to set the URL of the front document to myURL
南街女流氓 2024-09-09 14:09:39

在 Safari v.11 中为我工作

tell application "Safari"
    tell window 1
        make new tab with properties {URL:"https://twitter.com"}
    end tell
end tell

Worked for me in Safari v.11

tell application "Safari"
    tell window 1
        make new tab with properties {URL:"https://twitter.com"}
    end tell
end tell
够运 2024-09-09 14:09:39

我找到了一种使用 Safari 在后台打开新选项卡的方法。

tell application "Safari"

set the URL of (make new tab in window 1) to "your.url.net"

end tell

在我写这个答案的时候,我做了这个

tell application "Safari"

try

    display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1

    set theURL to text returned of result

    set netProto to "https://"

    if theURL contains netProto then

        set the URL of (make new tab in window 1) to theURL

    else

        set the URL of (make new tab in window 1) to netProto & theURL

    end if

end try

end tell

新版本

tell application "Safari"

repeat

    try

        display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1

        set theURL to text returned of result

        if theURL is "" then exit repeat

        set netProto to "https://"

        if theURL contains netProto then

            set the URL of (make new tab in window 1) to theURL

        else

            set the URL of (make new tab in window 1) to netProto & theURL

        end if

        display dialog "Do you want to open a new tab?" buttons {"Yes", "No"} default button "Yes"

        if button returned of result is "No" then exit repeat

    end try

end repeat

end tell

任何建议将不胜感激

最好的问候

I found a way to open a new tab in the background with Safari.

tell application "Safari"

set the URL of (make new tab in window 1) to "your.url.net"

end tell

During the time I wrote this answer I made this

tell application "Safari"

try

    display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1

    set theURL to text returned of result

    set netProto to "https://"

    if theURL contains netProto then

        set the URL of (make new tab in window 1) to theURL

    else

        set the URL of (make new tab in window 1) to netProto & theURL

    end if

end try

end tell

New version

tell application "Safari"

repeat

    try

        display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1

        set theURL to text returned of result

        if theURL is "" then exit repeat

        set netProto to "https://"

        if theURL contains netProto then

            set the URL of (make new tab in window 1) to theURL

        else

            set the URL of (make new tab in window 1) to netProto & theURL

        end if

        display dialog "Do you want to open a new tab?" buttons {"Yes", "No"} default button "Yes"

        if button returned of result is "No" then exit repeat

    end try

end repeat

end tell

Any suggestions will be appreciate

Best regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文