I’ve read the various forum postings on this topic, and tried to comprehend and cannibalise, but I’ve hit a brick wall and am throwing myself on this community for salvation!
I’m trying to create a script which makes a similar function to ‘Laytr’, where message is sent back to you at a date on your choosing when you’ll need to be reminded of it. The script takes a messages in mail and makes an Ical event of the same, with an alarm for the date chosen by the user.
I’ve got the creation of the event ok but it’s the setting of the date for the mail alarm that’s the problem. Here’s my code:
property toDoCompletionDate : ""
set newline to ASCII character 10
gettime()
tell application "Mail"
using terms from application "Mail"
set selectedMails to selection
repeat with eachMessage in selectedMails
-- creates the event and alarm
tell application "iCal" to tell calendar "Calendar"
set theEvent to make new event at end with properties {description:message_body, summary:thetitle, start date:toDoCompletionDate, end date:toDoCompletionDate, allday event:true}
tell theEvent to make new mail alarm at end with properties {trigger date:toDoCompletionDate}
end tell
-- moves the original mail message to a holding folder
set mailbox of eachMessage to mailbox "[Google Mail]/ Projects/Remind Later" of account "Personal"
end repeat
end using terms from
end tell
on gettime()
-- get some details about the current date for comparing the new date
set theDay to day of (current date) as integer
set theMonth to month of (current date) as integer
set theYear to year of (current date) as integer
-- get the current date and month to be default text
set thedaytext to theDay as text
set thedaymonth to theMonth as text
set answery to thedaytext & "/" & theMonth
set theDefaultDate to text returned of (display dialog "Enter the date (DD/MM)" buttons {"Cancel", "Set Date"} default button "Set Date" default answer answery) as string
-- compare whether the event is ahead of or behind the current date, and if the latter, add one year to the date
set newday to text 1 thru 2 of theDefaultDate
try
set newmonth to text 4 thru 5 of theDefaultDate as integer
on error
set newmonth to text 4 of theDefaultDate as integer
end try
if theMonth is greater than newmonth then set theYear to theYear + 1
if theMonth is equal to newmonth then if theDay is greater than newday then set theYear to theYear + 1
set theDefaultDate to theDefaultDate & "/" & theYear
-- convert from DD/MM/YY into a date format
my setDate(theDefaultDate)
-- change the time from midnight to 6.30am for the mail alarm to be sent
set toDoCompletionDate to date string of date theDefaultDate & " 06:30:00"
return date toDoCompletionDate
end gettime
-- this borrowed from a previous forum post.
on setDate(theDateStr)
set {R, TID} to {false, text item delimiters}
repeat with i in {"/", "-", " "}
set text item delimiters to i
set {x, text item delimiters} to {every text item in theDateStr, TID}
if (count of x) ≥ 3 then
set {R, R's year} to {date theDateStr, item 3 of x}
exit repeat
end if
end repeat
return R
end setDate
Whenever I run this, I get this error (the date is the one I happened to enter, today:
error "iCal got an error: Can't make \"Monday, 26 September 2011 06:30:00\" into type date." number -1700 from "Monday, 26 September 2011 06:30:00" to date
All help gratefully received!
Model: Macbook 13"
AppleScript: 2.2
Browser: Safari 534.48.3
Operating System: Mac OS X (10.7)