Applescript firewall
Another way round:
This isn't AppleScript, but maybe it'll help you.
You can easily build a shell script with this (let me know if you need any help on that, in case you're not used to it) and call it from/with wherever you want.
I used this with Sidekick, to automatically turn it on/off (and do a bunch of other stuff like change network location, mount network storage, time machine, screensaver passwords...) when I'm at work/home/public network/etc. The downside is that Sidekick opens a terminal in the background to execute the command, and it stays there, nicely hidden, waiting for you to enter your password. And since it doesn't pop up into the foreground it is easy to forget that it is there...
However, I find it preferable to the AppleScript method, which implies having "System Preferences" popping up and getting 'clicked' automatically. There's always a "WTF" moment when I switch locations and windows start opening up and buttons start getting clicked without me touching anything...
Now that I think of it, the Sidekick/Terminal/sudo password issue might easily be solved by adjusting the sudoers file. I'll give that a shot...
Cheers
Edit:
Adjusting the sudoers file effectively eliminates the need for entering a password to change firewall state. Just do
sudo visudo on a Terminal window and change
%admin ALL=(ALL) ALL
to
%admin ALL=(ALL) PASSWD: ALL, NOPASSWD: /usr/bin/defaults
or if you want to be the only user being able to do this without being asked for a password just leave the
%admin line alone and add one with:
myusername ALL=(ALL) PASSWD: ALL, NOPASSWD: /usr/bin/defaults
sudo will keep asking for passwords for everything except for the
defaults command.
sudo -A is another solution for the 'terminal in the background' problem I referred to. The
-A switch makes sudo ask for the password using a dialog instead of the terminal.