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

时间:2019-05-27 15:11:28

标签: applescript

我有一个简单的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

结束告诉

1 个答案:

答案 0 :(得分:1)

删除封闭的Finder 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