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:37

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.

查看更多
别忘想泡老子
3楼-- · 2020-02-08 03:39

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"})

查看更多
甜甜的少女心
4楼-- · 2020-02-08 03:43

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'"
查看更多
叼着烟拽天下
5楼-- · 2020-02-08 03:43

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
查看更多
干净又极端
6楼-- · 2020-02-08 03:46

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
查看更多
forever°为你锁心
7楼-- · 2020-02-08 03:50

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.

查看更多
登录 后发表回答