AppleScript无法访问SecurityAgent的元素

时间:2018-08-28 18:28:43

标签: macos applescript

我正在尝试编写一个AppleScript,以在窗口提示中输入管理员密码,但是该脚本找不到进程“ SecurityAgent”的任何元素

这是我用来检索“ SecurityAgent”下所有项目及其结果的诊断脚本,没什么。

我从Accessibility Inspector验证了该窗口确实在“ SecurityAgent”进程下

Diagnostic Code and Result

如果我尝试访问窗口1的任何元素,结果将是:

   "Can't get window 1 of process \"SecurityAgent\". Invalid index." number -1719 from window 1 of process "SecurityAgent"

2 个答案:

答案 0 :(得分:0)

一种可靠的方法是等待进程SecurityAgent开始

tell application "System Events"
    delay 0.5
    repeat until exists process "SecurityAgent"
        delay 0.5
    end repeat
    tell process "SecurityAgent"
        // do something
    end tell
end tell

答案 1 :(得分:0)

我遇到了同样的问题,对我来说,这似乎是一个错误。

奇怪的是,当使用辅助功能检查器检查应用程序时,它具有0个窗口,但是尽管如此,AXMainWindow和AXFocusedWindow的值仍指向可见窗口。

Screenshot of Accessibility Inspector

不幸的是,以下Applescript会为变量missing value产生theWindow

tell application "System Events"
    set theWindow to the value of attribute "AXMainWindow" of process "SecurityAgent"
end tell

在极少数情况下,窗口是已知的,并且theWindow取值为window 1 of process "SecurityAgent"

好消息是,似乎有一种解决方法:单击窗口后,AppleScript可以找到它!

最简单的解决方法是手动单击窗口。

当然,也应该可以自动化... 由于某些原因,以下代码对我不起作用:

tell application "System Events"
    tell process "SecurityAgent"
        activate
        set frontmost to true
        click at {200, 250}
    end tell
end tell

幸运的是,以下代码确实有效:

tell application "System Events"
    AST click at {200, 250}
end tell

但是,这确实需要您安装Applescript Toolbox,它仅适用于10.13以下的macOS。从macOS 10.14开始,不再支持第三方Applescript库。