Verifying what is contained in a list or record using AppleScript

Posted in AppleScript on January 17, 2019 · 1 min read
AppleScript does and doesn't contain are great when evaluating what is in lists, records and text objects.

Getting Started

Writing along with this post you will need to open Script Editor. Script Editor can be found (using the operating system Sierra) at Applications > Utilities > Script Editor.app.

List and Records

Knowing what is in a list or record using AppleScript you can could tell the editor to return a boolean result of true or false if the item is found. Given a simple record of:

{(1, 2, 3, 4, 5)}

Numerical existence

we can use contains to see if the number five exists and get back a result of true:

{(1, 2, 3, 4, 5)} contains {5}

Results:
true

Range failure checking

If you were to try to code a range of {(1, 5)} on the list you would get back a false:

{1, 2, 3, 4, 5} contains {1,5}

Result:
false

because {1,5} do not contain the remaining numbers. To verify if the list does not contain an item it would be:

{1, 2, 3, 4, 5} does not contain {5}

Result:
false

Doesn't and Does not substitution

When scripting for does not contain you can substitute it with doesn't contain but when the script is compiled, the editor will convert it to does not. A common way to utilize the contain operator is within a conditional like:

set myList to {"hello", "this", "is", "a", "test"}

if myList contains {"test"} then
  display dialog "You've shown a test"
end if

Strings and integers in a list or record

As seen above, a list or record with a string is defined using quotation marks. You could code a boolean (true or false) or an integer (1, 2, 3) as a string but when asking if it's contained you could get an incorrect return, for example:

{1, 2, 3, 4, "5"} does not contain {5}

Result:
true

Integer existence regardless of class

The result is true even though there is a 5 in the list because the list's 5 is a string ("5") and the does not contain {5} is checking for an integer. One way to go about testing a list for a string wether that number is in string or integer format is with a conditional and a try/catch block:

set testList to {"1", 2, 3, 4, "5"}
set findMe to 2

repeat with aItem in the testList
if class of contents of aItem = text then
try
set aItem to aItem as integer
end try
end if
if contents of aItem = 5 then return "Found it! You searched for " & findMe
end repeat

Result:
Found it! You searched for 2

Explaining the above code:

Two variables are being set testList and findMe:

  • testList: contains the list to be checked
  • findMe: What is being looked for in the list
  • A repeat was created to step into testList

Stepping into each item we check that class. In AppleScript a string and text appear to both return a class of text

  • If we did find an item as text or string we try to convert it to integer in a try/catch block. The try/catch exists so the repeat will not fail if the item contains alphabetic characters and not numerical characters
  • If the item integer we declared from findMe is found we return a message to the Result.
Help the site go ad free:
Storm Trooper