Removing duplicates in a AppleScript list or record

Posted in AppleScript on January 17, 2019 · 1 min read
Removing duplicate entries in a list or record using AppleScript is not a complicated task.

Say we were given a list of strings in AppleScript that has duplicate items:

{"apple", "orange", "apple", "watermelon", "pear", "strawberry"}

First we open Script Editor to a new document. Save the script as remove_duplicate.scpt. In the document we set the list as a variable:

set oldList to {"apple", "orange", "apple", "watermelon", "pear", "strawberry"}

One of the easiest ways to go about modifying a list or record is to create a new list or record. We need to declare a variable as an empty list or record so we can push to it later:

set oldList to {"apple", "orange", "apple", "watermelon", "pear", "strawberry"}
set newList to {}

Using a repeat we step through each item of the list oldList:

set oldList to {"apple", "orange", "apple", "watermelon", "pear", "strawberry"}
set newList to {}

repeat with fruit in oldList

end repeat

Now we need to know if the newList contains the item fruit:

repeat with fruit in oldList
    if newList does not contain contents of fruit then
        return true
    end if
end repeat

Implementing D.R.Y. in our scripting we can change the conditional to a one liner:

repeat with fruit in oldList
    if newList does not contain contents of fruit then return true
end repeat

Test running the code we should get back a return of true. The true return is not what we are after so remove return true and paste copy (contents of fruit) as text to end of the newList:

set oldList to {"apple", "orange", "apple", "watermelon", "pear", "strawberry"}
set newList to {}

repeat with fruit in oldList
    if newList does not contain contents of fruit then copy (contents of fruit) as text to end of the newList
end repeat

Then we need to return the list to see what we have:

set oldList to {"apple", "orange", "apple", "watermelon", "pear", "strawberry"}
set newList to {}

repeat with fruit in oldList
    if newList does not contain contents of fruit then copy (contents of fruit) as text to end of the newList
end repeat

return newList

In the Result of Script Editor we should see a new list without any duplicates:

{"apple", "orange", "watermelon", "pear", "strawberry"}

Our code could be cleaned a little further with setting the contents of the fruit item to a variable:

repeat with fruit in oldList
    set fruitItem to contents of fruit as text
    if newList does not contain fruitItem then copy fruitItem to end of the newList
end repeat
Help the site go ad free:
Storm Trooper