这个简单的Applescript按预期工作:
tell application "Safari"
activate
set theURL to URL of front document
end tell
我现在尝试对它进行Automator操作/服务:
on run {input, parameters}
tell application "System Events"
if (name of the first process whose frontmost is "true") is "Safari" then
set theURL to URL of front document
end if
end tell
return theURL
end run
但返回错误消息:
操作“运行AppleScript”遇到错误:“系统事件发生错误:无法获取文档1的URL。”
嗯,所以我在Automator中添加了一个< code >告诉应用程序“Safari”激活,以确保真的是Safari接收这些命令:
on run {input, parameters}
tell application "System Events"
if (name of the first process whose frontmost is "true") is "Safari" then
tell application "Safari"
activate
set theURL to URL of front document
end tell
end if
end tell
return theURL
end run
奇怪的是,现在“自动操作”操作卡在 0 % 上(如菜单栏中的“自动机”齿轮所示)。为了使脚本完成,我必须按 Esc
。
事实上,如果我在停留在0%时退出Safari,当我试图切换回Safari时,Safari会一直处于后台状态,直到我按Esc。按下Esc键后,Safari将自动预接地,Automator操作结束。
即:
最后,我已将“自动操作”操作设置为替换
那么,为什么我无法访问第一个版本中的 URL?为什么它会卡在第二个版本中?我应该如何写这个来完成我想要的?
编辑:澄清我正在努力实现的目标。我想从Safari中最前面的窗口获取URL,处理该URL,然后在其他地方使用它。为了简化这个问题,我已经删除了Automator工作流程中的处理和“其他地方”部分。
发生此错误是因为您在无法工作的System Events
告诉块中运行Safari术语。
事实上,没有必要把Safari放在最前面。这条线应该能胜任这项工作。
on run {input, parameters}
tell application "Safari" to return URL of front document
end run
更强大的版本检查前面的文档是否存在
on run {input, parameters}
tell application "Safari"
if exists document 1 then return URL of front document
return missing value
end tell
end run
如果要使“
自动操作”服务集服务接收到无输入
您可以执行以下操作来确定URL以及URL来自哪个应用程序/浏览器。
下面要点中的代码为了便于理解而故意冗长,此外,我不知道你到底想实现什么。然而,它为您提供了实现您想做的事情的一般要点。
它执行以下操作:
theURL
和哪个浏览器
保存您需要的信息。on run {input, parameters}
tell application "System Events" to set theActiveApp to name of first process where it is frontmost
set theURL to "unknown"
set whichBrowser to "unknown"
if theActiveApp is equal to "Safari" then
tell application "Safari"
if exists document 1 then
set theURL to URL of current tab of window 1
set whichBrowser to theActiveApp
else
display dialog "This service requires active window in Safari with a URL" & return & return & "Try opening a new window." with icon caution
return
end if
end tell
else if theActiveApp is equal to "Google Chrome" then
tell application "Google Chrome"
if exists window 1 then
set theURL to URL of active tab of window 1
set whichBrowser to theActiveApp
else
display dialog "This service requires active window in Chrome with a URL" & return & return & "Try opening a new window." with icon caution
return
end if
end tell
end if
-- The Service was NOT run from either Safari or Google Chrome
if theURL = "unknown" and whichBrowser = "unknown" then
tell application "Finder"
activate
display dialog "This Service must be run from either Safari or Google Chrome" & theActiveApp with icon caution
end tell
else
-- Report the results
display dialog "The url is: " & theURL & return & return & "From app: " & theActiveApp
end if
-- ONLY return theURL to the next task if we've got it.
if theURL ≠ "unknown" then
return theURL
end if
end run
注意
最后一部分内容如下:
if theURL ≠ "unknown" then
return theURL
end if
如果theURL
未知,这将导致工作流停止运行(即服务未从Safari/Chrome或浏览器中未打开窗口)。否则它将把theURL
传递给下一个Automator任务。
只有必要对话框的脚本版本
下面的此版本将仅显示必要的对话框,并且不会在对话框中报告URL和哪些浏览器
变量。
on run {input, parameters}
tell application "System Events" to set theActiveApp to name of first process where it is frontmost
set theURL to missing value
if theActiveApp is equal to "Safari" then
tell application "Safari"
if exists document 1 then
set theURL to URL of current tab of window 1
else
display dialog "This service requires active window in Safari with a URL" & return & return & "Try opening a new window." with icon caution
return -- return early
end if
end tell
else if theActiveApp is equal to "Google Chrome" then
tell application "Google Chrome"
if exists window 1 then
set theURL to URL of active tab of window 1
else
display dialog "This service requires active window in Chrome with a URL" & return & return & "Try opening a new window." with icon caution
return -- return early
end if
end tell
end if
if theURL is equal to missing value then
-- The Service was NOT run from either Safari or Google Chrome
tell application "Finder"
activate
display dialog "This Service must be run from either Safari or Google Chrome" & theActiveApp with icon caution
end tell
else
-- ONLY return theURL to the next Automator task if we've got it.
return theURL
end if
end run