Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the 'Use Automator to help publish iCal calendars' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use Automator to help publish iCal calendars
Authored by: ptone on Oct 24, '05 08:59:34AM
This applescript shows you how to navigate iCal's plist file to get a list of calendars and their associated storage file. This was written to refresh a published calendar from a source file that may have been changed by another publisher on the website:

property icalSupportFolder : (path to home folder as string) & "Library:Application Support:iCal:"
property nodesPlist : icalSupportFolder & "nodes.plist"
property sources : {}
property passwds : {}

on getValue(akey, keys, values)
	if keys does not contain akey then return false
	repeat with i from 1 to count of keys
		if item i of keys = akey then return item i of values
	end repeat
	return false
end getValue

on getPW(source)
	log "PW routine"
	return my getValue(source, sources, passwds)
end getPW

--iCal should be closed to refresh
tell application "Finder"
	if name of every process contains "iCal" then
		tell application "iCal" to quit
	end if
end tell

tell application "System Events"
	--set nodesPlist to POSIX path of nodesPlist
	set calendarList to property list items of property list item "List" of contents of property list file nodesPlist
	set publishedCalendarList to {}
	--set publishedCalendarNames to {}
	repeat with i from 1 to count of calendarList
		set pubData to property list items of property list item "Publishers" of item i of calendarList
		if pubData ≠ {} then
			--set end of publishedCalendarList to i
			set end of publishedCalendarList to ((i as string) & " : " & value of property list item "PublishName" of item 1 of pubData)
		end if
	end repeat
	set chosenCalendars to choose from list publishedCalendarList with prompt "Choose calendars to refresh (hold CMD key for multiple)" with title "Published Calendars" with multiple selections allowed
	repeat with aChosenCalendar in chosenCalendars
		set calIndex to characters 1 thru 2 of aChosenCalendar as string as number
		set sourceItem to value of 
			property list item 1 of 
			property list item "Sources" of 
			property list item 1 of 
			property list item "Publishers" of (item calIndex of calendarList)
		set sourceURL to value of 
			property list item "UploadURL" of 
			property list item 1 of 
			property list item "Publishers" of (item calIndex of calendarList)
		set sourceFile to icalSupportFolder & "Sources:" & sourceItem & ".calendar:corestorage.ics" as alias
		(* --- URL Access scripting does not store pw in keychain
		tell application "URL Access Scripting"
			download sourceURL to sourceFile replacing yes with authentication
		end tell
		--using curl for download and storing pw
		if sourceURL contains "@" then --authentication steps
			set username to characters 8 thru ((offset of "@" in sourceURL) - 1) of sourceURL as string
			if sources contains sourceItem then
				set passwd to my getPW(sourceItem)
				display dialog "Please enter your password" with title "Web Authentication" default answer "" buttons {"OK, Always", "OK, Once"} default button 1 with hidden answer
				set theResult to result
				set passwd to text returned of theResult
				if button returned of theResult = "OK, Always" then
					set end of sources to sourceItem
					set end of passwds to passwd
				end if
			end if
			set sourceFile to POSIX path of sourceFile
			set sourceFile to quoted form of sourceFile
			set userpass to username & ":" & passwd
			set sourceURL to "http://" & characters ((offset of "@" in sourceURL) + 1) thru -1 of sourceURL as string
			set shellcommand to "curl -u " & userpass & " " & sourceURL & " -o " & sourceFile
			do shell script shellcommand
			--no auth needed
		end if
	end repeat
	tell application "iCal"
		reload calendars
	end tell
end tell

[ Reply to This | # ]