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

How to have Applescript shut down confirmation dialog in frontmost

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

它非常简单并且工作正常,但是 OS 发送的确认对话框“您确定要立即关闭计算机吗”带有计时器不是最前面的window,所以用户必须在这个对话框内点击激活,然后点击他想要的按钮。

如果此确认对话框位于最前面 window,则只需按回车键即可确认选择。关于如何将此确认对话框置于顶部的任何想法?

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

结束讲述

删除封闭的 Finder 告诉块并使用 GUI 脚本激活 window

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