groups

info


tags: applescript,address,book

Link to this snippet:


Download to Code Collector

language: AppleScript
licence: Other

Detect Name Switch - Address Book

options: send to code collectorview all seydoggy's snippets
(*
Detect Name Switch

By Adam Merrifield <http://seydoggy.com>

This script will cause irreversible changes
to contacts in your Address Book.

USE AT YOUR OWN RISK!!

This script detects possible duplicate contacts in your Address Book that may simply be a reversal of fist and last names. Upon Finding those duplicates it will prompt you with the relevant information from each so you can decide if they are duplicates and whether to delete one of the other.

- r1 12-23-09 12:45
*)
tell application "Address Book"
	activate
	set allPeople to every person
	
	repeat with i from 1 to count of allPeople
		set firstPerson to ""
		set secondPerson to ""
		set otherPeople to ""
		
		set firstPerson to (item i of allPeople)
		set firstName to (first name of firstPerson)
		set lastName to (last name of firstPerson)
		set theirEmail to (value of email of firstPerson) as string
		set otherPeople to (every person whose (last name is firstName) and (first name is lastName))
		if otherPeople is not "" then
			repeat with j from 1 to count of otherPeople
				set secondPerson to (item j of otherPeople)
				set firstName2 to (first name of secondPerson)
				set lastName2 to (last name of secondPerson)
				set theirEmail2 to (value of email of secondPerson) as string
				if secondPerson is not "" then
					
					-- alert for my attention
					set thisSound to (((path to library folder from system domain) as string) & "Sounds:Hero.aiff") as alias
					try
						tell application "QuickTime Player 7"
							open thisSound
							set close when done of document 1 to true
							play document 1
							delay 1
							quit
						end tell
					end try
					
					-- show me the possible duplicates
					display dialog ¬
						"These items might be duplicates:" & return & return & ¬
						"ITEM 1" & return & ¬
						"First name: " & firstName & return & ¬
						"Last name: " & lastName & return & ¬
						"Email: " & theirEmail & return & return & return & ¬
						"ITEM 2" & return & ¬
						"First name: " & firstName2 & return & ¬
						"Last name: " & lastName2 & return & ¬
						"Email: " & theirEmail2 & return & return & return & ¬
						"Are they duplicates?" buttons {"Cancel", "No", "Yes"} default button 3 giving up after 300
					copy result as list to {the buttonReturned, the gaveUp}
					if gaveUp is true then error number -128
					if buttonReturned is "Yes" then
						display dialog ¬
							"Which one shall we delete?" & return & return & ¬
							"ITEM 1" & return & ¬
							"First name: " & firstName & return & ¬
							"Last name: " & lastName & return & ¬
							"Email: " & theirEmail & return & return & return & ¬
							"ITEM 2" & return & ¬
							"First name: " & firstName2 & return & ¬
							"Last name: " & lastName2 & return & ¬
							"Email: " & theirEmail2 ¬
							buttons {"Cancel", "ITEM 1", "ITEM 2"} giving up after 300
						copy result as list to {the buttonReturned, the gaveUp}
						if gaveUp is true then error number -128
						if buttonReturned is "ITEM 1" then
							delete firstPerson
						else
							delete secondPerson
						end if
						save
					end if
				end if
			end repeat
		end if
	end repeat
end tell