256
Logo

Mac OSX iCal Automatically Add Alarms

So I've been very frustrated for some time with Mac's iCal and it's inability to automatically add an alarm for a new event. We use Zimbra and the Zimbra connector which automagically inserts events into iCal as I get meeting requests from others. However, I often get no notification of the meeting and have missed a number of them.

I tried to use iCalFix but it didn't quite do the job so I finally decided to roll my own.

Applescript to the "Rescue"

After futzing around with Applescript I finally got the following script to work. It seems like it always takes far longer to write Applescript scripts then just about any other language. Maybe it's the lack of a good editor or just the language itself but it takes me 3x longer than I expect to get anything working.

The script basically runs through all of the recent events and gives them a sound alarm (although you can tune) this. Here's the script:

tell application "iCal"
  -- Run through only the most recent events, we assume all of the older events,
  -- even if they are repeating, will have been set before.  When you start off
  -- you may want to set the 3 to be 3000 to get all of your events.
  set minDate to (current date) - 3 * days
  -- the calendars that I am adjusting
  set adjustCalendars to {"Calendar"}
  -- run through the calendars
  repeat with myCalendar in calendars
    tell myCalendar
      -- run through all of the events in the calendar
      repeat with myEvent in events
        tell myEvent
          -- if it is a future or revent event...
          if start date comes after minDate or end date comes after minDate then
            -- fix it
            FixEvent(myEvent, minDate) of me
          end if
        end tell
      end repeat
    end tell
  end repeat
end tell

on FixEvent(myEvent, minDate)
  tell application "iCal"
    tell myEvent
      -- delete any prevous sound alarms
      repeat with myAlarm in sound alarms
        delete sound alarm 1
      end repeat
      -- make my new sound alarms YMMV
      make new sound alarm at end with properties {trigger interval:-30}
      make new sound alarm at end with properties {trigger interval:-1}
    end tell
  end tell
end FixEvent

Running It Periodically

After struggling with the Automator which I've really never gotten to work... ever. I switched to cron which I know. I saved the script into my Documents/Applescripts folder, saved it as an application (no bundle) without start screen and ran the following command to edit my crontab entry.

crontab -e

This kicks you into an editor such as emacs or vi. I don't have the time to write how-tos on those right now. I added the following lines to my crontab file. The first is a comment to explain the format.

# minute(0-50) hour(0-23) day-of-month(1-31) month(1-12) day-of-week(0-6)
20,50 8,9,10,11,12,13,14,15,16,17,18,19,20 * * 1,2,3,4,5 /usr/bin/open /Users/jsmith/Documents/Applescripts/Reset\ iCal\ Alarms.app

So this will run my script 'Reset iCal Alarms.app' at 20 and 50 minutes after the hour during work hours, Monday through Friday. This works really well but it interrupts the UI at these times. To complete the process, I found the very cool Drop Script Backgrounder application which is old but works with my 10.4. Most likely it is making small changes to the application config but the result is that the script runs in the background.

Perfect!

Free Spam Protection   Android ORM   Simple Java Zip   JMX using HTTP   Great Eggnog Recipe