激活最前面的 Safari 和 运行 Javascript
Activate the frontmost Safari and run Javascript
我正在尝试在 Safari 上 运行 Javascript,最前面的是 true。
tell (application "Safari" whose frontmost is true) to do JavaScript theScript in current tab of first window
但是我得到这样的错误:
execution error: Can’t get application "System Events" whose frontmost of it = true. Access not allowed. (-1723)
我哪里弄错了?
AppleScript 通常要求以特定方式格式化语法。对于您的脚本,它无法区分标识符,因为您将它们错误地放在同一行上。
set theScript to "alert('Hello, World!');"
tell application "Safari"
set frontmost to true
activate
do JavaScript theScript in current tab of first window
end tell
您可以通过编译和 运行 在 AppleScript 编辑器中测试您的脚本。 events
、replies
和 results
应该会给您一些指示,指出问题可能出在哪里。
编辑:处理最前面(或最近)的进程:*
set theApp to "Safari"
set theScript to "alert('Hello, World!');"
on isOpen(appName)
tell application "System Events" to (name of processes) contains appName
end isOpen
set isActive to isOpen(theApp)
if isActive then
tell application "System Events" to tell (process "Safari" whose frontmost is true)
tell application "Safari"
activate
tell application "System Events" to set frontSafari to item 1 of (get name of processes whose frontmost is true)
if (count windows) is 0 then
display dialog "There are no " & theApp & " windows open." buttons {"OK"}
else if frontSafari is "Safari" then
tell application "Safari" to do JavaScript theScript in current tab of first window
end if
end tell
end tell
else
display dialog theApp & " is not currently running." buttons {"OK"}
end if
我正在尝试在 Safari 上 运行 Javascript,最前面的是 true。
tell (application "Safari" whose frontmost is true) to do JavaScript theScript in current tab of first window
但是我得到这样的错误:
execution error: Can’t get application "System Events" whose frontmost of it = true. Access not allowed. (-1723)
我哪里弄错了?
AppleScript 通常要求以特定方式格式化语法。对于您的脚本,它无法区分标识符,因为您将它们错误地放在同一行上。
set theScript to "alert('Hello, World!');"
tell application "Safari"
set frontmost to true
activate
do JavaScript theScript in current tab of first window
end tell
您可以通过编译和 运行 在 AppleScript 编辑器中测试您的脚本。 events
、replies
和 results
应该会给您一些指示,指出问题可能出在哪里。
编辑:处理最前面(或最近)的进程:*
set theApp to "Safari"
set theScript to "alert('Hello, World!');"
on isOpen(appName)
tell application "System Events" to (name of processes) contains appName
end isOpen
set isActive to isOpen(theApp)
if isActive then
tell application "System Events" to tell (process "Safari" whose frontmost is true)
tell application "Safari"
activate
tell application "System Events" to set frontSafari to item 1 of (get name of processes whose frontmost is true)
if (count windows) is 0 then
display dialog "There are no " & theApp & " windows open." buttons {"OK"}
else if frontSafari is "Safari" then
tell application "Safari" to do JavaScript theScript in current tab of first window
end if
end tell
end tell
else
display dialog theApp & " is not currently running." buttons {"OK"}
end if