Scripting iCal 1.5.1

Any example scripts out there for scripting iCal? I have already done some that I am using with Indigo but don’t quite have a handle on it.

There’s only one at scriptbuilders

http://scriptbuilders.net/category.php?search=remote-ical

I have so far used most of my examples from Blank Rebel Productions and their iCal Script Pack. This has been the most helpfully. It has some good examples on how to create Calendars and events.

Beyond that I want to cycle through the events in a calendar and remove any that have certain name. All my efforts have failed.

Hi there,

I have some code that will allow you to cycle through your events, if you don’t have it already. I included an example on how to delete an event.

The script exports the calendar as a tab delimited file. This way, I can email it to myself at work and import it into Outlook. I still have a few additions to make to it.

The code to cycle through the events:

set allCalendars to calendars
repeat with thisCalendar in allCalendars

if title of thisCalendar is selectedCalendar then --repeats until it finds the selected calendar
set allEvents to events in thisCalendar
repeat with thisEvent in allEvents – repeats for each event
if summary of thisEvent is equal to “Testing” then delete thisEvent

Of course, you need your end repeats.

I hope this is of assistance.

-- iCal to tab delimited file
-- I am creating this since Microsoft Outlook does not correctly import (it doesn't even try) iCal files like it is suppose to.

-- This is the header to the tab delimited file (tdf).
set TDFHEAD to "Subject	Start Date	Start Time	End Date	End Time	All day event	Reminder on/off	Reminder Date	Reminder Time	Meeting Organizer	Required Attendees	Optional Attendees	Meeting Resources	Billing Information	Categories	Description	Location	Mileage	Priority	Private	Sensitivity	Show time as"
set BEGINNINGDATE to (current date) - 30 * days --set the beginning date here
set ENDDATE to (current date) + 60 * days --set the end date here
set DEFAULTCALENDAR to "Schedule" as string -- set the default calendar name here
set tdfText to TDFHEAD & return -- this variable will have the tdf  events concatenated to it, then written to a text file afterwards
-- set tdfText to "" & return -- for testing purposes only

tell application "iCal"
	activate -- activates iCal
	display dialog "Enter the calendar to export: " default answer DEFAULTCALENDAR buttons {"Cancel", "OK"} default button 2 -- prompts user to pick a calendar to export, default set as "Schedule"
	copy the result as list to {selectedCalendar, buttonPressed}
	set allCalendars to calendars
	repeat with thisCalendar in allCalendars
		if title of thisCalendar is selectedCalendar then --repeats until it finds the selected calendar
			set allEvents to events in thisCalendar
			repeat with thisEvent in allEvents -- repeats for each event
				set thisDate to start date of thisEvent
				set stopDate to end date of thisEvent
				if (thisDate is greater than BEGINNINGDATE) and (thisDate is less than ENDDATE) then -- only executes if date is within range
					set tdfText to tdfText & summary of thisEvent & tab -- subject
					set tdfText to tdfText & my dateString(thisDate) & tab -- start date
					set tdfText to tdfText & time string of thisDate & tab -- start time
					set tdfText to tdfText & my dateString(end date of thisEvent) & tab -- end date
					set tdfText to tdfText & time string of stopDate & tab -- end time
					set tdfText to tdfText & my booleanCaps(allday event of thisEvent as string) & tab -- all day event?  calls a function to convert to caps
					set tdfText to tdfText & "False" & tab & my dateString(thisDate) & tab & time string of thisDate & tab & tab & tab & tab -- for now, not including reminder and attendees
					set tdfText to tdfText & tab & tab & tab -- for now, not including Meeting Resources, Billing Information, and Categories
					set tdfText to tdfText & description of thisEvent & tab -- description
					set tdfText to tdfText & location of thisEvent & tab -- location
					set tdfText to tdfText & tab -- not including mileage 
					set tdfText to tdfText & "Normal	False	Normal	1" & return -- I don't know what these are for, so I'm just putting whatever for Private Sensitivity and Show Time As
					-- display dialog tdfText buttons {"Cancel", "OK"} default button 2
				end if
			end repeat
		end if
	end repeat
	-- display dialog selectedCalendar buttons {"Cancel", "OK"} default button 2
	
end tell

tell application "Finder" --write to text file
	set frontmost to true
	set filename to choose file name with prompt "What do you want to call the file? Also, choose location. "
	set fileref to open for access filename with write permission
	write tdfText to fileref
	close access fileref
end tell

on booleanCaps(x)
	if x is equal to "true" then
		return "True"
	else
		return "False"
	end if
end booleanCaps

on dateString(aDate)
	set x to "" as string
	
	set y to month of aDate as string
	if y is equal to "January" then set x to x & "1" & "/"
	if y is equal to "February" then set x to x & "2" & "/"
	if y is equal to "March" then set x to x & "3" & "/"
	if y is equal to "April" then set x to x & "4" & "/"
	if y is equal to "May" then set x to x & "5" & "/"
	if y is equal to "June" then set x to x & "6" & "/"
	if y is equal to "July" then set x to x & "7" & "/"
	if y is equal to "August" then set x to x & "8" & "/"
	if y is equal to "September" then set x to x & "9" & "/"
	if y is equal to "October" then set x to x & "10" & "/"
	if y is equal to "November" then set x to x & "11" & "/"
	if y is equal to "December" then set x to x & "12" & "/"
	set x to x & day of aDate & "/" & year of aDate as string
	
	return x
end dateString

[/b]

Great! I will give it a try!

Having had a significant hand in AquaMinds’ NoteTaker application’s AppleScript implementation, I know of some interesting uses for scripting iCal.

http://www.aquaminds.com/scripts.jsp

You might also go there and download some of these scripts which deal with iCal interaction between it and NoteTaker.

THANKS. Great examples for me to work with.

However, it is funner to just create them yourself. :wink:

Just kidding. I see no need to reinvent the wheel. Thanks again. :smiley:

I have tried to integrate Danielsan event deletion code into my exisiting script. But it errors out. Maybe someone can suggest where the problem is where it says “–delete events that already exist”


[This script was automatically tagged for color coded syntax by Script to Markup Code]

The other day I was contacted by Pete Boardman who wrote a very nifty iCal script to search events for some string, then gather the start and end times of all the events that match and add them together. He uses iCal to track projects he’s working on and can use the script to determine how much time he’s worked on a particular project. I augmented the script a bit (see below). Pete’s weblog can be found here:

[url=http://homepage.mac.com/pete.boardman/weblog/]http://homepage.mac.com/pete.boardman/weblog/[/url]

The permanent link to the script page is here:

[url=http://homepage.mac.com/pete.boardman/weblog/100023.html]http://homepage.mac.com/pete.boardman/weblog/100023.html[/url]

The augmented script follows (note, if you have 10.3, use the “short date string” lines for improved (in my opinion) output):

Jon


[This script was automatically tagged for color coded syntax by Script to Markup Code]

I’ve simplified my deleteing code to this:


[This script was automatically tagged for color coded syntax by Script to Markup Code]

However the event window shows me this:


tell application "iCal"
	count every calendar
		3
	count item 1 of every calendar
		1
	get summary of item 1 of item 1 of every calendar
		"iCal got an error: NSCannotCreateScriptCommandError"

Your deletion routine could probably be simplified to just:

Jon


[This script was automatically tagged for color coded syntax by Script to Markup Code]

That did the trick Jon. I’m still trying to get the hang of Applescript so that’s why you see some weird stuff in my code.