Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'An AppleScript to remove Address Book duplicates' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to remove Address Book duplicates
Authored by: zegor on Jul 14, '07 03:48:24PM
Here's a variant of the script that
  • Gets the name of the duplicates list from the user
  • Allows on a per-duplicate decision on whether to consider it a duplicate
  • Considers all the name components, the company name and one email address for comparison purposes. Handles empty names just fine.
  • Mixes French and English variable names :-)
-- 
-- A script to look for duplicate cards in the Address Book
-- It looks for identical names, companies and emails
-- As distributed it requires identical emails, 
-- or identical first,middle and last names plus company name to be considered identical
-- Simply comment out the code associated with an attribute to remove it from consideration
-- If multiple emails are associated with a card then the first one is considered canonical.
-- 
-- The script is a modification of the script authored by Safar 
-- http://www.macosxhints.com/users.php?mode=profile&uid=1000517 and http://www.etincelle.org/   
-- I have kept the French variable names, and added my English ones (ONE world!)
--
-- Version 13. SM 20070714
--
property duplicate_group_name : "Doubles"
property automatic_move : true -- the default and automatic action is to move the possible duplicates to the group with the name given by duplicate_group_name

display dialog "The name of the group to collect possible duplicates" default answer duplicate_group_name
set duplicate_group_name to text returned of result

display dialog "Should identified duplicates be moved to " & duplicate_group_name & " automatically?" buttons ["yes", "no"] default button 2
if (button returned of result is "yes") then
	set automatic_move to true
else
	set automatic_move to false
end if
set liste to {}
set email_liste to {}
set avirer to {}
tell application "Address Book"
	with timeout of 3200 seconds
		set code to ""
		if not (duplicate_group_name is in name of every group) then
			set duplicate_group to make new group with properties {name:duplicate_group_name}
		else
			set duplicate_group to every group whose name is duplicate_group_name
		end if
		repeat with this_person in every person -- of group "PossibleDuplicates"
			-- Comment out the components you don't want to test on
			set full_name to ""
			
			set first_name to first name of this_person as string
			if first_name = "missing value" then set first_name to ""
			set full_name to full_name & first_name
			
			set middle_name to middle name of this_person as string
			if middle_name = "missing value" then set middle_name to ""
			set full_name to full_name & middle_name
			
			set last_name to last name of this_person as string
			if last_name = "missing value" then set last_name to ""
			set full_name to full_name & last_name
			
			set this_email_list to properties of emails of this_person
			try
				set this_email to value of item 1 of this_email_list
			on error
				set this_email to ""
			end try
			set full_name to full_name & this_email
			
			set this_company to company of this_person as string
			if this_company = "false" then set this_company to ""
			set full_name to full_name & this_company
			
			if (full_name > "" and not (full_name = "true")) then
				if full_name is in liste then
					if automatic_move = false then
						-- check the user's preference
						display dialog ("Possible duplicate (name): " & first_name & " " & middle_name & " " & last_name & " " & this_company & " " & this_email) buttons ["ignore", "move to " & duplicate_group_name, "Cancel"] default button 2
						if button returned of result is ("move to " & duplicate_group_name) then
							set lid to this_person's id
							set avirer to avirer & lid
						end if
					else
						set lid to this_person's id
						set avirer to avirer & lid
					end if
				else
					set liste to liste & full_name
				end if
			else if (this_email > "") then
				if this_email is in email_liste then
					if automatic_move = false then
						-- check the user's preference
						display dialog ("Possible duplicate (email): " & first_name & " " & middle_name & " " & last_name & " " & this_company & " " & this_email) buttons ["ignore", "move to " & duplicate_group_name, "Cancel"] default button 2
						if button returned of result is ("move to " & duplicate_group_name) then
							set lid to this_person's id
							set avirer to avirer & lid
						end if
					else
						set lid to this_person's id
						set avirer to avirer & lid
					end if
				else
					set email_liste to email_liste & this_email
				end if
			end if
			
		end repeat
		
		repeat with une in avirer
			set bla to person id une
			-- set lenom to name of bla
			add bla to group duplicate_group_name
		end repeat
		save addressbook
	end timeout
end tell


[ Reply to This | # ]