是否可以使用AppleScript在Safari的新标签页中打开链接?
答案 0 :(得分:36)
这将有效:
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
end tell
end tell
答案 1 :(得分:22)
我认为这也符合您的要求,但它更短,并且不像浏览器那样:
do shell script "open http://www.webpagehere.com"
这将在默认浏览器中打开指定的URL。如果你明确想在Safari中打开它,请使用:
do shell script "open -a Safari 'http://www.webpagehere.com'"
答案 2 :(得分:7)
这通常应创建一个新选项卡并将其聚焦(或者如果URL已经打开则聚焦现有选项卡):
tell application "Safari"
open location "http://stackoverflow.com"
activate
end tell
如果“将选项卡中的打开页面而不是窗口”设置为“永远”,则会打开一个新窗口。
tell application "System Events" to open location
不适用于包含非ASCII字符的某些网址:
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 index to 1
不会提升窗口,但会使窗口显示为系统事件的window 1
,可以AXRaise
。
答案 3 :(得分:3)
代码:
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
一个问题是,只有在系统的语言设置为英语时,这才有效。
答案 4 :(得分:3)
我一直在使用以下脚本在一个窗口中打开数百个文档到标签中。
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中不再有效。它将打开新选项卡,但它不会加载属性glob中提供的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
答案 5 :(得分:2)
自从这里发布了一个新答案以来已经有一段时间了。我认为这是实现这一目标的最佳方式。如果没有打开,它将打开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
答案 6 :(得分:0)
我无法发表评论: - /所以我会回答说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"})
Arrgh - 一条线溢出。这里没有代码标签:
告诉应用程序“Safari”的窗口1将当前标签设置为(制作包含属性的新标签{URL:“http://www.stackoverflow.com”})
答案 7 :(得分:0)
我最终使用automator来做到这一点,这更容易,它的工作原理。
答案 8 :(得分:0)
您可以尝试以下方法:
//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在安全设置中使用specialCapabilities。
答案 9 :(得分:0)
不是最短的解决方案,但也有效,而且不仅仅是英文......
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
答案 10 :(得分:0)
在Safari v.11中为我工作
tell application "Safari"
tell window 1
make new tab with properties {URL:"https://twitter.com"}
end tell
end tell
答案 11 :(得分:0)
我找到了一种使用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
任何建议将不胜感激
最诚挚的问候