Re: New to applescript: year of
Re: New to applescript: year of
- Subject: Re: New to applescript: year of
- From: has <email@hidden>
- Date: Thu, 28 Sep 2006 11:54:49 +0100
Yvan KOENIG wrote:
why this doesn't work:
repeat with myMessage in messages of mailbox "myMailbox"
set myYear to year of date received of myMessage
log myYear
end repeat
It seems that some application's AppleScript's implementation is lazy.
This slightly modified code works for me:
set myYear to year of ( get date received of myMessage)
Right solution, though wrong reason. AppleScript's 'implicit get'
behaviour is the culprit here.
The line 'set myYear to year of date received of myMessage' doesn't
work because when AppleScript does its implicit get, it sends the
entire reference - 'year of date received of message 1 of mailbox
"myMailbox"' - to Mail. However, scriptable applications only
understand references to the properties and elements of application
objects; they don't understand references to properties and elements
of AppleScript values (strings, lists, dates, etc.). So Mail gets as
far as resolving 'date received of message 1 of mailbox "myMailbox"',
but because the 'date received' property contains an AppleScript
value, not an application object, it can't go any further; hence the
error when it tries to resolve the 'year of' part of the reference.
You have to force AppleScript to send the right reference ('date
received of message 1 of mailbox "myMailbox"') to Mail by inserting
an explicit 'get' command into your code at the appropriate point.
Mail wil then return an AppleScript date value, and AppleScript
(which does know how to manipulate date values) can obtain the value
of its year property.
tell application "Mail"
set {laSource, laDestination} to {"zzz", "xyz"}
set k to count of messages of mailbox laSource
repeat with i from k to 1 by -1
set myMessage to message i of mailbox laSource
set myYear to year of (get date received of myMessage)
log myYear
if myYear = 2004 then set mailbox of myMessage to mailbox
laDestination
end repeat
end tell
That'll work, but it'll be slow. Much quicker to get Mail to move all
the messages in one go:
on makeDate(y, mo, d, h, mi, s)
(*
Make new date object given, year, month, day, hour, minute and
second as integers (month can also be given as a constant). Doesn't
use date strings, so isn't affected by users' Date & Time preferences
(i.e. localisation-independent).
*)
set dt to date (get "1")
set monthNames to {January, February, March, April, May, June, July,
August, September, October, November, December}
if mo is not in monthNames then set mo to item mo of monthNames
set {dt's year, dt's month, dt's day} to {y, mo, d}
set dt's time to h * hours + mi * minutes + s
return dt
end makeDate
set myYear to 2005
set {laSource, laDestination} to {"zzz", "xyz"}
set startDate to makeDate(myYear, 1, 1, 0, 0, 0)
set endDate to makeDate(myYear + 1, 1, 1, 0, 0, 0)
tell application "Mail"
move (every message of mailbox laSource whose date received ≥
startDate and date received < endDate) to mailbox laDestination
end tell
has
--
http://freespace.virgin.net/hamish.sanderson/
http://appscript.sourceforge.net
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden