对话框中的对话框:第二个对话框不起作用

时间:2014-02-11 17:07:53

标签: applescript

我尝试编写一个脚本,当我选择“睡眠”选项时,会出现一个对话框询问“睡眠,重新启动或关机”并带有一个额外的对话框。

重启和关闭工作完美但当我使用睡眠按钮弹出额外的对话框时,它们不起作用。

set question to display dialog "Sleep, Restart, or Shutdown?" buttons {"Sleep", "Restart", "Shutdown"} with title "What do you want to do?"
set answer to button returned of question

if answer is equal to "Restart" then
    tell application "Finder" to restart
end if

if answer is equal to "Shutdown" then
    tell application "Finder" to shutdown
end if

if answer is equal to "Sleep" then
    display dialog "Exit All and Sleep or Just Sleep?" buttons {"Exit All", "Just Sleep"}
end if

if answer is equal to "Exit All" then
    tell application "Finder" to run application "/Users/kjoesting/Desktop/Exit All and sleep.app"
else if answer is equal to "Just Sleep" then
    tell application "Finder" to sleep
end if

问题是“全部退出,睡眠或刚刚睡眠”都不会做任何事情并返回:**tell application "AppleScript Editor" display dialog "Sleep, Restart, or Shutdown?" buttons {"Sleep", "Restart", "Shutdown"} with title "What do you want to do?" --> {button returned:"Sleep"} display dialog "Exit All and Sleep or Just Sleep?" buttons {"Exit All", "Just Sleep"} --> {button returned:"Just Sleep"} end tell**

谁能告诉我哪里出错了?

1 个答案:

答案 0 :(得分:0)

这样它应该有效:

set question to display dialog "Sleep, Restart, or Shutdown?" buttons {"Sleep", "Restart", "Shutdown"} with title "What do you want to do?"
set answer to button returned of question

if answer is equal to "Restart" then
    tell application "Finder" to restart
end if

if answer is equal to "Shutdown" then
    tell application "Finder" to shut down
end if

if answer is equal to "Sleep" then

    set question2 to display dialog "Exit All and Sleep or Just Sleep?" buttons {"Exit All", "Just Sleep"}
    set answer2 to button returned of question2

    if answer2 is equal to "Exit All" then
        tell application "Finder" to run application "/Users/kjoesting/Desktop/Exit All and sleep.app"
    else if answer2 is equal to "Just Sleep" then
        tell application "Finder" to sleep
    end if

end if

这是另一个版本。我评论了这些操作并添加了日志语句。 这样就可以测试整个事情,看看它是如何工作的,无需重新启动等。取消注释相关行并删除日志语句以使其在现实生活中起作用:

display dialog "Sleep, Restart, or Shutdown?" buttons {"Sleep", "Restart", "Shutdown"} default button 1 with title "What do you want to do?"
set the button_pressed to the button returned of the result

if the button_pressed is "Sleep" then
    -- action for 1st button goes here
    log "Sleep"

    display dialog "Exit All and Sleep or Just Sleep?" buttons {"Exit All", "Just Sleep"} default button 2
    set the button_pressed to the button returned of the result
    if the button_pressed is equal to "Exit All" then
        --tell application "Finder" to run application "/Users/kjoesting/Desktop/Exit All and sleep.app"
        log "Exit All"

    else if button_pressed is equal to "Just Sleep" then
        -- tell application "Finder" to sleep
        log "Just Sleep"
    end if


else if the button_pressed is "Restart" then
    -- action for 2nd button goes here
    log "Restart"
    --tell application "Finder" to restart

else
    -- action for 3rd button goes here (Shutdown)
    log "Shut Down"
    --tell application "Finder" to shut down

end if