我可以限制用户不在AppleScript的对话框中输入特殊字符吗?

时间:2013-07-17 13:16:49

标签: macos applescript osx-lion osx-mountain-lion

我有一个要求,我必须限制用户输入他要输入内容的对话框中的特殊字符。我想只允许数字和字符。谁能告诉我是否可能?

代码如下:

display dialog "Enter You Name: " default answer "Name" buttons{"Proceed"}

在默认答案位置,用户不应输入任何特殊字符。我可以通过这样做来限制他吗?

1 个答案:

答案 0 :(得分:3)

您可以尝试这样的事情:

set allowedLetters to characters of (do shell script "printf \"%c\" {a..z}")
set allowedNumbers to characters of (do shell script "printf \"%c\" {0..9}")
set allowedAll to allowedLetters & allowedNumbers & space

repeat
    set response to text returned of (display dialog "Enter You Name: " default answer "Name" buttons {"Proceed"} default button 1)
    try
        repeat with aCharacter in response
            if (aCharacter as text) is not in allowedAll then
                display dialog "Please enter letters and numbers only" buttons {"OK"} cancel button 1 default button 1
            end if
        end repeat
        exit repeat
    end try
end repeat

return response