Automate alarms in iCal

Discussion in 'Mac Apps and Mac App Store' started by aslauga, Apr 12, 2004.

  1. aslauga macrumors member

    Joined:
    Jan 14, 2004
    #1
    I have all my friends' birthdays automatically put into iCal from Address Book, into a separate calendar called 'Birthdays'. Is there a way to automatically make every event in this calendar alarm me five days in advance, without having to set each one manually (I have a lot of friends!) so I have time to send a card?
     
  2. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #2
    This script should work. It could probably be simplified, but I got most of the code from another script.

    Code:
    tell application "iCal"
    	set iCalendars to {}
    	set theCals to {}
    	set theCals to calendars
    	repeat with i from 1 to count of theCals
    		copy title of item i of theCals to end of iCalendars
    	end repeat
    	
    	set i to 1
    	repeat with currentCalendar in iCalendars
    		if item i of iCalendars is equal to "Birthdays" then
    			set CalNum to i
    			exit repeat
    		end if
    		set i to i + 1
    	end repeat
    	
    	set currentCal to item CalNum of theCals
    	set theEvents to events of currentCal
    	repeat with i from 1 to count of theEvents
    		make new display alarm at end of display alarms of item i of theEvents with properties {trigger interval:-7200}
    	end repeat
    end tell
    To use it, go to /Applications/AppleScript/Script Editor and paste it there. Then click the run button. The script has the following limitations:
    *The calendar "Birthdays" must be there, and with that exact name.
    *It doesn't delete existing alarms, just adds a 5-day-before alarm to each existing event.
     
  3. aslauga thread starter macrumors member

    Joined:
    Jan 14, 2004
    #3
    Thank you so much - that was exactly what I wanted! One thing, how would I make it only add an alarm if there wasn't one there already, and also automatically run when a new even is added to the Calendar?

    On a broader note, where can I learn how to use AppleScript myself?
     
  4. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #4
    Here's the adjusted code to only add an alarm to events that have no [display] alarm:

    Code:
    tell application "iCal"
    	set iCalendars to {}
    	set theCals to {}
    	set theCals to calendars
    	repeat with i from 1 to count of theCals
    		copy title of item i of theCals to end of iCalendars
    	end repeat
    	
    	set i to 1
    	repeat with currentCalendar in iCalendars
    		if item i of iCalendars is equal to "Birthdays" then
    			set CalNum to i
    			exit repeat
    		end if
    		set i to i + 1
    	end repeat
    	
    	set currentCal to item CalNum of theCals
    	set theEvents to events of currentCal
    	repeat with i from 1 to count of theEvents
    		if (count of display alarms of item i of theEvents) is equal to 0 then
    			make new display alarm at end of display alarms of item i of theEvents with properties {trigger interval:-7200}
    		end if
    	end repeat
    end tell
    I'm not sure how to automatically run it every time you make a new event. AFAIK, iCal has no Scripts menu, but you can open /Applications/Applescript/Install Script Menu to add a Scripts menu to the right of the menu bar. Then go to Home/Library/Scripts and create a folder called 'iCal Scripts'. Save the above script there. You can then quickly run the script from the Scripts menu.

    Another option would be to use a script to create each event, which will then automatically create an alarm for it. Something like this:

    Code:
    tell application "iCal"
    	set iCalendars to {}
    	set theCals to {}
    	set theCals to calendars
    	repeat with i from 1 to count of theCals
    		copy title of item i of theCals to end of iCalendars
    	end repeat
    	
    	set i to 1
    	repeat with currentCalendar in iCalendars
    		if item i of iCalendars is equal to "Birthdays" then
    			set CalNum to i
    			exit repeat
    		end if
    		set i to i + 1
    	end repeat
    	set currentCal to item CalNum of theCals
    	
    	set {theAction, theDescription} to {button returned, text returned} of (display dialog "What is a description of the event" buttons {"OK", "Cancel"} default button "OK" default answer "New Event")
    	
    	if theAction is equal to "OK" then
    		set {theAction, theDate} to {button returned, text returned} of (display dialog "What date is this event?" buttons {"OK", "Cancel"} default button "OK" default answer "")
    		
    		if theAction is equal to "OK" then
    			set theNewEvent to make event at end of events of currentCal with properties {summary:theDescription, allday event:true, start date:date (theDate), recurrence:"FREQ=YEARLY"}
    			make new display alarm at end of display alarms of theNewEvent with properties {trigger interval:-7200}
    		end if
    	end if
    end tell
    This script prompts you for a summary and date for a new event, and then creates it in the calendar "Birthdays". It makes the event an allday event, and also makes it repeat each year. Then it creates a display alarm for the event which appears 5 days before the event.
    Note that, when you specify a date, the easiest way is to specify it in the format MM/DD/YY or DD/MM/YY, depending on your date settings. AppleScript is flexible, so you could put something like MM/D/YYYY instead.

    The way I learnt AppleScript was to look at other people's scripts and see how they did things. I then started by writing scripts with the things I understood (I think display dialog was the first thing I did), and developed on that.
    When you want to do things with a particular application, that code has to be enclosed in a tell block. In Script Editor, if you go to File->Open dictionary, then it will display a list of all scriptable applications on your computer. Select the appropriate application and it will tell you what you can do with the application (it will probably be a bit confusing at first though).
    Apple has also documented AppleScript at http://developer.apple.com/documentation/AppleScript/. Pay special attention to the 'Getting Started' link.
    Good luck, and happy scripting. :)
     
  5. About2SwitchOvr macrumors regular

    Joined:
    Mar 22, 2004
    Location:
    Boston
    #5
    Thanks for the input...greatly appreciated


    I too have been looking to learn about Scripts... I will take your advice.. THANKS!
     

Share This Page