首页 > 解决方案 > 如何在 AppleScript 中验证用户输入

问题描述

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

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

标签: python-3.xapplescript

解决方案


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

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

如果您想确保用户键入的内容不是空格字符(或粘贴在制表符中),请添加,例如:

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

推荐阅读