(*
Damien Bonvillain 2004/05
Create a new rule based on the List-Post header of an email received from a mailing-list.
In theory, should work, but in practice fails miserabily. From my experiments, it seems that
making a new rule condition with "header key" is buggy, as the rule condition ends up being for
the sender of the mail.
So, after the folder and the rule are created, one must edit the rule condition, change the "sender is" to "List-Post" header (by creating it if needed).
Created from Create New Mailing List Mailbox
Copyright © 2003 Apple Computer, Inc.
*)
using terms from application "Mail"
on perform mail action with messages msgs
tell application "Mail"
set listIds to {}
set senderHeaders to {}
repeat with msg in msgs
set headerKey to "List-Post"
set listId to (my get_header_from_message(headerKey, msg))
if listId is not "" then
set listName to content of item 1 of listId
set i to my index_of(listName, ":")
set listName to text from character (i + 1) to character (length of listName) of listName
set i to my index_of(listName, "@")
set listName to text from character 1 to character (i - 1) of listName
try
set mboxName to "Mailing Lists/" & listName
try
set res to display dialog "Enter the mailbox name:" default answer (mboxName as string)
set mboxName to text returned of res
on error
return
end try
set mbox to mailbox named mboxName
get name of mbox
on error
if mboxName is not "" then
make new mailbox with properties {name:mboxName}
set mbox to mailbox named mboxName
end if
end try
-- Now set up the rule for the mailbox
set listIdHeader to content of item 1 of listId
set newRule to make new rule at end of rules with properties {name:"Mailing List - " & listName}
set should move message of newRule to yes
set move message of newRule to mbox
delete rule conditions of newRule
set myRuleCondition to make new rule condition at beginning of (rule conditions of newRule) with properties {rule type:header key, header:headerKey, expression:listIdHeader, qualifier:equal to value}
set enabled of newRule to true
log headerKey
display dialog "A new rule for this mailing list has been created."
else
display dialog "The selected email address does not appear to be from a mailing list. There is no List-Post header."
end if
end repeat
end tell
end perform mail action with messages
on get_header_from_message(desiredHeader, theMessage)
tell application "Mail"
set hdrs to (headers of theMessage)
repeat with hdr in hdrs
if name of hdr is desiredHeader then
return contents of hdr
end if
end repeat
return ""
end tell
end get_header_from_message
(*
on run
tell application "Mail"
set selMsgs to selection
end tell
tell me to perform mail action with messages selMsgs
tell application "Mail"
properties of item 1 of rule conditions of the last item of rules
end tell
end run
*)
end using terms from
on index_of(haystack, needle)
repeat with i from 1 to count of characters in haystack
if character i of haystack is needle then
return i
end if
end repeat
return 0
end index_of
Bug ou erreur de codage ? Toujours est-il que même en rajoutant List-Post dans la liste des en-têtes valables pour les règles, le make new rule condition échoue silencieusement et crée une règle bâtarde. En plus je n'ai pas trop confiance en moi avec ce langage si éloigné de la syntaxe C, je ne me repère pas dans cette absence de ponctuation ; où mettre les conjonctions, n'en manque-t'il pas une ? Pourquoi est-ce que Mail faillit silencieusement dans quasiment tous les cas d'erreur que j'ai introduit ? Pas très rassurant tout ça.
Mise à jour 2005/02/07. Benjamin propose la modification suivante :
set theCond to make new rule condition tell theCond (* set rule type to «constant erutthdk» set «property rhed» to listHeader *) set rule type to «constant erutthdk» set «property rhed» to headerKey set qualifier to equal to value set expression to listIdHeader end tellA tester. Dommage que je me sépare de mon Mac dans peu de temps.

une réaction
1 De Benjamin Ragheb - 06/02/2005, 19:14
I have solved the problem!
In "...properties { header:headerKey ... }" the header property is being interpreted by the AppleScript compiler as «property mhdr» but in this context it should be «property rhed» instead. My solution is to create the rule condition this way:
set theCond to make new rule condition
tell theCond
(*
set rule type to «constant erutthdk»
set «property rhed» to listHeader
*)
set rule type to «constant erutthdk»
set «property rhed» to headerKey
set qualifier to equal to value
set expression to listIdHeader
end tell
I repeated those two lines as the comments because, once compiled, they will be transformed into their text form. You will need to copy and paste them again each time you recompile the script.