如何验证 AppleScript 中的用户输入

How to validate user input in AppleScript

这是我第一次使用 AppleScript,如何添加验证以使用户无法在输入字段中提交空白信息?

firstname = """
display dialog "What is your name?" default answer "" ¬
buttons {"Submit"}
"""

这是一个如何确保用户输入内容的示例:

set textReturned to ""
repeat while textReturned is equal to ""
    set textReturned to text returned of (display dialog ¬
        "What is your name?" buttons {"Submit"} ¬
        default button 1 default answer "")
end repeat

如果您想确保用户键入的不是 space 字符(或粘贴到 tab字符)然后添加,例如:

if (do shell script "sed 's/[[:blank:]]//g'<<<" & ¬
    textReturned's quoted form) ¬
    is equal to "" then set textReturned to ""

那就是,例如:

set textReturned to ""
repeat while textReturned is equal to ""
    set textReturned to text returned of (display dialog ¬
        "What is your name?" buttons {"Submit"} ¬
        default button 1 default answer "")
    if (do shell script "sed 's/[[:blank:]]//g'<<<" & ¬
        textReturned's quoted form) ¬
        is equal to "" then set textReturned to ""
end repeat