Find and move multiple addresses in Address Book into new group

Hi,

Would anyone know a way to find multiple addresses in Address Book (Mac, Snow Leopard) and then move those contacts into a new group? I have the addresses as a list in a text file.

Thanks.

You can do something along these lines:

property targetEmails : {"RobS@gmail.com", "RobT@gmail.com"}
property groupName : "groupie"

tell application "Address Book"
	if not (exists group groupName) then
		make new group with properties {name:groupName}
	end if
	
	repeat with anEmail in targetEmails
		set anEmail to anEmail as text
		set aPerson to (first person whose vcard contains anEmail)
		add aPerson to group groupName
	end repeat
	save
end tell

Thanks,

I tried it with three email to start with.

Just get an unhelpful message - “missing value”

The email addresses are in my Address Book.

property targetEmails : {“xxxxx@xxxxxx.com”, “xxxxx@xxxxxx.com”, “xxxxx@xxxxxx.com”}
property groupName : “groupie”

tell application “Address Book”
if not (exists group groupName) then
make new group with properties {name:groupName}
end if

repeat with anEmail in targetEmails
	set anEmail to anEmail as text
	set aPerson to (first person whose vcard contains anEmail)
	add aPerson to group groupName
end repeat
save

end tell

Try this:


property targetEmails : {"xxxxx@xxxxxx.com", "xxxxx@xxxxxx.com", "xxxxx@xxxxxx.com"}
property groupName : "groupie"

tell application "Address Book"
	if not (exists group groupName) then
		make new group with properties {name:groupName}
	end if
	
	repeat with anEmail in targetEmails
		set anEmail to anEmail as text
		set thePeople to (people whose value of emails contains anEmail)
		-- In case there are multiple contacts that contain the same email
		repeat with aPerson in thePeople
			add aPerson to group groupName
		end repeat
	end repeat
	save
end tell

Thanks. Address Book opened, created the ‘groupie’ Group and now there are the three contacts in that group. Still get ‘missing value’ in Applescript Editor though…

Update: Did it for 130+ emails and it works! Thanks. (Still get ‘missing value’ though.)

I’m glad it worked for you. Don’t worry about missing value, it isn’t an error.