At work I connect my laptop to the network using old-school cat5 cables because there is no wireless available. However, at home I use wireless. Every morning when I get to work I open my MacBook Pro and go to the System Preferences -> Sharing -> Internet to start sharing the internet using AirPort because I like to scrobble the morning’s tracks from my iPod Touch before I open iTunes and tuck into the rest of the day’s music. It’s just part of my morning routine.
This only has to happen because when I get home again and I want to upload some of the day’s photos to Flickr[1] I have to turn internet sharing off so I can connect to my home wireless. This is a process I’ve streamlined (using Quicksilver) down to seven keystrokes and six mouse clicks (starting internet sharing) or seven keystrokes and two clicks (stopping it again). But it was still too much. In fact, I found it a total pain. For a while now I’ve been looking for a solution to make things faster. Today I finally cobbled one together using tips from few different places, and here’s how to do it.
To get the process down to nine keystrokes (plus your system password) for starting and ten keystrokes for stopping internet sharing we’re going to use three of Apple’s goodies[2]:
- AppleScript
- Shell scripting
- Quicksilver
Let’s get started.
Saving Your Settings
First of all, open System Preferences -> Sharing and get internet sharing setup and started the way you like it. Then open terminal and type the following:
cd /Library/Preferences/SystemConfiguration/ [enter] sudo cp com.apple.nat.plist com.apple.nat.on.plist [enter] [here you will need to enter your administrator password]
Setting Up The Shell Script
Now open the text editor of your choice[3] and paste in the following[4]:
1 2 3 4 5 6 7 8 9 10 | #!/bin/sh . /etc/rc.common ## # Start up Internet Sharing ## sudo cp /Library/Preferences/SystemConfiguration/com.apple.nat.on.plist /Library/Preferences/SystemConfiguration/com.apple.nat.plist ConsoleMessage "Starting Internet Sharing" /usr/libexec/InternetSharing |
Save that file as StartIS.sh into the directory ~/Library/Application Support/Quicksilver[5] for user later on[6].
Setting Up The AppleScript
Open a fresh document in your text editor or in Apple’s Script Editor program[7].
Paste in the following[8]:
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | using terms from application "Quicksilver" on process text cmd my growlRegister() if cmd = "on" then tell application "System Events" to activate tell application "System Events" set the_username to do shell script "whoami" set the_password to "password" display dialog "You now need to enter the password for the currently logged in account: " & the_username & " This account must have Administrator access to this computer." default answer "password" buttons {"OK", "Cancel"} default button "OK" with icon 2 with title "Password" with hidden answer set the_password to text returned of the result end tell set results to do shell script "sudo sh ~/Library/Application\\ Support/Quicksilver/StartIS.sh" password the_password with administrator privileges growlNotify("Internet Sharing Started", "") else tell application "System Preferences" activate end tell tell application "System Events" delay 1 tell process "System Preferences" click menu item "Sharing" of menu "View" of menu bar 1 delay 3 tell window "Sharing" if (exists tab group 1) then tell tab group 1 click radio button "Internet" delay 1 click button "Stop" delay 1 end tell end if end tell end tell end tell ignoring application responses tell application "System Preferences" to quit end ignoring growlNotify("Internet Sharing Stopped", "") end if end process text end using terms from using terms from application "GrowlHelperApp" -- Register Growl on growlRegister() tell application "GrowlHelperApp" register as application "is" all notifications {"Alert"} default notifications {"Alert"} icon of application "Script Editor.app" end tell end growlRegister -- Notify using Growl -- Example: growlNotify("This is an Alert","This is a test of the Growl Alert System") on growlNotify(grrTitle, grrDescription) tell application "GrowlHelperApp" notify with name "Alert" title grrTitle description grrDescription application name "is" end tell end growlNotify end using terms from |
Save this file as Internet Sharing.scpt in the directory ~/Library/Application Support/Quicksilver/Actions[9].
Some parts of this huge block of code probably deserve a bit of an explanation, so here goes. If you’re not interested in the explanation feel free to skip ahead to the next major heading.
I’ll start at the logical place, the end, just to get it out of the way. If you’re using OS X and you haven’t got it yet, you should get Growl. I’ve added Growl notification to the script, just because I can[10]. So everything from the line using terms from application "GrowlHelperApp" down is all about Growl notifications.
Now let’s start from the top. First of all you’ll notice we’re using this script through Quicksilver. It won’t work in is present form as a stand alone script not accessed through QS, but that’s certainly an option if you want to fiddle around with it. When you invoke this script through QS (more on that later) QS will pass the script a piece of text which the script uses to decide if you’re trying to turn internet sharing on or off. Which, unfortunately have to be done in very different ways.
Turning Internet Sharing On
To turn on internet sharing the AppleScript uses the shell script we made earlier, and it needs your system admin password. So the first thing it does is pop up a dialog asking for your password before it continues[11]. Then it goes off and runs the shell script and gives you a Growl notification that internet sharing has been turned on.
Turning Internet Sharing Off
Turning off proves to be much more complicated (or at least much more annoying). The AppleScript code turns internet sharing off by actually opening the preferences pane and simulating clicks on all the relevant buttons. Consequently it takes time, and annoyingly pops up the preferences pane. But it’s still easier than opening it up manually and clicking manually, so I’m counting it as a win. If you know a better way to do this, please leave a comment and let me know.
Using the Script
All that’s left to do is to restart Quicksilver so it picks up your new script in the Actions folder and it’s done.
To turn on internet sharing the new way, simply activate Quicksilver hit the “.” to get into text mode, type on and tab to the actions pane and type is to get to Internet Sharing[12] and hit enter. You’ll get a password dialog to enter your system admin password, which you should do. You should then get a Growl notification to tell you it has been started.
To turn it off is even simpler. Invoke QS hit “.” (it doesn’t matter what text is there, so long as it’s not on tab to the actions pane type and is and hit enter. You’ll notice the Sharing Preferences Pane appears but feel free to click away and continue doing your thing. It should take about ten seconds and once it’s complete you should get a Growl notification again to say internet sharing is off.
Caveats
I’m no expert. Seriously. It took me ages to figure this out, and since there didn’t seem to be any readily available solution out there, I made it available. This works for me, on my OSX Tiger system. If you’ve got a Tiger too, then I don’t see any reason it shouldn’t work for you, but it might not. It also might break something for you even though it hasn’t broken anything for me. If that happens you’re on your own.
Update
If this isn’t working for you. One reason might be that GUI scripting is turned off. You can turn it on using the AppleScript Utility (”/Applications/AppleScript/AppleScript Utility.app”).
- Or any number of other evening type tasks. [↩]
- You might think that sounds like more, rather than less. But for me, one mouse click is about equal to ten keystrokes (possibly more) and crucially there is now no mouse involved. [↩]
- I recommend TextWrangler. [↩]
- This script has been modified from a tip over at Mac OSX Hints, with some modifications similar to, but not the same as suggested in one of the comments by kmschindler. [↩]
- In my case that’s
/Users/simon/Library/Application Support/Quicksilver. [↩] - If you’re not using Quicksilver (a) you don’t know what you’re missing out on, and (b) these scripts won’t work for you as presented here, but rest assured they can work without QS given a little bit of modification. [↩]
- Here I’d recommend the Script Editor option since we’re doing AppleScript and it will format things nicely for you when you save your document. [↩]
- Unfortunately, this is much more long winded than I was hoping because I couldn’t find a way to turn internet sharing off using a shell command. If you know a way to do this I would love to hear about it in the comments. [↩]
- In my case that’s
/Users/simon/Library/Application Support/Quicksilver/Actions. [↩] - Or because someone else can and I’m good at copying. [↩]
- Many thanks to Aric for explaining how to run sudo shell scripts from AppleScript. [↩]
- Depending on the specific actions available in your QS setup
ismight not get you there, but you’ll soon find it if you fully typeInternet Sharing. [↩]
0 Responses to “Using Quicksilver to start or stop internet sharing in OS X Tiger”
Leave a Reply