Open URL in new Safari tab with AppleScript

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

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

回答1:

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 


回答2:

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'" 


回答3:

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.



回答4:

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.



回答5:

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 


回答6:

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 


回答7:

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



回答8:

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



回答9:

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.



回答10:

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 


回答11:

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!