iCal Alarms!

The future will be better tomorrow. --Dan Quayle
Welcome back to the second in our iCal series! Last time I showed you that making To Do items and Events were fairly straight-forward. Today I want to show you some nifty things you can do with alarms for Events and To Do’s. The format for each type of alarm is the same no matter whether you are attaching it to a To Do or an Event (large ‘E’), so you can apply the lessons here to either type of event (small ‘e’).

First off, let’s create a new calendar to play in. We’ll create our new events in this calendar so that we don’t mess up any of our other calendars if something goes horribly wrong. If you check the iCal dictionary (in OS X 10.4 Tiger, this may not apply to Leopard), you’ll see a command create calendar. Don’t use it. If you read the entry you will see that this command has been deprecated in favor of the usual make command.

Here, then, is our simple calendar-creator:


set theDate to short date string of (current date)
--Get calendar name
display dialog "Enter name for new calendar:" default answer "my calendar"
set newName to text returned of the result
--Get calendar description
display dialog "Enter description of this calendar:" default answer "calendar description"
set cDesc to text returned of the result

--Get calendar color
display dialog "Do you want to choose a color for the calendar?" buttons {"No", "Yes"} default button 2
if the button returned of the result is "Yes" then
	set theColor to choose color
else
	set theColor to {46003, 46003, 46003} -- med gray
end if

tell application "iCal"
	activate
	try
		make new calendar with properties {name:newName, description:cDesc, color:theColor}
		set aProblem to false
	on error the error_message number the error_number
		set aProblem to true
		display dialog "Problem creating calendar" & return & "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
	end try
	if not aProblem then display dialog "Your calendar was created successfully." buttons {"OK"} default button 1 giving up after 5
end tell

The first thing we need to do is create our test calendar. You can call it whatever you like, but I usually have one called “Applescript.” Those of you that have read my article A Tutorial to Improve Your AppleScripts: Putting It All Together will remember that I also use a playlist called “Applescript” as the target for my iTunes scripts that build playlists. If you use Applescript a lot with an application and can re-use the same object for different scripts, it is a nice, clean way to keep your scripted items away from your other, normal items, be they playlists or calendars.

We will also need to create an event to attach our alarms to. Since the procedure is the same regardless of whether we are adding an alarm to an Event or a To Do, I’ll just use an event and leave it for you to apply the alarms to To Do’s if you need to.

Let’s use this generic script for creating an event called “Scripted event” and set the time for a minute from when the script is run. We’ll attach our alarms to this event shortly.


set now to (current date)
set eStart to now + 1 * minutes
set eEnd to now + 2 * minutes
set eName to "Scripted event"

tell application "iCal"
	set newEvent to make new event at end of events of calendar "Applescript" with properties {summary:eName, start date:eStart, end date:eEnd}
end tell

Lions and Tigers and Bears, oh MY!
There are four kinds of alarms available through Applescript. If you’ve perused the iCal dictionary, you already know that they are display alarm, mail alarm, sound alarm, and open file alarm. Let’s make one of each!

A display alarm is simply an alarm that throws up an iCal message box with the text of the event. It is the most usual kind of alarm, and very simple to create:


set now to (current date)
set eStart to now + 1 * minutes
set eEnd to now + 2 * minutes
set eName to "Scripted event - display alarm"
set alarmTime to 0 -- alarm at the exact moment of the event

tell application "iCal"
	set newEvent to make new event at end of events of calendar "Applescript" with properties {summary:eName, start date:eStart, end date:eEnd}
	make new display alarm at end of display alarms of newEvent with properties {trigger interval:alarmTime}
end tell

Mail alarms have a (to me) serious deficiency - they are limited to sending the event email to whoever is listed as the event attendee (or you, if there is no attendee). Not only that, but iCal seems to want to send the email only to the first address on your Address Book card. Still, if you want an email sent to yourself (which makes sense in some context - if you were out of the office and checking your email remotely maybe?), here’s how to do it: (I’ll cover a better way to do this later on.)


set now to (current date)
set eStart to now + 1 * minutes
set eEnd to now + 2 * minutes
set eName to "Scripted event - mail alarm"
set alarmTime to 0 -- alarm at the exact moment of the event

tell application "iCal"
	set newEvent to make new event at end of events of calendar "Applescript" with properties {summary:eName, start date:eStart, end date:eEnd}
	set theAlarm to make new mail alarm at end of mail alarms of newEvent with properties {trigger interval:alarmTime}
end tell

Sound alarms also throw up a display dialog while playing the sound specified. You can either use one of the system alert sounds (in the Sound pane of your System Prefs)…


set now to (current date)
set eStart to now + 1 * minutes
set eEnd to now + 2 * minutes
set eName to "Scripted event - sound alarm"
set alarmTime to 0 -- alarm at the exact moment of the event

tell application "iCal"
	set newEvent to make new event at end of events of calendar "Applescript" with properties {summary:eName, start date:eStart, end date:eEnd}
	set theAlarm to make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime, sound name:"Sosumi"}
end tell

…or you can give a file path for a sound of your own.


set now to (current date)
set eStart to now + 1 * minutes
set eEnd to now + 2 * minutes
set eName to "Scripted event - sound alarm"
set alarmTime to 0 -- alarm at the exact moment of the event
set theFile to choose file with prompt "Choose a sound file" of type {"AIFF", "public.aifc-audio"} without multiple selections allowed and invisibles
tell application "iCal"
	set newEvent to make new event at end of events of calendar "Applescript" with properties {summary:eName, start date:eStart, end date:eEnd}
	set theAlarm to make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime, sound file:POSIX path of theFile}
end tell

Like as the waves make towards the pebbled shore,
So do our minutes hasten to their end; --Shakespeare

And so, at last, we come to the end of our discussion of alarms. The open file alarm is, as we will see, the most versatile of all the alarms. As the name suggests, you can set an alarm that will open a file at a given time. Let’s say you have a report that must be filed every day at the same time. Simply create an open file alarm that will open the document you need at the right time and you’re ready to go!

Here’s a sample script:


set now to (current date)
set eStart to now + 1 * minutes
set eEnd to now + 2 * minutes
set eName to "Scripted event - open file alarm"
set alarmTime to 0 -- alarm at the exact moment of the event
set theFile to choose file with prompt "Choose a  file for the alarm" without multiple selections allowed and invisibles
tell application "iCal"
	set newEvent to make new event at end of events of calendar "Applescript" with properties {summary:eName, start date:eStart, end date:eEnd}
	set theAlarm to make new open file alarm at end of open file alarms of newEvent with properties {trigger interval:alarmTime, filepath:POSIX path of theFile}
end tell

As you can see, it’s similar to the sound alarm with a file path. But you can do something with this kind of alarm that you cannot do with any of the others…can you guess? “Anyone? Anyone? Bueller? Bueller?” Let’s see, iCal itself will let you set one other kind of alarm if you do it in iCal and not through a script…hmmm…

Yep, you can run a script! When you select an Applescript as the target of an open file script, you will notice that the alarm type shown in iCal’s sidebar changes to “Run Script.” The only limitation seems to be that it must be a script, not a script application. Script applications don’t work.

Remember what I said earlier about the deficient mail alarm? You can write an Applescript to send any email you like when the alarm goes off and skip the mail alarm and it’s problems!

I hope you’ve thought of some nifty things to do with iCal scripting and alarms. You can create scripts to wake you up, start or stop programs, play sounds and anything else that you can do with a script.

'Til next time, have fun scripting. Crunch code!

I have struggled for days with the Open File alarm, going as far as copying the example code. I cannot get this to work with an event.

On Yosemite, there is no error (no error in the console either) when executed via the script. The Event is created, but the open file alarm is not added to the event. I can add any other type of alarm (Display, sound, etc…). I can set it manually.

For kicks I tried the sample code on my wife’s Mac Book (Mavericks), there it does not work, but I get a console error stating that I must have a file path.

I must be missing something. I’m about googled out. Any insight would really be appreciated.


set eName to "Yankees Game"

set alarmTime to 0 -- alarm at the exact moment of the event

--display dialog theFile
set theFile to POSIX path of "/Applications/HueMLB.app"

tell application "Calendar"
     set newEvent to make new event at end of events of calendar "Automator" with properties {summary:eName, start date:theDate}
     set theAlarm to make open file alarm at end of open file alarms of newEvent with properties {filepath:POSIX path of theFile, trigger date:theDate, trigger interval:alarmTime}

 --set theAlarm to make display alarm at end of display alarms of newEvent with properties {trigger interval:alarmTime}

end tell
--display dialog "alarm added"




Browser: Firefox 31.0
Operating System: Mac OS X (10.8)