AppleScript to remove duplicates

I have 2 scripts and they both work individually, but when I try to merge them, it's not working.

Script 1:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set listOfItems to "Tiago
Paulo
Sergio
Tiago"

-- List of names
set nameList to paragraphs of listOfItems

-- Initialize an empty list to store formatted names
set formattedNames to {}

-- Iterate through each name and format it
repeat with aName in nameList
	set end of formattedNames to "\"" & aName & "\""
end repeat

-- Combine the formatted names into a single string with commas between them
set AppleScript's text item delimiters to ","
set formattedText to formattedNames as string
set AppleScript's text item delimiters to ""

So I can see the output as
"Tiago","Paulo","Sergio","Tiago"
... and that's what I want.

Script 2

This was copied from MacScripter.

set formattedText to {"Tiago", "Paulo", "Pedro", "Tiago"}

removeDups(formattedText)

to removeDups(l)
	script foo
		property foo2 : l
		property okAddresses : {}
	end script
	
	considering case
		repeat with i from 1 to count (foo's foo2)
			set formattedText to ((foo's foo2)'s item i)
			if formattedText is in foo's okAddresses then
			else
				set end of foo's okAddresses to formattedText
			end if
		end repeat
	end considering
	foo's okAddresses
end removeDups

I can see in Script Debugger that the duplicated name is removed, as expected.

Now when I try to merge them by replacing the list of names in Script 2, by the variable formattedText in Script 1, it doesn't work

Final, merged script

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set listOfItems to "Tiago
Paulo
Sergio
Sara"

-- List of names
set nameList to paragraphs of listOfItems

-- Initialize an empty list to store formatted names
set formattedNames to {}

-- Iterate through each name and format it
repeat with aName in nameList
	set end of formattedNames to "\"" & aName & "\""
end repeat

-- Combine the formatted names into a single string with commas between them
set AppleScript's text item delimiters to ","
set formattedText to formattedNames as string
set AppleScript's text item delimiters to ""

removeDups(formattedText) --> {"a@a.e", "b@b.c", "d@a.h"}

to removeDups(l)
	script foo
		property foo2 : l
		property okAddresses : {}
	end script
	
	considering case
		repeat with i from 1 to count (foo's foo2)
			set formattedText to ((foo's foo2)'s item i)
			if formattedText is in foo's okAddresses then
			else
				set end of foo's okAddresses to formattedText
			end if
		end repeat
	end considering
	foo's okAddresses
end removeDups

@ComplexPoint - do you see anything weird here?

Well as I noted in another thread:

I would tend to just write:

use framework "Foundation"

on run
    nub({"Tiago", "Paulo", "Miguel", "Tiago"})
end run


-- nub :: [a] -> [a]
on nub(xs)
    ((current application's NSArray's arrayWithArray:xs)'s ¬
        valueForKeyPath:"@distinctUnionOfObjects.self") as list
end nub

The nub function (defining a version of a list without duplicates) is defined (with a number of other general functions) at:

1 Like

Yes, but in order for me to learn a bit more and understand why this is not working, do you see anything that doesn't make sense when it's all merged and not working, versus each version working separately?

removeDups() expects a list, but the last part of script 1 explicitly converts your list to a string. Assuming your final output should be a formatted and de-duped string, in your combined script you need to removeDups() before you convert the list to a string.

I've used a ,<space> separator in the output rather than your bare comma -- mainly to show it's easy to do :wink:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set listOfItems to "Tiago
Paulo
Sergio
Sara
Tiago"

-- List of names
set nameList to paragraphs of listOfItems

-- Initialize an empty list to store formatted names
set formattedNames to {}

-- Iterate through each name and format it
repeat with aName in nameList
	set end of formattedNames to "\"" & aName & "\""
end repeat

removeDups(formattedNames)

set AppleScript's text item delimiters to ", "

return formattedNames as string


to removeDups(l)
	script foo
		property foo2 : l
		property okAddresses : {}
	end script
	
	considering case
		repeat with i from 1 to count (foo's foo2)
			set formattedText2 to ((foo's foo2)'s item i)
			if formattedText2 is in foo's okAddresses then
			else
				set end of foo's okAddresses to formattedText2
			end if
		end repeat
	end considering
	foo's okAddresses
end removeDups

Another improvement would be to removeDups() before you format your list -- why waste CPU cycles wrapping "s around something you're about to delete?