iCal - Backup Database

i wrote this up to back up the ical database , had to use UI scripting though

just saves it to a file named iCal on the desktop.

tell application "iCal" to activate
delay 2
tell application "System Events"
	tell menu item "Back up Database." of menu "File" of menu bar 1 of application process "iCal" to click
	delay 3
	keystroke "iCal"
	keystroke "d" using command down
	delay 0.8
	keystroke return
	
end tell

This is GREAT!

I’ve added the following lines to the beginning to make a folder where the backup file would be saved, but I am having trouble making AppleScript choose the new folder as the save destination.


set l_backupfolder to "iCal Backup"

tell application "Finder"
	activate
	select window of desktop
	if not (folder (l_backupfolder) exists) then
		make new folder at desktop with properties {name:"iCal Backup"}
	end if
end tell

Since I couldn’t figure out a way to navigate through the save dialogue with AppleScript, I took the less elegant path of saving the files to the desktop first, then moving them into the backup directory. I’ve also added a date to string procedure so that the default filename-mm/dd/yy format is maintained.

-- Get system date and convert it to mm--dd--yy format --
set todaysDate to (current date)
set {d, m, y} to {day, month, year} of todaysDate

set yearString to text -2 thru -1 of ("0" & (y mod 100))

set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with i from 1 to 12
	if m = (item i of monthList) then
		set monthString to text -2 thru -1 of ("0" & i)
		exit repeat
	end if
end repeat

set todaysDate to " - " & monthString & "/" & d & "/" & yearString
-- end date procedure --


set l_backupfolder to "iCal-AddressBook Backup" & todaysDate

-- Create a backup folder on the Desktop --
tell application "Finder"
	activate
	select window of desktop
	if not (folder (l_backupfolder) exists) then
		make new folder at desktop with properties {name:l_backupfolder}
	else
		display dialog "You seem to have already backed up today. Backing up again may overwrite some files. Continue anyways?" buttons {"Cancel", "OK"} default button 2
	end if
end tell

-- Backup iCal Database --
tell application "iCal" to activate
tell application "System Events"
	tell menu item "Back up  Database." of menu "File" of menu bar 1 of application process "iCal" to click
	delay 1
	keystroke "iCal" & todaysDate
	keystroke "d" using command down
	delay 1
	keystroke return
	delay 5
end tell

-- Backup Address Book Database --
tell application "Address Book" to activate
tell application "System Events"
	tell menu item "Back up Address Book." of menu "File" of menu bar 1 of application process "Address Book" to click
	delay 1
	keystroke "Address Book" & todaysDate
	keystroke "d" using command down
	delay 1
	keystroke return
	delay 5
end tell


tell application "Finder"
	activate
	move file ("iCal" & todaysDate & ".icbu") in desktop to folder l_backupfolder in desktop
	move file ("Address Book" & todaysDate) in desktop to folder l_backupfolder in desktop
end tell

Thanks Celia,

The script doesn’t seem to work properly for me (Quicksilver, Tiger 10.4.5 and iCal 2.0.3). Sometimes it works, however most time it refused to work at all. I spent hours on it to get working (I’m an absolute starter…).
I find out that when you put this: keystroke tab between “delay 1” and “keystroke return” it works fine. Put it twice, in – Backup iCal Database – and in – Backup Address Book Database – .

Save the script as an application, link it in iCal as an application (assigning a script alarm) and let make iCal automatically scheduled backups for you.

Thanks a lot,

Leander

I get our friend

as an error!

How could I specify another folder? I don’t like having a cluttered desktop

Ok I made some modifications to the way the buttons and menus are chosen… they’re a bit more specific.

-- Backup iCal Database --
tell application "iCal" to activate
tell application "System Events"
	tell process "iCal"
		tell menu bar 1
			tell menu bar item 3
				tell menu "File"
					click menu item 10
				end tell
			end tell
		end tell
	end tell
	delay 1
	keystroke "iCal" & todaysDate
	keystroke "d" using command down
	delay 1
	keystroke "s" using command down
	delay 5
end tell
quit application "iCal"

-- Backup Address Book Database --
tell application "Address Book" to activate
tell application "System Events"
	tell process "Address Book"
		tell menu bar 1
			tell menu bar item 3
				tell menu "File"
					click menu item 12
				end tell
			end tell
		end tell
	end tell
	delay 1
	keystroke "Address Book" & todaysDate
	keystroke "d" using command down
	delay 1
	keystroke "s" using command down
	delay 5
end tell
quit application "Address Book"

Notice the “keystroke “s” using command down”. Much more specific (and effective) than just saying return.

What I’d like to do now I think is periodically make archives of the files (say, once a month) and name the archives “January 2007” or something like that, then sftp those files to a server somewhere. Is it possible to store passwords (or any other strings) not in cleartext with these applescripts?

Model: MacBook
AppleScript: 1.10.7
Browser: Firefox 2.0
Operating System: Mac OS X (10.4)

Hi,

Here’s a way without GUI scripting.

-- Make iCal Backup --
-- Makes icbu file for iCal at user's Desktop.
-- Will only work in 10.4.
-- John Maisey -- 5th September 2005 -- 1.1

set myPath to (path to desktop from user domain)
-- If you want to hard code a path to the destination folder
-- replace '(path to desktop from user domain)' in the above linewith
-- 'folder "path:to:your:folder:"'.
--set myAppSuppPath to alias ((choose folder with prompt "Locate your old Applcation Support folder.") & "iCal" as text)
set myAppSuppPath to alias (((path to application support from user domain) as text) & "iCal")

set myNowFormat to do shell script "date +%FT%TZ"
set myDayFormat to do shell script "date +%d/%m/%Y"
set myNewName to ("iCal " & myDayFormat & ".icbu") as text
set myPreamble to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" & return & "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" & return & "<plist version=\"1.0\">" & return & "<dict>" & return & "	<key>date</key>" & return & "	<date>"
set myPostamble to "</date>" & return & "	<key>version</key>" & return & "	<string>1.0</string>" & return & "</dict>" & return & "</plist>"
set myPlist to (myPreamble & myNowFormat & myPostamble)

tell application "Finder"
	if exists file myNewName of myPath then
		set myButton to button returned of (display alert "\"" & myNewName & "\" already exists" message "Would you like to replace this file?" as warning buttons {"Cancel", "Replace"} default button 2 giving up after 120)
		if myButton is "Replace" then
			delete file myNewName of myPath
		else
			return
		end if
	end if
	set myFolder to (duplicate myAppSuppPath to myPath)
	delete (files of myFolder whose name contains "sync" or name contains "alarms")
	set myInfoPlist to make new file at myFolder with properties {name:"Info.plist"}
	my WriteToFile(myInfoPlist as text, myPlist, true)
	set name of myFolder to myNewName
end tell

--handler to write text to text file with flag to clear text before
on WriteToFile(theFile, theText, clearFLag)
	try
		open for access file theFile with write permission
	end try
	if clearFLag is true then
		set eof of file theFile to 0
	end if
	write theText & return to file theFile starting at eof
	try
		close access file theFile
	end try
end WriteToFile

Best wishes

John M

John M,
How can I do this same backup routine you have scripted for iCal (without GUI scripting) for backing up Address Book?

The previous script:

tell application "Address Book" to activate
tell application "System Events"
	tell process "Address Book"
		click menu item 12 of menu 3 of menu bar 1
		delay 1
		tell sheet 1 of window 1 to click button 1
	end tell
end tell
delay 8

and other scripts like:

tell application "Address Book" to activate
tell application "System Events"
   tell process "Address Book"
       tell menu bar 1
           click menu item "Back up Address Book." of menu "File"
       end tell
   end tell
end tell

or:

tell application "Address Book" to activate
tell application "System Events"
   tell menu item "Back up Address Book." of menu "File" of menu bar 1 of application process "Address Book" to click
   delay 1
   keystroke "Address Book" & todaysDate
   keystroke "d" using command down
   delay 1
   keystroke return
   delay 5
end tell

These scripts above all give me the error:

thanks!
patrick