Skip to content

iCal backup applescript

A few days ago there was an incident with my mobile phone and I lost an entire calendar in iCal. Since then I've found a new need to have daily backups. I first thought of writing a perl script to open ical and address book and backup the data to a local place. Then I realized that it would be easier and more effecient just write an apple script to do that.

So here it is. This apple script will go through the process of backing up iCal and AddressBook to a folder, tar+gzip the folder and scp it to a remote host. It is pretty simple and self explanitory. If you have any questions, just ask.

Download file

{ 14 } Comments

  1. Nice script, Nick. Of course your backups folder will get bigger and bigger...a nice enhancement would be to also trash any backups more than n days old...

  2. That is a good idea, now I just have to figure out how to do it! :)

  3. I solved the issue of purging bloated backup folders by creaing an Automator workflow which would select specified finder items and move them to the trash.

    To automate the script I created an event in iCal which would run the script once per month.

    It was far easier than writing an applescript.

  4. I've just downloaded your AppleScript and will run it as soon as I add code to maintain a shuffle of backups. My purpose in writing, however, is that this would be a great addition to ScriptBuilders, which is part of MacScripter. Would you consider registering yourself as an author at http://scriptbuilders.net, then zipping your saved script as a script or application and uploading it?

    Look forward to hosting it.

    Adam Bell

  5. Needs a bit of work - did the iCal properly, but hung on the Address book. Do you mind if I rewrite it when I have time?

  6. Yeah, feel free to rewrite it and make it better. Let me know what you come up with so I can write about it as well.

  7. What about if you have a server that requires a password to do the SCP? Is that possible, or do you have to do something on the server to permit certain users to SCP without a password?

  8. Hi, thanks for making this script. It looks great, but I'm getting a "NSReceiverEvaluationScriptError: 4" when it gets to the line telling iCal (or AddressBook) to 'click' whatever the backup menu item is. I've posted a full account of my troubles over at MacScripter here.

    If you've got any ideas I'd sure appreciate hearing them! Thanks.

    PS - I also found on MacScripter the simple way to delete files older than a certain date:

    tell application "Finder"
    delete every item of folder "path:to:folder" whose modification date is less than ((get current date) - 7 * days)
    end tell

  9. Hey Nick,

    Extremely useful script. Thanks for sharing it!

    I had to modify it a bit script so that I could differentiate the daily backups and time-stamp them:

    ...
    set l_backupfolder to ("iCal_Address_Book-backup")
    ...
    do shell script "mv ~/Desktop/backup.tgz ~/Desktop/" & (l_backupfolder & "-" & yearString & monthString & d & ".tar.gz")
    do shell script "rm -rfv ~/Desktop/" & l_backupfolder
    do shell script "scp ~/Desktop/" & (l_backupfolder & "-" & yearString & monthString & d & ".tar.gz") & " user@host:~/backup/"
    do shell script "rm -rfv ~/Desktop/" & (l_backupfolder & "-" & yearString & monthString & d & ".tar.gz")
    ...

    But I was wondering, could you tell me how to make the time-stamp look more like YYYYMMDD? Right now it's YYMMd.

    I also lost an entire calendar recently (after trying to sync with my mobile phone over bluetooth) so I added an iCal event to run your backup script every morning.

    The next step will be to modify the script to rotate backups. But since I'm more familiar with bash scripting, I was going to implement that part in a shell script.

    Thanks for sharing the great script!

  10. The problem is that Apple recently updated the Address Book and the item referred to in the script no longer exists. As far as I can tell from the "File" menu there is no longer a way to back up except to .Mac.

  11. I'm getting the "NSReceiverEvaluationScriptError: 4" error too, on a fully up to date Tiger. Both applications still have the file menu option to back up.

  12. hi nick, that's a really useful script. I can't seem to get the KEYSTROKE RETURN to work consistently though. Sometimes it actually saves the iCal backup, and sometimes I hear a system beep, seeming to indicate that the RETURN key is pressed by not activating the SAVE button.

    Is there anything I may be missing? I can't understand why else the behavior is unpredictable...

  13. I think you get NSReceiverEvaluationScriptError if you have the "Enable access for assistive devices" checkbox disabled in the Universal Access preferences. Enabling it fixes the problem. Also, changing "menubar 1" to "menubar" may help.

  14. HI, I have OSX 10.4.9 on intel, with assistive device enabled, but still get the NSReceiverEvaluationScriptError err. I tried editeding the offending script line to:
    tell menu item "Back up Address Book" of menu "File" of menu bar of application process "iCal" to click
    but it still fails. I suspect the final chars in the menu item name - how do you generate them in Script Editor ?
    thanks

    /Pete

Post a Comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Your email is never published nor shared. Required fields are marked *