SecurityAgent弹出窗口无法访问窗口1(或任何此类问题)

时间:2018-03-26 15:08:17

标签: popup passwords window applescript

我正在根据自己的需要使用AppleScript编写一些简单的应用程序。我应该说我仍然是一个总要努力去理解代码的各种元素。

尝试启用服务时,让我们说使用脚本化UI操作打开服务器,在任何其他时间,都会发生安全客户端弹出窗口。为了自动执行操作,我编写了以下代码:

第1位是检查服务器是否已经打开,如果没有,请单击按钮将其打开(工作正常):

    tell application "ServerActivator" to activate
    try
tell application "System Events"
        tell process "ServerActivator"
            if text field 1 of group "IP Config" of tab group 1 of window "Server Activator" is enabled then click button "ON" of window "Server Activator"
        end tell
    end tell
on error errMsg
end try

第二位是填充SecurityAgent弹出密码

    try
    tell application "System Events"
        tell process "SecurityAgent"
            activate
            set value of text field "Password :" of window 1 to "myPassword"
            click button 2 of window 1
        end tell
    end tell
on error errMsg
end try

第二位无法正常工作,因为代码通常返回窗口1不存在。但是,当SecurityAgent弹出窗口出现时,我应该手动单击窗口,然后只有代码才能工作。显然,我希望这可以独立于任何用户操作而工作。有什么想法吗?

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

等待该流程存在,不需要try块以及activate行。

tell application "System Events"
    repeat until exists window 1 of process "SecurityAgent"
        delay 0.5
    end repeat
    tell window 1 of process "SecurityAgent"
        set value of text field 1 to "myPassword"
        keystroke return
    end tell
end tell