Skip navigation

converting the date to make iCal appointment

317 Views 4 Replies Latest reply: Aug 8, 2012 11:13 AM by b3nnb RSS
b3nnb Calculating status...
Currently Being Moderated
Aug 7, 2012 12:20 PM

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 ",")


##Make the ical event

          tell application "iCal"

                    tell calendar "Home"

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

  date theDate


                              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 make new mail alarm at end of mail alarms of newEvent with properties {trigger interval:alarmTime}

                              end tell


                    end tell

          end tell


end run

Applescript, Mac OS X (10.7.4)
  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    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

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    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


More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.