Open URL in new Safari tab with AppleScript

2020-02-08 03:11发布

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

12条回答
我命由我不由天
2楼-- · 2020-02-08 03:58

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
查看更多
该账号已被封号
3楼-- · 2020-02-08 03:58

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
查看更多
小情绪 Triste *
4楼-- · 2020-02-08 03:58

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
查看更多
SAY GOODBYE
5楼-- · 2020-02-08 03:58

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

查看更多
混吃等死
6楼-- · 2020-02-08 04:01

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.

查看更多
家丑人穷心不美
7楼-- · 2020-02-08 04:02

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

查看更多
登录 后发表回答