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

An AppleScript to remove Address Book duplicates Apps
To get rid of duplicates in Address Book, I wrote the following AppleScript. Copy and paste it into Script Editor, and save it as a script. As Rob would say, "make sure you have a good backup." So from the file menu in Address Book, choose 'Export Database' first.
set liste to {}
set avirer to {}
tell application "Address Book"
  with timeout of 3200 seconds
    set code to ""
    repeat with this_person in every person --of group "bidule"
      set first_name to first name of this_person as string
      set middle_name to middle name of this_person as string
      set last_name to last name of this_person as string
      set full_name to first_name & middle_name & last_name
      if full_name is in liste then
        set lid to this_person's id
        set avirer to avirer & lid
      else
        set liste to liste & full_name
      end if
    end repeat
    repeat with une in avirer
      set bla to person id une
      set lenom to name of bla
      delete person id une
      save addressbook
    end repeat
  end timeout
end tell
[robg adds: I compiled the script, but haven't tested it. In 10.3.8, Address Book has a File: Back up Database option, which is probably what you should do before trying this script.]
    •    
  • Currently 3.17 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (6 votes cast)
 
[33,849 views]  

An AppleScript to remove Address Book duplicates | 7 comments | Create New Account
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.
vive la france!
Authored by: jspivack on Apr 14, '05 11:41:39AM

C'est rassurant de voir des variables francophones!

[Tr: it's nice to see french variables!]

liste = list
avirer = tobedeleted
bidule = thingy

:)



[ Reply to This | # ]
An AppleScript to remove Address Book duplicates
Authored by: dkeitel on Apr 14, '05 01:55:14PM
If you have entries from companies that don't have any person listed in them, they also get deleted. For instance, I have a card for my local record store, but I don't know the name of the guy who works there, so I never put it in. This entry got deleted with this script. Fortunately I had a backup.

I changed the script around to dump all the doubles it finds into a group named "doubles", so you can go through by hand and delete. I know less than anything about writing applescripts, so you have to create a group named "doubles" before you run this script.


set liste to {}
set avirer to {}
tell application "Address Book"
	with timeout of 3200 seconds
		set code to ""
		repeat with this_person in every person --of group "bidule"
			set first_name to first name of this_person as string
			set middle_name to middle name of this_person as string
			set last_name to last name of this_person as string
			set full_name to first_name & middle_name & last_name
			if full_name is in liste then
				set lid to this_person's id
				set avirer to avirer & lid
			else
				set liste to liste & full_name
			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 "doubles"
			save addressbook
		end repeat
	end timeout
end tell


[ Reply to This | # ]
An AppleScript to remove Address Book duplicates
Authored by: Jack Mac on Aug 29, '05 08:13:30PM

I am running 10.2.8.

Address Book 3.0.3 (v225)

Will the script work, has anyone tried it with the versions mentioned?



[ Reply to This | # ]
An AppleScript to remove Address Book duplicates
Authored by: Dave Creek on Dec 07, '05 11:07:56AM

This script is exactly what I want, but...

Could you modify it so it does not select entries that have both the first and last name blank? That way entries that have a company name listed but don't have a person listed would not show up in the "doubles" group.



[ Reply to This | # ]
An AppleScript to remove Address Book duplicates
Authored by: kupietz on Feb 01, '06 09:10:51PM
Here's a version that considers all contact info fields that would be included in a vCard, instead of just the name fields. I'm not sure what exact fields are considered, I know the Notes field isn't but other than that it only found records that were complete field-for-field duplicates of other records in my Address Book. Make sure you comb through to check that you aren't deleting any important notes before you delete records. You still need to create an empty group "Doubles" in your address book before running this.
set liste to {}
set avirer to {}
tell application "Address Book"
	with timeout of 3200 seconds
		set code to ""
		repeat with this_person in every person --of group "bidule"
			set full_name to vcard of this_person as string
			if full_name is in liste then
				set lid to this_person's id
				set avirer to avirer & lid
			else
				set liste to liste & full_name
			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 "Doubles"
			save addressbook
		end repeat
	end timeout
end tell
Thanks to the original author and other contributors, this saved me a bunch of time.

[ Reply to This | # ]
An AppleScript to remove Address Book duplicates
Authored by: kupietz on Feb 01, '06 10:14:50PM

One note: I'm not sure because I didn't keep tabs on exactly what I did, but it seems like if you delete all groups except "All", comparing vCard attributes finds more duplicates than if you have people in several groups. I'm not clear on this whole vCard thing.

Mike



[ Reply to This | # ]
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 | # ]