AppleScript & Mail : rajouter expéditeur carnet d'adresse

Squelettor

Membre enregistré
12 Janvier 2008
8
0
www.perdu.com
Bonjour,

J'aimerai rajouter les expéditeurs des mail que je reçois dans le carnet d'adresse. Je veux faire ça avec une règle Mail. J'appelle donc dans la règle le script suivant :

Bloc de code:
using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theSender to sender of eachMessage
				set theSenderAddress to address of theSender
				set theSenderName to theSenderAddress
				set theGroupName to "Add_Auto"
				add2AddresseBook(theSenderName, theSenderAddress, 
theGroupName)
			end repeat
		end tell
	end perform mail action with messages
end using terms from

on add2AddresseBook(theName, theAddress, theGroup)
	tell application "Address Book"
		set theGroup to group theGroupName
		set theNewPerson to make new person at end of people
		set first name of theNewPerson to theName
		tell theNewPerson to make new email at end of emails with properties {label:"home", value:theSenderAddress}
		add theNewPerson to theGroup
	end tell
end add2AddresseBook

Quand la règle passe, il ne se passe rien...
Y a-t-il un fichier de log quelque part ?
Une idée ??
Merci d'avance
 
Salut,

J'ai modifié quelques lignes :

- ajout de "my" devant l'appel de la fonction (l'appel doit être précédé de "my" lorsqu'il est dans un bloc tell)

- dans la fonction tu utilises les variables "theSenderAddress" et "theGroupName", les variables sont locales à moins que tu ne les déclares globales, chose que tu n'as pas fait et donc elle n'ont pas leur place dans la fonction…

. une remarque, tu pourrais ne passer que 2 arguments à la fonction puisque le nom et le mail sont identiques…

Bloc de code:
using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theSender to sender of eachMessage
				set theSenderAddress to address of theSender
				set theSenderName to theSenderAddress
				set theGroupName to "Add_Auto"
				my add2AddresseBook(theSenderName, theSenderAddress, theGroupName)
			end repeat
		end tell
	end perform mail action with messages
end using terms from

on add2AddresseBook(theName, theAddress, theGroup)
	tell application "Address Book"
		tell group theGroup
			set theNewPerson to make new person at end of people
			set first name of theNewPerson to theName
			tell theNewPerson to make new email at end of emails with properties {label:"home", value:theAddress}
		end tell
	end tell
end add2AddresseBook

ps : je ne l'ai pas essayé donc… :siffle:
;)
 
Merci pour la réponse.

Ca marchais toujours pas mais après d'autres recherches, voilà la solution qui marche :

Bloc de code:
using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with theMessage in theMessages
				set theSender to sender of theMessage
				set theAddress to (extract address from theSender)
				set theName to (extract name from theSender)
				set theGroupName to "Add_Auto"
				my add2AddresseBook(theName, theAddress, theGroupName)
			end repeat
		end tell
	end perform mail action with messages
end using terms from


on add2AddresseBook(theName, theAddress, theGroupName)
	tell application "Address Book"
		set thePerson to make new person with properties {first name:theName}
		tell thePerson
			make new email at end of emails with properties {label:"Home", value:theAddress}
		end tell
		--make new group with properties {name:"Add_Auto"}
		add thePerson to group theGroupName
		save addressbook
	end tell
end add2AddresseBook

Un lien intéressant : http://www.mactech.com/articles/mactech/Vol.21/21.10/ScriptingAddressBook/index.html