add/delete e-mail-address to/from addressbook

hi,
i want to create two rules in mail (2.0.5).

  1. If i receive a email with the subject “subscribe” → add email-address to a categorie in my addressbook
  2. If i receive a email with the subject “unsubscribe” → delete email-address from my addressbook

but i can’t find some scripts. do you have a idea?

cheers
peter

Browser: Safari 417.8
Operating System: Mac OS X (10.4)

This should work. If you like it, save it as an app and run it as a startup app in system preferences. If you do this, it will start 2 minutes after your computer turns on. It will then check for new subscriptions/unsubscriptions every 30 minutes. Let me know if you have problems. Ian

delay 2 * minutes
repeat
	tell application "Mail"
		activate
		launch
		delay 5
		get name of accounts
		set mailboxName to name of accounts
		check for new mail of account mailboxName
		tell application "Mail"
			repeat until not (exists (first message of inbox whose first word of content is "subscribe"))
				set sendername to sender of first message of inbox whose first word of content is "subscribe"
				set name1 to extract name from sendername
				set address1 to extract address from sendername
				set firstname to word 1 of name1
				set lastname to word 2 of name1
				delete (first message of inbox whose first word of content is "subscribe")
				tell application "Address Book"
					set the_person to (make new person with properties {first name:firstname, last name:lastname, email:address1})
					make new email at the end of the_person with properties {value:address1, label:"work"}
					save addressbook
				end tell
			end repeat
		end tell
		tell application "Mail"
			repeat until not (exists (first message of inbox whose first word of content is "unsubscribe"))
				set sendername to sender of first message of inbox whose first word of content is "unsubscribe"
				set name1 to extract name from sendername
				set address1 to extract address from sendername
				set firstname to word 1 of name1
				set lastname to word 2 of name1
				delete (first message of inbox whose first word of content is "unsubscribe")
				tell application "Address Book"
					delete person name1
					save addressbook
				end tell
			end repeat
		end tell
	end tell
	delay 30 * minutes
end repeat

Oh…I havvve this setup so that you write subscribe or unsubscriib in the email message, not the email subject.

If you want it setup to read the subject box, use this. Another thing to think about. If you are using CronniX to schedual things on your computer, you can time this too. If you do this, just cut off the “delay 2* miutes”, repeat, and “delay 30* minutes” parts.

 delay 2 * minutes
repeat
	tell application "Mail"
		activate
		launch
		delay 5
		get name of accounts
		set mailboxName to name of accounts
		check for new mail of account mailboxName
		tell application "Mail"
			repeat until not (exists (first message of inbox whose subject is "subscribe"))
				set sendername to sender of first message of inbox whose subject is "subscribe"
				set name1 to extract name from sendername
				set address1 to extract address from sendername
				set firstname to word 1 of name1
				set lastname to word 2 of name1
				delete (first message of inbox whose subject is "subscribe")
				tell application "Address Book"
					set the_person to (make new person with properties {first name:firstname, last name:lastname, email:address1})
					make new email at the end of the_person with properties {value:address1, label:"work"}
					save addressbook
				end tell
			end repeat
		end tell
		tell application "Mail"
			repeat until not (exists (first message of inbox whose subject is "unsubscribe"))
				set sendername to sender of first message of inbox whose subject is "unsubscribe"
				set name1 to extract name from sendername
				set address1 to extract address from sendername
				set firstname to word 1 of name1
				set lastname to word 2 of name1
				delete (first message of inbox whose subject is "unsubscribe")
				tell application "Address Book"
					delete person name1
					save addressbook
				end tell
			end repeat
		end tell
	end tell
	delay 30 * minutes
end repeat 

thank you very much bonedoc for your help.

is it possible to modify this script a little bit.

  • sometimes there are 5 same entries of a subscriber in my addressbook

  • add/remove subscriber to/from a addressbook category

  • i don’t want that a extra program run in the background. how can i use it as a mail-applescript-rule?

my rules are at the moment:
if all of the following conditions are met:
to contains xxx@xxx.de
subject contains “subscribe”

perform the following actions:
run applescript
replay to message
move message to mailbox: folderXXX

thanks
Alfons

ok, here is your final script. It will only add a name if it does not exist. It will delete every copy of a name if it exists. Also, fill in the name of the group you are putting these into. For exmple, if you have a group name in the addressbook called “website”, enter that group name where designated below. I took the repeat out. I do not know how to run the script as you say. Start a new topic that asks about that. Maybe you can use the automator? Hope this helps!

 tell application "Mail"
	activate
	launch
	delay 5
	get name of accounts
	set mailboxName to name of accounts
	check for new mail of account mailboxName
	tell application "Mail"
		repeat until not (exists (first message of inbox whose subject is "subscribe"))
			set sendername to sender of first message of inbox whose subject is "subscribe"
			set name1 to extract name from sendername
			set address1 to extract address from sendername
			set firstname to word 1 of name1
			set lastname to word 2 of name1
			delete (first message of inbox whose subject is "subscribe")
			tell application "Address Book"
				repeat until exists person name1
					set the_person to (make new person with properties {first name:firstname, last name:lastname, email:address1})
					add the_person to group "website update" *****HERE!!!!!
					make new email at the end of the_person with properties {value:address1, label:"work"}
					save addressbook
				end repeat
			end tell
		end repeat
	end tell
	tell application "Mail"
		repeat until not (exists (first message of inbox whose subject is "unsubscribe"))
			set sendername to sender of first message of inbox whose subject is "unsubscribe"
			set name1 to extract name from sendername
			set address1 to extract address from sendername
			set firstname to word 1 of name1
			set lastname to word 2 of name1
			delete (first message of inbox whose subject is "unsubscribe")
			tell application "Address Book"
				repeat until not (exists (person name1))
					delete person name1
				end repeat
				save addressbook
			end tell
		end repeat
	end tell
end tell

cool!

THANK YOU. It work great!!!