I need help conditionally adding people Address Book

I’m working on a script that will execute as part of a Mail rule. So far it works fine, but I’m just a bit stuck. Here’s what I’ve got:

Here’s the script so far:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		
		tell application "Mail"
			repeat with thisMessage in theMessages
				
				tell thisMessage
					set theSubject to sender
					set theName to extract name from theSubject
					set resultingString to theName & " has sent you a message.  Wanna do something?"
					set tempVar to display dialog resultingString buttons {"Add Sender to Group1", "Do Nothing"}
					set theButtonPressed to button returned of tempVar
					
					if theButtonPressed is "Add Sender to Group1" then
						--do address book stuff
						say "address book"
					end if
					
				end tell
			end repeat
		end tell
		
	end perform mail action with messages
end using terms from

So far it works just fine. When I hit the button “Add sender to Group1” is speaks “address book” (this is just so I know it’s working, BTW), but I’m just pretty ignorant as to what to do next to accomplish what I want, which is one of the following:

a) if the sender currently has no address book entry, create one and copy it to the group “Group1”
b) if the sender does have an entry, copy it to “Group1”
c) if the sender both already has an entry and is also already in the “Group1” group, do nothing.

Any ideas? Thanks a lot!

Model: Dual-proc 2GHz G5 PowerMac
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi,

How about this? I haven’t tried it as a rule, but the equivalent works when run as a script.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		
		tell application "Mail"
			repeat with thisMessage in theMessages
				
				tell thisMessage
					set theSubject to sender
					set theName to extract name from theSubject
					set theEmail to extract address from theSubject
				end tell
				
				set resultingString to theName & " has sent you a message. Wanna do something?"
				set tempVar to display dialog resultingString buttons {"Add Sender to Group1", "Do Nothing"}
				set theButtonPressed to button returned of tempVar
				
				if theButtonPressed is "Add Sender to Group1" then
					tell application "Address Book"
						-- Look for people with this email address.
						set myPeople to (people where value of emails contains theEmail)
						-- If there are none, make a new record and add it to the group.
						if myPeople is equal to {} then
							-- Make new record.  The name may need some work (for names with three words etc.)
							set newPerson to make new person with properties {first name:(word 1 of theName), last name:(word -1 of theName)}
							-- Add the email address.
							tell newPerson to make new email at end of emails with properties {value:theEmail}
							-- Add them to the group.
							add newPerson to group "Group1"
						else
							-- Else, just add first person who has this email addreess to the group.
							add (item 1 of myPeople) to group "Group1"
						end if
						-- Update Address Book.
						save addressbook
					end tell
				end if
				
			end repeat
		end tell
		
	end perform mail action with messages
end using terms from

I looked for the people in Address Book by email address not name. You might want to look into how to improve the name process for names that have three words, hyphenated names etc.

Best wishes

John M

Holy shit, you’re a genius. It works flawlessly. I can’t thank you enough!

property lastPick : 1
tell application "Contacts"
	set allGroupNames to name of every group
	set whichGroup to choose from list allGroupNames ¬
		with title ¬
		"Which Group" with prompt ¬
		"Select the group you would like to add sender(s) to" default items lastPick ¬
		OK button name ¬
		"This Group" multiple selections allowed false ¬
		without empty selection allowed
end tell
set groupName to item 1 of whichGroup
set lastPick to whichGroup
tell application "Mail"
	set selectedMessages to the selection
	set allSenders to {}
	repeat with thisMessage in selectedMessages
		set thisSender to sender of thisMessage
		if thisSender is not in allSenders then set the end of allSenders to thisSender
	end repeat
end tell


tell application "Contacts"
	set AppleScript's text item delimiters to {" <", ">"}
	set theGroup to group groupName
	set namesInGroup to name of every person of theGroup
	repeat with thisPerson in allSenders
		set middleName to ""
		set firstName to ""
		set lastName to ""
		set AppleScript's text item delimiters to {" <", ">"}
		set {senderName, senderEmail} to text items of thisPerson
		set groupMembers to id of people of theGroup
		try
			set establishedContact to (person 1 whose name is senderName)
			if id of establishedContact is not in groupMembers then
				add establishedContact to theGroup
				save
				return
			else
				return
			end if
		on error errMsg number errNum
			set AppleScript's text item delimiters to {" "}
			
			set nameWords to text items of senderName
			if the (count of nameWords) = 1 then
				set {firstName, lastName} to {"", senderName}
			else if the (count of nameWords) = 2 then
				set {firstName, lastName} to nameWords
			else
				set firstName to item 1 of nameWords
				set middleName to items 2 thru -2 of nameWords as text
				set lastName to the last item of nameWords
			end if
			set firstName to item 1 of nameWords
			
			tell application "Contacts"
				set thePerson to make new person with properties {first name:firstName, middle name:middleName, last name:lastName}
				make new email at end of emails of thePerson with properties {label:"Work", value:senderEmail}
				add thePerson to theGroup
				save
			end tell
		end try
	end repeat
end tell