如何让Applescript在最前面关闭确认对话框

如何让Applescript在最前面关闭确认对话框,applescript,Applescript,我有一个简单的applescript,编译为应用程序,允许用户在重启、关闭等之间进行选择 它非常简单,工作正常,但操作系统发送的带有计时器的确认对话框“您确定现在要关闭计算机吗”并不是最前面的窗口,因此用户必须在该对话框中单击以激活,然后单击所需的按钮 如果此确认对话框是最前面的窗口,则只需按enter键即可确认选择。你知道如何将这个确认对话框置于顶部吗 tell application "Finder" set userChoice to my getChoixUser("list") --

我有一个简单的applescript,编译为应用程序,允许用户在重启、关闭等之间进行选择

它非常简单,工作正常,但操作系统发送的带有计时器的确认对话框“您确定现在要关闭计算机吗”并不是最前面的窗口,因此用户必须在该对话框中单击以激活,然后单击所需的按钮

如果此确认对话框是最前面的窗口,则只需按enter键即可确认选择。你知道如何将这个确认对话框置于顶部吗

tell application "Finder"
set userChoice to my getChoixUser("list") -- choose from list

try
    if (userChoice contains "Veille") then -- sleep
        tell application "Finder" to sleep
    else if (userChoice contains "Eteindre") then -- shut down
        tell application "loginwindow" to «event aevtrsdn»
    else if (userChoice contains "Redémarrer") then -- restart
        tell application "loginwindow" to «event aevtrrst»
    else if (userChoice contains "économiseur") then -- screen saver
        tell application "System Events" to start current screen saver
    end if

on error errMsg
    beep
    tell application "Finder" to display dialog errMsg buttons {"OK"} default button 1 with title scriptName with icon 0
end try

结束tell

移除封闭的
查找程序
tell块,并使用GUI脚本激活窗口

set userChoice to getChoixUser("list") -- choose from list

try
    if (userChoice contains "Veille") then -- sleep
        tell application "Finder" to sleep
    else if (userChoice contains "Eteindre") then -- shut down
        tell application "loginwindow" to «event aevtrsdn»
        focusLoginWindow()
    else if (userChoice contains "Redémarrer") then -- restart
        tell application "loginwindow" to «event aevtrrst»
        focusLoginWindow()
    else if (userChoice contains "économiseur") then -- screen saver
        tell application "System Events" to start current screen saver
    end if

on error errMsg
    beep
    tell application "Finder" to display dialog errMsg buttons {"OK"} default button 1 with title scriptName with icon 0
end try

on focusLoginWindow()
    tell application "System Events" to tell process "loginwindow"
        repeat until exists window 1
            delay 0.2
        end repeat
        set attribute "AXFocused" of window 1 to true
    end tell
end focusLoginWindow

删除Finder tell块不会改变任何内容:默认的确认对话框按钮仍然为“灰色”,并且不响应enter键。事实上,只有“告诉应用程序”loginwindow“到«事件aevtrrst»并返回”,问题是相同的。对。编译为脚本和应用程序的脚本的行为不同。我更新了答案。