Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

converting the date to make iCal appointment

Hey Gang,


Im working on a script to mac iCal appointments that have an email alarm to send out a templated email.

I am unable to figure out what im missing to get the date to be recognized.


The goal of the entire script is:

popup - ask for the date of the appointment

popup - ask for the eMails of the persons involved

and then make an ical alarm to send an email out, the day before the actual appointment.


i feel there is a way better way for me to be working with the date in this situation but cant sort out what way.

any help would be grateful


on run

set y to (year of (current date) as string)

set m to (month of (current date) as string)

set m2 to month of ((current date) + 30 * days) as string


##get day of month

set theDay to (choose from list {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} ¬

with prompt "Select the Day of the appointment") as text

set theMonth to (month of (current date) as integer)



##Get the email list

set emailList to ""

set emailList to text returned of (display dialog "List the eMails of Presentation participants" default answer "example@me.com, second@me.com")


##Make the ical event

tell application "iCal"

tell calendar "Home"

set theDate to (theDay & "/" & theMonth & "/" & y) as string


datetheDate


make new event at end with properties {description:"Event Description", summary:"Event Name", location:"Event Location", start date:theDate, allday event:true}


set now to theDate

set eventStart to theDate

set eventEnd to theDate + 2 * minutes

set eventName to ("Presentation for " & emailList) as string

set alarmTime to (now - 1 * days) as string


tell application "iCal"

set newEvent to make new event of calendar "Home" with properties {summary:eventName, start date:eventStart, end date:eventEnd}

set theAlarm to makenewmail alarmat end of mail alarms of newEventwith properties {trigger interval:alarmTime}

end tell


end tell

end tell


end run

Applescript-OTHER, Mac OS X (10.7.4)

Posted on Aug 7, 2012 12:20 PM

Reply
Question marked as Best reply

Posted on Aug 7, 2012 1:14 PM

There are many requirements about constructing dates in AppleScript, and different ways of doing it. Unfortunately, your approach:


set theDate to (theDay & "/" & theMonth & "/" & y) as string


is not one of them. It's not acceptable to create a date in this format because of the ambiguity. Does the string "4/6/2012" translate to April 6th or 4th June?

Even assuming the OS will honor your system's settings it's still invalid because the script could be run on someone else's machine, with some other date definition.


Therefore, when creating dates you have to be completely unambiguous.


The best way of doing this is to not use strings at all. Don't even use month names because they vary from language to language.

So, looking at your script it seems that you're trying to create three dates - an EventStart based on a user-selected day in the current month, an EventEnd based on EventStart + 2 minutes, and AlarmTime which is one day prior to EventStart.

(Note there is an inconsistency in your alarm setup. Alarms can either use a 'trigger interval' which is defined as an offset from the event time, or a 'trigger date' which is an absolute point in time. You'll need to change your alarm creation code accordingly, but for this example I'll assume you want a trigger date.

Given those details, this should get you pointed in the right direction:


set theDay to (choose from list {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31})


-- Now we have the data we need..

-- so start by creating a new date object, based on the current date

set eventStart to (get current date)

-- and change its day based on the user selection

set day of eventStart to theDay


-- now eventStart is a valid date object, with the day set to the appropriate value

-- from here we can contstruct the other dates:


set eventEnd to eventStart + (2 * minutes)

set alarmTime to eventStart - (1 * days)


--Now you have your three dates that you can plug into the rest of your script

4 replies
Question marked as Best reply

Aug 7, 2012 1:14 PM in response to b3nnb

There are many requirements about constructing dates in AppleScript, and different ways of doing it. Unfortunately, your approach:


set theDate to (theDay & "/" & theMonth & "/" & y) as string


is not one of them. It's not acceptable to create a date in this format because of the ambiguity. Does the string "4/6/2012" translate to April 6th or 4th June?

Even assuming the OS will honor your system's settings it's still invalid because the script could be run on someone else's machine, with some other date definition.


Therefore, when creating dates you have to be completely unambiguous.


The best way of doing this is to not use strings at all. Don't even use month names because they vary from language to language.

So, looking at your script it seems that you're trying to create three dates - an EventStart based on a user-selected day in the current month, an EventEnd based on EventStart + 2 minutes, and AlarmTime which is one day prior to EventStart.

(Note there is an inconsistency in your alarm setup. Alarms can either use a 'trigger interval' which is defined as an offset from the event time, or a 'trigger date' which is an absolute point in time. You'll need to change your alarm creation code accordingly, but for this example I'll assume you want a trigger date.

Given those details, this should get you pointed in the right direction:


set theDay to (choose from list {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31})


-- Now we have the data we need..

-- so start by creating a new date object, based on the current date

set eventStart to (get current date)

-- and change its day based on the user selection

set day of eventStart to theDay


-- now eventStart is a valid date object, with the day set to the appropriate value

-- from here we can contstruct the other dates:


set eventEnd to eventStart + (2 * minutes)

set alarmTime to eventStart - (1 * days)


--Now you have your three dates that you can plug into the rest of your script

Aug 8, 2012 7:59 AM in response to Camelot

Thanks. what about adding the selection of the current month and next month. as the date selection will be a week time slot at a time, there may be overlap

i tried this but it was not working.

eg. run this on a friday to input the dates that go up to one week ahead no more


set m to month of (current date) as string

set m2 to month of ((current date) + 30 * days) as string


set theMonth to (choose from list {m, m2})



set month of eventStart to theMonth

Aug 8, 2012 9:41 AM in response to b3nnb

I saw that you were playing with months, but weren't using that anywhere else in your script, so I assumed it was superfluous.


I'm not quite sure I understand what you want, though.


what about adding the selection of the current month and next month. as the date selection will be a week time slot at a time


If events are one week long then you don't care about months, you care about days.


For example, to set a StartDate of tomorrow at 10am, and an endDate one week later you could:


set startDate to (get current date) + 1 * days

set endDate to startDate + 7 * days


If you're in the last week of a month, e.g. the 28th August, and you add 7 days, AppleScript will automatically work out the change in month, so you don't need to.


If you do want to manipulate months, you still need to get out of the mindset of using strings. Months are not strings.


If you want to schedule an event 'next' month:


set startDate to (get current date)

set curMonth to month of startDate as integer-- use the month index

set nextMonth to curMonth + 1 -- now you can use simple arithmetic

set month of startDate to nextMonth

set endDate to startDate + 7 * days

Aug 8, 2012 11:13 AM in response to Camelot

so the user interaction has changed. your scripts will help me in the future though. Thanks

I re-wrote the start but for whatever reason it wont make the iCal event with alarm.

it says unable to get date of Calendar 'Home'

and using the code

(date theDate) work with the other script. i only used that as it was in a textbook.



##Get the Date

set theDate to text returned of (display dialog ¬

"Enter the Date" with title ¬

"Presentation Reminder" with icon 1 ¬

default answer ¬

"d/m/year" buttons {"Cancel Apt", "Make Apt"} ¬

default button 2)

set now to (date theDate)


##Get the email list

set emailList to ""

set emailList to text returned of (display dialog "List the eMail's of Presentation participants" default answer "example@me.com, example2@me.com")


##Make the ical event

tell application "iCal"

tell calendar "Home"


--date theDate

make new event with properties {description:"Event Description", summary:"Event Name", location:"Event Location", start date:(date theDate), allday event:true}


set eventStart to (datetheDate)

set eventEnd to ((date theDate) + 2 * minutes)

set eventName to ("Presentation for " & emailList) as string

set alarmTime to (now - 1 * days) as string

set newEvent to makenewevent of calendar "Home" with properties {summary:eventName, start date:eventStart, end date:eventEnd}

set theAlarm to makenewmail alarmat end of mail alarms of newEventwith properties {trigger interval:alarmTime}

end tell


end tell

end run

converting the date to make iCal appointment

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.