我试图在Safari上运行Javascript,其前端是真的。
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)
我犯了错误?
答案 0 :(得分:2)
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