On 2006-09-28, at 11:22:21, Christopher Nebel wrote:
On Sep 27, 2006, at 9:48 PM, Yvan KOENIG wrote:
It's the first thing I tried but it failed.
set {laSource, laDestination} to {"zzz", "xyz"}
tell application "Mail"
set k to count of messages of mailbox laSource
if k > 0 then
repeat with myMessage in (every message of mailbox laSource)
set my_message to contents of myMessage
if year of (get date received of my_message) = 2005 then move my_message to mailbox laDestination
end repeat
end if
end tell
In general, it's not safe to modify a collection while you're iterating over it. It can be done, but you have to be careful about how you do it. One way is to use an explicitly index-based loop and count backwards ("repeat with i from (count messages) to 1 by -1" ). Another way is to get the list of messages into a local variable and then iterate over that ("set ml to every message; repeat with m in ml"), though that does not work for collections that use index-based specifiers, and it uses more memory at once. A third way -- the best one, assuming it works -- is to not use a loop at all and use a "whose" specifier, though how to do that in this particular case is a bit circuitous thanks to the "year of" problem ("move every message whose date received is greater than date "1/1/2005" and whose date received is less than date "1/1/2006").
Thanks,
That's largely what I was trying to communicate to Yvan in my previous post.
then the container is altered -- it no longer contains the number of items it thought it had when it started -- and the container count is compromised.
etc.
Another thing I noticed with any working variant of this script is that they will fail with an error dialog if no messages are found. Therefore if you'd rather fail gracefully when there is no match, then wrap them in a 'try' block or something like:
set sbox to "dumb"
set dbox to "date"
script mail
on MoveMessages(leSource, laDest)
tell application "Mail"
set mlist to {}
tell mailbox leSource
set mcount to count of messages
repeat with m from 1 to mcount
set d to (date received of message m) as date
set y to year of d
set mon to month of d as integer
if (y > 2007 and mon > 1) then
set end of mlist to message m
end if
end repeat
end tell
if ((count of mlist) > 0) then move mlist to mailbox laDest
end tell
end MoveMessages
end script
tell mail to MoveMessages(sbox, dbox)