punctuation
AppleScript considers punctuation marks such as commas and periods in
strings by default when it compares strings for equality. If you
enclose the string comparison in the statement ignoring punctuation...end ignoring
, however, AppleScript will
ignore the punctuation when evaluating a string comparison.
punctuation
comprises the following characters in
AppleScript:
. |
? |
: |
; |
! |
\ |
' |
" |
` |
The next example uses the punctuation
constant to
ignore any commas in the numbers entered into a dialog box by a user.
If the script did not use the ignoring punctuation
statement, and the user entered a number with one or more commas
(such as 1,000,000), then the statement:
if inputNum as real ≤ 1000
would raise an error. This is because by default the strings with
nonnumber characters in them cannot be coerced to integers or
real
s (with the exception of strings like
“1.0e + 4,” which would evaluate to
the real
type in scientific notation—1.0e +
4). In the case of the following script, the punctuation marks can be
ignored, allowing “1,000,000” to be
coerced to a real
data type (which would look like
1.0e + 6). Chapter 3 discusses the
real
type.
set inputNum to text returned of (display dialog "Enter a number larger¬ than 1,000" default answer "") ignoring punctuation if inputNum as real ≤ 1000 then display dialog "Remember it has to be bigger than 1,000" else display dialog "Your number is: " & inputNum end if end ignoring