Help scripting to check if an iCal refresh all calendars is completed!

I am writing a script to refresh all calendars, sync mobile me, wait until the refresh and sync are completed, close all open apps and finally shut down the Mac. I have gotten all the other pieces of the script in place except for a delay test to see if the calendar refresh has completed. Any help would be appreciated. Below is my current script (it may not be pretty - I am new to scripting):

– Opens iCal and refreshes all Calendars
tell application “iCal” to activate
tell application “System Events”
tell process “iCal”
keystroke “r” using {command down, shift down}
end tell
end tell

– Starts MobileMe Sync and waits until finished
tell application “System Events”
tell process “SystemUIServer”
tell menu bar 1
set SyncMBI to (first menu bar item whose description contains “Sync”)
click SyncMBI
tell SyncMBI
tell menu 1
set SyncButton to (first menu item whose name contains “Sync Now”)
click SyncButton
end tell
end tell
end tell
end tell
end tell

property securityDelay : 5

repeat until mobileMeIsSyncing() – checks if sync has started
delay 1
end repeat

repeat
repeat while mobileMeIsSyncing() – check if sync has stopped
delay 1
end repeat
delay securityDelay
if not mobileMeIsSyncing() then exit repeat – check if sync
end repeat

on mobileMeIsSyncing()
try
do shell script “/bin/ps -arxo comm | /usr/bin/grep mobilemesyncclient”
return true
on error
return false
end try
end mobileMeIsSyncing

– Closes all open Applications
– get list of open apps
tell application “System Events” to set theapps to name of every application process whose visible is true and name is not “Finder”
set keepapp to {“AppleScript Editor”}
– quit each app
repeat with closeapp in theapps
if closeapp is not in keepapp then quit application closeapp
end repeat

display dialog “Done Syncing. Ready to Shutdown.”

tell application “System Events”
shut down
end tell

Model: MacBook Pro
AppleScript: 2.2
Browser: Safari 534.48.3
Operating System: Mac OS X (10.7)