Search code examples
applescriptcase-sensitive

Make AppleScript Case-Sensitive


In the following script,

set theText to "ASDF"
set lowercase to "abcdefghijklmnopqrstuvwxyz"
set numeric to "0123456789"

set containsLowercase to false
set containsUppercase to false
set containsNumber to false
set containsSomethingElse to false

repeat with theCharacter in theText
  if lowercase contains theCharacter then
      set containsLowercase to true
  else if uppercase contains theCharacter then
      set containsUppercase to true
  else if numeric contains theCharacter then
      set containsNumber to true
  else
      set containsSomethingElse to true
  end if
end repeat

if containsLowercase and (containsNumber or containsSomethingElse) then
    if containsNumber and containsSomethingElse then
        say "The text contains a mix of lowercase, numbers, and something else."
else if containsNumber then
    say "The text contains a mix of lowercase and numbers."
else
    say "The text contains a mix of lowercase and something non-numeric."
end if
else if containsLowercase then
    say "The text contains only lowercase characters."
else if containsNumber and containsSomethingElse then
    say "The text contains a mix of numbers and something non-alphabetic."
else if containsNumber then
    say "The text contains only numbers."
else if containsSomethingElse then
    say "The text contains only non-numeric and non-alphabetic characters."
else
    say "I think there is something wrong with my logic, Dave."
end if

Even though theText is only capitals, it still thinks it contains the lowercase alphabet defined with the variable lowercase. I read somewhere that AppleScript is case-insensitive. I'm not sure if this is possible, but I want it to be case-sensitive. Any way to do that?


Solution

  • You can use the "considering" statement.

    considering case
        if theAlphabet contains theCharacter then
            set containsLowerAlpha to true
        end if
        if theUpperAlphabet contains theCharacter then
            set containsUpperAlpha to true
        end if
    end considering
    

    More about considering at the Apple documentation for control statements.