0
votes

I have a simple applescript, compiled as application, that allows user to choose between restart, shut down, etc.

It's very simple and works fine, but the confirmation dialog sent by the OS "are you sure you want to shut down your computer now" with the timer is not the frontmost window, so user have to click inside this dialog to activate, and then click the button he wants.

If this confirmation dialog would be the frontmost window, a simple enter key would confirm the choice. Any idea about how to bring this confirmation dialog to top?

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

end tell

1

1 Answers

1
votes

Remove the enclosing Finder tell block and activate the window with GUI scripting

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