Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nfcatt

macrumors member
Original poster
May 7, 2006
42
0
Hi Guru's

What is the best way to export a single calendar out using AS? I want a script to mimic the action of selecting File -> Export in iCal..

Thanks in advance,

Nelson.
 

xUKHCx

Administrator emeritus
Jan 15, 2006
12,583
9
The Kop
iCal doesn't support calendar exporting via applescript. You could possibly do it the GUI scripting (which is a bit messy)

However,

I think the best bet is to just grab the file from ~/Library/Application Support/iCal/ then have a root around for the appropriate the corestorage.ics (The .plist file in the folder will reveal the name of the calendar). If you are on tiger than you can just spotlight for corestorage.ics it is a bit of a hunt around to find the right one (if you have lots of calendars.)

Then you can use applescript to copy and rename it to the where ever you want.
 

nfcatt

macrumors member
Original poster
May 7, 2006
42
0
Hi,

Thought I'd have to do the way you suggest... I was hoping I had misread the dictionary for iCal and missed the export command! (Still a newbie at AS)

Cheers,

Nelson.
 

xUKHCx

Administrator emeritus
Jan 15, 2006
12,583
9
The Kop
Hi,

Thought I'd have to do the way you suggest... I was hoping I had misread the dictionary for iCal and missed the export command! (Still a newbie at AS)

Cheers,

Nelson.

It is a rather glaring omission to iCal's applescript support. It's good that you went looking through the dictionary first.
 

nfcatt

macrumors member
Original poster
May 7, 2006
42
0
Absolutely - it's a little messy to have to rely on a variable file location in order to access the calendar as a file...
I'm ultimately trying to write a quick script which will upload via FTP a copy of the calendar to my webserver... (replacing my uses for .mac one step at a time!!!)

Nelson..
 

xUKHCx

Administrator emeritus
Jan 15, 2006
12,583
9
The Kop
Absolutely - it's a little messy to have to rely on a variable file location in order to access the calendar as a file...
I'm ultimately trying to write a quick script which will upload via FTP a copy of the calendar to my webserver... (replacing my uses for .mac one step at a time!!!)

Nelson..

Is it just one specific calendar or all of you calendars?. And do you want this to work on any machine or do you not mind it being bespoke to yours?.

Because you could set it up so that it searches for all corestorage.ics and then play around with them to get them organised how you wish.
 

nfcatt

macrumors member
Original poster
May 7, 2006
42
0
Hi,

It is only for my machine - and yes it woud be better if i could have AS search thru all of the folders and find the one that holds a particular calendar..

I tried to see if the UID of each calendar as returned by it's properties was the same as the folder name, but they do not match, so short of having a script which reads each plist file looking for the correct title...I'm not sure how to set it up..

Loving the challenge of learning AS though!!!

Thanks,

Nelson.
 

nfcatt

macrumors member
Original poster
May 7, 2006
42
0
My Solution

Hi,

Just in case anyone wants to know.. This is what I came up with to export a selected calendar out of iCal. The script basically has a default calendar it will export - in my case Roster. If I want to export a different calendar it will look up what calendars I have and ask me to select one. It then checks thru all the calendar folders, finds the correct file and places it on the desktop.. Not the neatest code, but it works..

Nelson..


Code:
--path to ical calendar folders
set initial_path to ((path to home folder) & "Library:Application Support:iCal:Sources") as string

--get list of folders
set folder_list to {}
set folder_list to list folder initial_path without invisibles

--open each folder, checking each info.plist file for the name of the calendar I want
-- default calendar to be published is "Roster", but can be changed
set myCalendar to "Roster"
display dialog "Calendar " & "\"" & myCalendar & "\"" & " will be Published" buttons {"OK", "Change", "Quit"} default button "OK" with title "Calendar Publisher"
set button_pressed to button returned of result
if button_pressed is "Quit" then
	return
else if button_pressed is "Change" then
	tell application "iCal"
		activate
		set visible of window 1 to false
		set all_calendars_names to the name of every calendar
		choose from list all_calendars_names with prompt "Publish Which Calendar?" with title "Calendar Publisher"
		if result is not false then
			set myCalendar to item 1 of result
			if myCalendar = "Birthdays" then
				display dialog "Can't Publish The Birthdays Calendar" buttons {"Quit"}
				return
			end if
		else
			return
		end if
	end tell
end if

set num_chars to (count of items in myCalendar) as string
set x to ""
set found_folder to ""

repeat with x in folder_list
	set theFile to initial_path & ":" & x & ":info.plist"
	set file_contents to (read alias theFile)
	set srch_string to characters 279 thru (279 + (num_chars - 1)) of file_contents as string
	
	if srch_string contains myCalendar then
		set found_folder to characters 206 thru 241 of file_contents as string
		exit repeat
	end if
end repeat


--set the full path to the wanted calendar
set theCalendar to initial_path & ":" & found_folder & ".calendar" & ":corestorage.ics" as string

--duplicate the file and rename it to the name of the Calendar
tell application "Finder" to (duplicate alias theCalendar to (path to desktop folder) with replacing)
set name of result to myCalendar & ".ics"

--upload to the ftp server
 

xUKHCx

Administrator emeritus
Jan 15, 2006
12,583
9
The Kop

I came up with a couple of solutions that pull the name of the calendars out of the Info.plist. Different approach to your solution, presenting it here for learning.

This solution pulls all of the calendars and then asks the user which ones to "export". Can easily be adapted to always export one or all of the calendars. Again not the cleanest of code (the doubling up at the end) but the code is short so didn't see the need to clean it up any further. This does however rely on undocumented features of iCal which I wouldn't be surprised if they were altered in Leopard.

Code:
set pathtoapp to POSIX path of (path to application support from user domain)
set pathtodesk to POSIX path of (path to desktop)
set pathtosources to quoted form of (pathtoapp & "iCal/Sources")

set infolist to paragraphs of (do shell script "find -f " & pathtosources & "| grep -v “/Contents” | grep Info.plist$") -- produces a list of all the calendars that have a Info.plist file present
set calstartlist to {}
set caldestlist to {}
set calnamelist to {}
try
	set desfolder to (do shell script "cd ~/Desktop ; mkdir Calendars") -- tries and make the Calendar folder at the desktop
end try

repeat with calname in infolist --repeats through the results of the above search
	set calname to calname as string
	set calname to text 1 thru -7 of calname
	set calstart to quoted form of (text 1 thru -5 of calname & "corestorage.ics") --creates the path to the original .ics file
	copy calname to end of calstartlist
	set calname to quoted form of calname
	try
		set calname to (do shell script "defaults read " & calname & " Title") -- reads the INfo.plist file to grab the name of the calendar as set in iCal
		set caldest to quoted form of (pathtodesk & "Calendars/" & calname & ".ics") --creates the destination path
		copy calname to end of calnamelist
		copy caldest to end of caldestlist
	end try
end repeat

set calexport to choose from list calnamelist with multiple selections allowed
repeat with calname in infolist --repeats through the results of the above search
	set calname to calname as string
	set calname to text 1 thru -7 of calname
	set calstart to quoted form of (text 1 thru -5 of calname & "corestorage.ics") --creates the path to the original .ics file
	copy calname to end of calstartlist
	set calname to quoted form of calname
	try
		set calname to (do shell script "defaults read " & calname & " Title") -- reads the INfo.plist file to grab the name of the calendar as set in iCal
		set caldest to quoted form of (pathtodesk & "Calendars/" & calname & ".ics") --creates the destination path
		if calname is in calexport then
			do shell script "ditto " & calstart & " " & caldest --copies  to the desktop while renaming it to the name given in iCal	
		end if
	end try
end repeat
 

nfcatt

macrumors member
Original poster
May 7, 2006
42
0
Thanks again!

I will "pull apart" your code and see if I understand what's going on.. truly appreciate the input!

Nelson.
 

xUKHCx

Administrator emeritus
Jan 15, 2006
12,583
9
The Kop
Thanks again!

I will "pull apart" your code and see if I understand what's going on.. truly appreciate the input!

Nelson.

This is exactly how I am learning my way through applescript, so thought you'd might appreciate an alternative method. If you want any help in understanding then feel free to ask. Would've posted sooner but semi-forgot about this thread, also you are pretty quick at responding but suppose if you are on "Spy" then i guess it is pretty easy to spot a thread you started.

Rich
 

nfcatt

macrumors member
Original poster
May 7, 2006
42
0
Thanks Rich, I'll yell out if I have trouble!!

Great feature of the forum is I get an email telling me when someone has replied to this thread..I'm sure there's a way to 'subscribe' to other threads as well..

Nelson.
 

Mac Virgin

macrumors newbie
Mar 27, 2006
15
0
How do I copy and paste the code posted?

Hi guys, my old "nfcatt" username email died and have reverted to my old login of Mac Virgin..

How do I easily copy the code so I can paste it into Script Editor??

I've got to be missing something here!!

Thanks again!!

Nelson..
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
A while back I wrote some code that almost perfectly duplicated the File > Export command. It uses the private CALCore framework that iCal uses. If you want it (it's Cocoa), let me know.

How do I easily copy the code so I can paste it into Script Editor??

Select the first few lines, then scroll to the bottom, hold shift, and select to the end.
 

Mac Virgin

macrumors newbie
Mar 27, 2006
15
0
Of course that's how to do it!!

I would love to have a look at that code you wrote - thankyou.

Nelson..
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Of course that's how to do it!!

I would love to have a look at that code you wrote - thankyou.

Nelson..

Here is the short version without much error checking. I haven't checked to see if it still works, but I'm sure it does. I used class-dump to figure out the methods. You will need to link against CALCore.framework (in /System/Library/PrivateFrameworks)

Edit: instead of duplicating the code here, it is already on CocoaDev - http://www.cocoadev.com/index.pl?CALCore (forget I had it on there)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.