4 Replies Latest reply: Aug 8, 2012 11:13 AM by b3nnb
b3nnb Level 1 Level 1 (0 points)

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

  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 (46,450 points)

    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

  • b3nnb Level 1 Level 1 (0 points)

    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

  • Camelot Level 8 Level 8 (46,450 points)

    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

  • b3nnb Level 1 Level 1 (0 points)

    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 (date theDate)

                        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 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 run