如何在苹果脚本中制作对话框以等待用户输入

How to make dialogue box in apple script to wait until user gives in put

我正在尝试使用苹果脚本在 mac 中获得一个对话框。

    tell application "System Events"
    activate
    display dialog "Enter your name:  " default answer "" buttons {"OK"}   default button "OK" with title "Good Name"
    set the Name to text returned of the result

我面临的问题是,当我不输入名称时,弹出窗口自行关闭并抛出错误。但我希望它在用户输入之前一直存在

您需要添加一个 repeat 来处理错误:

set theMessage to ""
set theIcon to note

tell application "System Events"
    activate
    repeat
        display dialog theMessage & "Enter your name:  " default answer "" buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel" with title "Good Name" with icon theIcon
        set the theName to text returned of the result
        try
            if theName = "" then error
            exit repeat
        on error
            set theMessage to "Invalid. "
            set theIcon to caution
        end try
    end repeat
    display dialog "Your name is " & theName & "." buttons {"OK"} default button "OK" with icon note
end tell

当输入为“”时,重复 on error 被触发,这会给出一条 invalid 消息并重复输入对话框——否则脚本继续(名称输出作为示例添加)。