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

Find duplicate Address Book entries via a script UNIX
I was looking for an AppleScript that would just show me the duplicate entries in my Address Book, and not delete them out of hand, since some have different information yet the same name. I could not find one, but found this great gem from Apple Developer Documentation - Apple Script:
osascript -e 'tell app "Address Book" to get the name of every person' \
| perl -pe 's/, /\n/g' | sort | uniq -d
It will give you a nice listing of every duplicate entry in the Address Book, making it easier to sort through them for information and delete them individually.

[robg adds: Address Book's Card: Look for Duplicate Entries feature doesn't provide much information at all prior to merging -- it will just say "one duplicate card was found; do you want to merge this data?," without telling you which cards are duplicates. This script will let you verify which cards may be merged, prior to doing so. Note that the script is based simply on name matches, so you may see more duplicates listed via its ouput than you would with Address Book's duplicate feature.]
    •    
  • Currently 3.78 / 5
  You rated: 3 / 5 (9 votes cast)
 
[35,068 views]  

Find duplicate Address Book entries via a script | 18 comments | Create New Account
Click here to return to the 'Find duplicate Address Book entries via a script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Find duplicate Address Book entries via a script
Authored by: BobFrnz on Mar 27, '06 10:28:04AM

Tried copying this directly into AppleScript (Panther3.8) it wouldn't compile past
osascript -e 'tell
It did not like single quote. Tried removing the single quotes and reversing the singles and doubles no luck.

Too bad this script would be really useful if a non expert could get it to run.



[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: BobFrnz on Mar 27, '06 10:41:02AM

OOPS. Just realized this is a terminal script. Runs great:-))
Sorry



[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: thomashallock on Aug 27, '07 03:08:58PM

osascript is a command-line tool. Copy and paste this into the terminal, found in /Applications/Utilities/Terminal.app



[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: mark hunte on Mar 27, '06 11:07:27AM
Sorry But this Applescript may be better.
This script will Find the duplicate Entries in Your address book and put them into a New Group Called "Dupelicate Entries" Allowing you to easly edit them.) ***NOTE: The script will ADD a NEW GROUP IN YOUR ADDRESS BOOK : NAMED "Dupelicate Entries" if it does not exist:
 
(*
Details found at - http://www.macosxhints.com/comment.php?sid=20060322202753429
Written by © Mark Hunte - 2006 *)

tell application "Address Book"
	set biglist to {}
	set theGroup to count every person
	if not (exists (group "Dupelicate Entries")) then
		make new group with properties {name:"Dupelicate Entries"}
	end if
	set the_names to the name of every person as list
	repeat with i from 1 to number of items in the_names
		set this_Name to item i of the_names
		set theName to name of person this_Name as string
		if this_Name is not in biglist then
			copy this_Name to end of biglist
		else
			add (people whose name is theName) to group "Dupelicate Entries"
		end if
	end repeat
	save addressbook
end tell

---
mh

[ Reply to This | # ]

Find duplicate Address Book entries via a script
Authored by: rusto on Mar 27, '06 05:04:01PM

Any reason you spelled "duplicates" as "dupelicates" in the script?



[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: mark hunte on Mar 27, '06 11:35:30PM

Nope, just Stupid... : )

Unfortunately you can not go back and edit silly spelling mistakes here.

---
mh



[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: mark hunte on Oct 03, '09 09:39:45AM
I had an email from dzg, letting me know this was broken in 10.6.
As far as I can tell its an odd bug. But a simplest fix is below.


(*
REVISED FOR 10.6
Details found at - 
http://www.macosxhints.com/article.php?story=20060322202753429
Written by © Mark Hunte - 2009*)

tell application "Address Book"
	set biglist to {}
	set theGroup to "Dupilicate Entries"
	if not (exists (group "Dupilicate Entries")) then
		make new group with properties {name:"Dupilicate Entries"}
		save
	end if
	set the_names to name of people
	repeat with i from 1 to number of items in the_names
		set theName to item i of the_names
		if theName is not in biglist then
			copy theName to end of biglist
		else
			set counter to (people whose name is theName)
			if (count of counter) > 1 then
				repeat with i from 1 to number of items in counter
					set this_item to item i of counter
					add this_item to group theGroup
				end repeat
				save
			end if
		end if
	end repeat
end tell

---
mh

[ Reply to This | # ]

Find duplicate items in entries (eg phone number twice)
Authored by: nikle on Mar 27, '06 11:52:23AM

Due to a sync accident all my contacts have duplicate phone numbers and email addresses, is there a script that will delete any duplicate items within an entry/contact?



[ Reply to This | # ]
Find duplicate items in entries (eg phone number twice)
Authored by: vocaro on Jul 06, '06 10:45:28PM
Find duplicate Address Book entries via a script
Authored by: dzg on Oct 02, '09 11:46:51AM

Help!

Since upgrading to 10.6 snow leopard, this does not work!

When trying to "add (people whose name is theName) to group ___"
Address Book gives an error!



[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: mark hunte on Oct 03, '09 09:43:17AM
Just noticed you posted here..
As far as I can tell its an odd bug. But a simplest fix is below.


(*
REVISED FOR 10.6
Details found at - 
http://www.macosxhints.com/article.php?story=20060322202753429
Written by © Mark Hunte - 2009*)

tell application "Address Book"
	set biglist to {}
	set theGroup to "Dupilicate Entries"
	if not (exists (group "Dupilicate Entries")) then
		make new group with properties {name:"Dupilicate Entries"}
		save
	end if
	set the_names to name of people
	repeat with i from 1 to number of items in the_names
		set theName to item i of the_names
		if theName is not in biglist then
			copy theName to end of biglist
		else
			set counter to (people whose name is theName)
			if (count of counter) > 1 then
				repeat with i from 1 to number of items in counter
					set this_item to item i of counter
					add this_item to group theGroup
				end repeat
				save
			end if
		end if
	end repeat
end tell

---
mh

[ Reply to This | # ]

Find duplicate Address Book entries via a script
Authored by: BDBush on Sep 29, '10 10:55:32AM

i tried copying this info into the terminal window and got a lot of "command not found" (see results). the first was 2 lines were

Parsnips-Mac:~ SomeOne$ tell application "Address Book"
-bash: tell: command not found

I have not worked much with the terminal or apple scripts, what am i missing?

I could post the results but it is REALLY LONG

thanks for any help



[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: Wanai on Oct 06, '10 07:11:19PM

It's an Applescript so you either need to open the app "Applescript Editor" in the Utilities folder and paste it in the new window or in terminal you need to enter the command to run an applescript. The latter is slightly more involved so just run it in "Applescript Editor".



[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: BDBush on Oct 07, '10 11:32:37AM

Worked like a charm,, thanks for the help!



[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: davidef on Jan 08, '11 06:30:39PM

Very useful thread. Running the applescript worked exactly as described.
Thanks!



[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: bromo on May 02, '11 04:25:57PM

Here is a slightly improved version of the duplicated finder script. This script will handle embedded special characters correctly. (O'Reilly will parse properly among others) It works case independently so John Smith and John smith will appear as duplicates. Finally it prefixes each duplicated name with a number indicating how many times the name is duplicated.


osascript -e 'set result_list to ""'
-e 'tell application "Address Book" to repeat with i_ref in (get name of every person)'
-e 'set result_list to result_list & quoted form of contents of i_ref & linefeed'
-e 'end repeat'
| sort -f | uniq -ci | grep -v '^ *1 '

BTW the unix pipeline at the end creates a sorted list with one entry for each name prefixed by the number of entries that have a match to the name. The grep command then filters out all the entries that have only 1 matching entry.



[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: dgerrity on Nov 13, '11 03:17:51PM
That is fantastic! Thank you. BTW it's hard to read the options to uniq -- they are dash c as in show count, and i (eye) as in case insensitive.

[ Reply to This | # ]
Find duplicate Address Book entries via a script
Authored by: doggrunner on Feb 29, '12 12:43:15PM

This works for snow leopard. I used it with LION and it aborts adding the addressbook entry (this_item to group theGroup).

It gets the following error: "error "Address Book got an error: You can only add a person to a group." number 1"
This happens on the first duplicate.
The AppleScript Error is: Address Book got an error: You can only add a person to a group.


tell application "Address Book"
exists group "Dupilicate Entries"
--> true
get name of every person
--> {"J...n"}
get every person whose name = "K..."
--> {person id "ACBABB6E-7219-41D6-9ED5-B92B65910DE7:ABPerson", person id "85E472F7-2DF4-44F8-979A-0B8EC1AF805F:ABPerson", person id "A4943471-289A-4E00-8969-4F71B569BCCF:ABPerson"}
add person id "ACBABB6E-7219-41D6-9ED5-B92B65910DE7:ABPerson" to group "Dupilicate Entries"
--> error "You can only add a person to a group." number 1
Result:
error "Address Book got an error: You can only add a person to a group." number 1



[ Reply to This | # ]