Scripting Address Book

Hi - have posted here before but am very much still an Applescript newbie.

Context:

  • our company has recently been acquired - all our email addresses and Jabber handles are in the course of changing
  • Jabber is being introduced to the acquiring company - they do not have handles at all
  • also in some cases our telephone extensions

What I’d like to do:

I can get as far as extracting people from the address book but not by email address … I think it’s these lists within lists which are throwing me.

This has no error checking. It simply assumes that the existing data have legitimate values and that the people don’t already have the additions and changes you want to effect. Check that it does what you want before using it in earnest!

-- Edit these two variables as required.
set oldDomain to "oldcompany.com"
set newDomain to "newcompany.com"

set lowerChrs to "abcdefghijklmnopqrstuvwxyz"

tell application "Address Book"
	-- Get all the email address and people IDs en masse to save looping through every person in Address Book itself.
	set {emailAddresses, peopleIDs} to {value of emails, id} of people
	
	repeat with i from 1 to (count peopleIDs)
		set thisPersonsEmailAddresses to item i of emailAddresses
		repeat with thisEmailAddress in thisPersonsEmailAddresses
			-- Only react to email addresses ending in the old or new company domains.
			if (thisEmailAddress ends with oldDomain) or (thisEmailAddress ends with newDomain) then
				tell person id (item i of peopleIDs)
					-- Construct the pre-"@" part of this person's e-mail/Jabber address.
					set {first name:name1, last name:name2} to it
					set emailName to character (offset of (character 1 of name1) in lowerChrs) of lowerChrs & character (offset of (character 1 of name2) in lowerChrs) of lowerChrs
					if ((count name2) > 1) then set emailName to emailName & text 2 thru -1 of name2
					-- Add a Jabber handle for them.
					make new Jabber handle at end of Jabber handles with properties {label:"work", value:emailName & "@chat." & newDomain}
					
					-- If the person's from the old domain .
					if (thisEmailAddress ends with oldDomain) then
						-- . add a new e-mail address for them .
						make new email at end of emails with properties {label:"work", value:emailName & "@" & newDomain}
						-- . and doctor their extension number(s).
						set thesePhones to (phones whose label is "extension")
						repeat with thisPhone in thesePhones
							set extn to value of thisPhone
							if ((count extn) is 3) and (extn begins with "1") then set thisPhone's value to "2" & extn
						end repeat
					end if
				end tell
				exit repeat -- We only need one hit per person.
			end if
		end repeat
	end repeat
	save -- Save the changes.
end tell

Nigel - will try this out - many thanks.
Mark