Duplicate Remover - not working

hey everyone,
Below is my applescript and I can’t it to work properly. I’m basically just altering another script found on here for my needs, but it’s not going as smoothly as I hoped.
Any help is greatly appreciated!
Thanks!


set listA to text returned of (display dialog "Enter your first list seperated by commas:" default answer "")
set listB to text returned of (display dialog "Enter your first list seperated by commas:" default answer "")


set TheList to {{listA}, {listB}}
set TheList to RemoveDuplicates(TheList)
-->{{1, 2}, {3, 4}, {2, 2}, {1, 1}}

to RemoveDuplicates(TheList)
	set newList to {}
	repeat with a from 1 to (TheList's length)
		if newList does not contain {(TheList's item a)} then set end of newList to (TheList's item a)
	end repeat
	return newList
end RemoveDuplicates
set newList to the clipboard

Sounds like you want to enter two strings of numbers and remove from one of them any numbers found in the other. The handler (RemoveDuplicates) takes a single list and remove items that are found twice in the list by creating a new list, but not adding to it if the next number in the original list is already in it.

Could you be clearer about what you want to start with and what you actually want to accomplish, i.e. end up with?

Assuming you want to do exactly what your example is supposed to do (I think):

set listA to text returned of (display dialog "Enter your first list seperated by commas:" default answer "") -- this is a string of text
set listB to text returned of (display dialog "Enter your first list seperated by commas:" default answer "") -- another string of text
-- convert the comma delimited string to a list
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "," -- separate the items of the string about the commas
set listA to text items of listA
set listB to text items of listB
set AppleScript's text item delimiters to tid
-- concatenate the two lists
set TheList to (listA & listB) -- this sticks listB onto the end of listA
-- keep the unique members of the lists
set TheList to RemoveDuplicates(TheList)

to RemoveDuplicates(TheList)
	set newList to {}
	repeat with a from 1 to (TheList's length)
		if newList does not contain {(TheList's item a)} then set end of newList to (TheList's item a)
	end repeat
	return newList
end RemoveDuplicates

Hey Adam,
First, thanks for the input. Sorry about not getting back to you sooner. I left work before your posts and don’t check my work mail unless I’m at work.
So the script works great, but I actually needed the reverse. I want to keep the numbers that are the same between the two lists.

So obviously I’m doing something wrong to your script… :frowning:
I appreciate the notes you left in there. A LOT! can you explain the last bit.

also, I realize what I initially stated was wrong. sorry about that. Duplicate Saver! lol but just one of the duplicates, if possible


set listA to text returned of (display dialog "Enter your first list seperated by commas:" default answer "") -- this is a string of text
set listB to text returned of (display dialog "Enter your first list seperated by commas:" default answer "") -- another string of text
-- convert the comma delimited string to a list
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "," -- separate the items of the string about the commas
set listA to text items of listA
set listB to text items of listB
set AppleScript's text item delimiters to tid
-- concatenate the two lists
set TheList to (listA & listB) -- this sticks listB onto the end of listA
-- keep the unique members of the lists
set TheList to KeepDuplicates(TheList)

to KeepDuplicates(TheList)
	set newList to {}
	repeat with a from 1 to (TheList's length)
		if newList contains {(TheList's item a)} then set end of newList to (TheList's item a)
	end repeat
	return newList
end KeepDuplicates

One of these might help.

set x to {1, 2, 3, 4, 5}
set y to {2, 4, 6, 8}
set z to {}
repeat with i from 1 to (count of x)
	if item i of x is in y then copy item i of x to end of z --makes a list of only duplicate items
	if item i of x is not in y then copy item i of x to end of y --adds only the items of x to y that are not in y already
end repeat
return y

Thanks Jerome,
Yea, that definitely worked. I combined it with Adams.
But for whatever reason, I can’t get the result set to the clipboard OR in a display dialog.
What am I doing wrong? I’ve tried a bunch of things, some of which are below.
Thanks in advance.


set listA to text returned of (display dialog "Enter your first list seperated by commas:" default answer "") -- this is a string of text
set listB to text returned of (display dialog "Enter your first list seperated by commas:" default answer "") -- another string of text

set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "," -- separate the items of the string about the commas
set listA to text items of listA
set listB to text items of listB
set AppleScript's text item delimiters to tid

set newList to {}
repeat with a from 1 to (count of listA)
	if item a of listA is in listB then copy item a of listA to end of newList --makes a list of only duplicate items
	--if item a of listA is not in listB then copy item a of listA to end of listB --adds only the items of x to y that are not in y already
end repeat
return newList
set result to the clipboard

--set newList to the clipboard
--display dialog "Your numbers have been set to the clipboard.  (Cmd + V)" 
--set text returned of newList to the clipboard


remove the return statement, that ends the script…or put the clipboard statement before the return.

So I tried that and its not working, least on my machine… :confused:
In reality, I just need the data sent either to the clipboard or in a display dialog for the user to copy and paste else where. Another option would be to save it in a text document on the desktop, but that seems like extra and unnecessary work.


set listA to text returned of (display dialog "Enter your first list seperated by commas:" default answer "") -- this is a string of text
set listB to text returned of (display dialog "Enter your first list seperated by commas:" default answer "") -- another string of text

set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "," -- separate the items of the string about the commas
set listA to text items of listA
set listB to text items of listB
set AppleScript's text item delimiters to tid

set newList to {}
repeat with a from 1 to (count of listA)
	if item a of listA is in listB then copy item a of listA to end of newList --makes a list of only duplicate items
	--if item a of listA is not in listB then copy item a of listA to end of listB --adds only the items of x to y that are not in y already
end repeat
set result to the clipboard
display dialog "Your numbers have been set to the clipboard.  (Cmd + V)" 
return newList

Try this: (you can’t set the clipboard to a list):


set listA to text returned of (display dialog "Enter your first list seperated by commas:" default answer "") -- this is a string of text
set listB to text returned of (display dialog "Enter your first list seperated by commas:" default answer "") -- another string of text

set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "," -- separate the items of the string about the commas
set listA to text items of listA
set listB to text items of listB
set AppleScript's text item delimiters to tid

set newList to {}
repeat with a from 1 to (count of listA)
	if item a of listA is in listB then copy item a of listA to end of newList --makes a list of only duplicate items
	--if item a of listA is not in listB then copy item a of listA to end of listB --adds only the items of x to y that are not in y already
end repeat
set AppleScript's text item delimiters to ", " -- convert the list back to comma-space delimited string
set NL to newList as text
set AppleScript's text item delimiters to tid
set the clipboard to NL
--display dialog "Your numbers have been set to the clipboard.  (Cmd + V)"

YAY! Thanks to both of you!
Thats it. I didn’t know you couldn’t set a list to the clipboard… That’s too bad. But converting it to text works wonderfully.
Why is that setting it as a string wouldn’t work either? or did I just do that wrong?