Archives for: April 2005, 24

04/24/05

Permalink 02:26:18 pm, Categories: Apple, 407 words   English (US)

Applescript and cron

I've been meaning to learn Applescript. It seems great for scripting all sorts of things. However, I've never got around to learning this :(. Here's my first lame Applescript application.

I have a website (duh) that I would like to backup to my Powerbook weekly. Now the easiest thing to do would be to create a backup shell script and install this in cron. That's great for a machine on 24x7, but my laptop is not. Hmm, ok so let's use anacron. That will make sure my backup script runs. But then I thought, what if I suspend my powerbook during a backup. I wouldn't be able to know when the backup was running.

Ok, so how about bringing up a dialog before the backup starts (and ideally during the backup, but I couldn't figure this out). So began a sojourn into Applescript. I found this article describing cronning iTunes, and it's somewhat related to what I need to do. How do I display a dialog? Granted I would probably know this if I actually learned Applescript, but I found display dialog.

I brought up Script Editor and started playing with it. I'm impressed with how intuitive it is to write these scripts. It has a cool record function also, which I didn't use. I ended up with the script:

tell me
	activate
end tell

display dialog "Backup process will run in a few seconds"
   giving up after 3
do shell script "~/Backups/website/backup.sh >/dev/null"

The 'tell me' stuff was needed to bring the script to the foreground when it runs from cron. I found this by digging in this document. I had to save this as a 'Application' in Script Editor so I could run it from the command line.

I already had anacron installed, but the next issue was that it was only running root's scripts (periodic). I ended up adding a script to /etc/periodic/weekly/ with contents:

#!/bin/sh

su - virajalankar -c /path/to/backup.app

This would run my Applescript as the proper user. It's sort of a clumsy solution though. I should probably put the backup logic in backup.app rather than a separate shell script. But like I said, I don't know alot of Applescript.

The end result is this script gets run 'weekly', and brings up a dialog just before it runs. It won't do much good if I don't have an Internet connection when it runs though :(.

Viraj's Weblog

Donate to keep this site going!

Amount USD $

April 2005
Mon Tue Wed Thu Fri Sat Sun
<<  <   >  >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  

Search

Categories


Misc

Syndicate this blog XML

What is RSS?

powered by
b2evolution