Automate an Internet content filter
Aug 03, '10 07:30:00AM
Contributed by: Anonymous
As an adult with ADD, I'm a huge fan of Steve Lambert's SelfControl. This nifty app, for those who don't know, is a great boon to the Internet addict and to easily distractible folks such as myself. It is a timed Internet filter that can block sites based on a whitelist or blacklist. Once you turn it on, it's virtually impossible to deactivate until its timer runs out. So, if I wanted to work on a project for 3 hours without checking Twitter or Facebook, I would simply add facebook.com and twitter.com to SelfControl's list of blocked sites, set its timer for 3 hours, and get some real work done.
I find it helpful to be able to automate this app, so that I don't change my mind and decide to blow off work at the beginning of the day. To this end I have an AppleScript scheduled to activate SelfControl every weekday morning while I'm still sleeping, which lasts until mid-day, so I can get stuff done when I wake up, rather than just roll out of bed onto Facebook.
- Naturally, you'll need SelfControl.
- You'll also need to download and install Daniel Jalkut's Usable Keychain Scripting to quickly and securely access your admin password in order to activate SelfControl. As of this writing, OS X's current Keychain Scripting is rather slow.
You can now run selfcontrol.scpt easily from Quicksilver, put it in the Dock, or schedule it in iCal. All of these methods will activate SelfControl (with its current blacklist or whitelist setting) for 45 minutes, or for whatever you set the default time to be.
- Open Keychain Access. Create a new password by typing Cmd-N or going to File » New Password Item...
- Under Keychain Item Name, type 'SelfControl.' Make up any Account Name in the next field, and in the Password field, type the password for your user account (the password you would use to authenticate anything in OS X). Click 'Add', and you should now have a new application password entitled 'SelfControl' in your Keychain.
- Open AppleScript Editor and paste in the following code:
on run argv
set defaultTime to 1
set myTime to item 1 of argv as number
set myTime to defaultTime
tell application "Usable Keychain Scripting"
tell current keychain
set myPass to (password of first generic item ¬
whose name contains "SelfControl")
-- eliminate invisible characters, or "gremlins," from password
set x to quoted form of myPass
set myPass to do shell script "echo " & x & " | perl -pe 's/[^[:print:]]//g'"
tell application "SelfControl" to activate
tell application "System Events"
tell process "SelfControl"
tell slider of window "SelfControl" to set value to myTime
click button "Start" of window "SelfControl"
tell window 1 of process "SecurityAgent"
with timeout of 15 seconds
set tryAgain to false
set value of text field 2 of scroll area 1 of group 1 to myPass
set tryAgain to true
if not tryAgain then exit repeat
click button 2 of group 2
- Run the script once; you will be prompted to allow Usable Keychain Scripting to access your keychain. Click 'Always Allow.' Note: This will activate SelfControl for 1 minute, so don't navigate away from this page during that time!
- In your script, at the line that says defaultTime to 1, change 1 to 45 (or whatever number of minutes you'd like the default time to be.) I use 45, so that I can run this script hourly: work 45 minutes, play for 15. Now save the script and name it something like selfcontrol.scpt.
The script can also be run with arguments from the command line to achieve different timings. I like to have SelfControl automatically run from 7-10am every weekday morning, so my crontab has the following line in it:
0 7 * * 1,2,3,4,5 osascript /Applications/kerfi/Applescript/selfcontrol.scpt 180
[crarko adds: I haven't tested this one, although I can think of a number of applications for it.]
Mac OS X Hints