Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automate an Internet content filter Internet
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.

Prerequisites:
  • 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.
The Hint:
  • 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
     
     try
      set myTime to item 1 of argv as number
     on error
      set myTime to defaultTime
     end try
     
     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'"
      end tell
     end tell
     
     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"
      end tell
      tell window 1 of process "SecurityAgent"
       with timeout of 15 seconds
        repeat
         set tryAgain to false
         try
          set value of text field 2 of scroll area 1 of group 1 to myPass
         on error
          delay 1
          set tryAgain to true
         end try
         if not tryAgain then exit repeat
        end repeat
        click button 2 of group 2
       end timeout
      end tell
     end tell
    end run
    
  • 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.
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.

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.]
    •    
  • Currently 2.94 / 5
  You rated: 1 / 5 (32 votes cast)
 
[30,885 views]  

Automate an Internet content filter | 38 comments | Create New Account
Click here to return to the 'Automate an Internet content filter' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automate an Internet content filter
Authored by: arcticmac on Aug 04, '10 05:27:11AM

Hm... sounds cool. I think if I used it though, I'd get distracted trying to figure out how it works and disable it, instead of with the problem websites...



[ Reply to This | # ]
Automate an Internet content filter
Authored by: ericcartman on Aug 04, '10 12:13:33PM

thanks. i knew about the app switcher and i knew you could quit within it, but i didn't know you could hide. so thanks again.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: bluesdance on Aug 06, '10 02:54:28PM
The "quoted form of" bit made the script crash. When I removed that and changed the line of code to set x to myPass, the script worked. I don't know what's different about that but it worked. Anyone know how to use iCal to run a script with a command line argument?

[ Reply to This | # ]
Automate an Internet content filter
Authored by: karljam on Oct 04, '10 08:57:54PM

Thanks for the addition. Did you find out about how to send arguments to scripts in iCal? I only used cron because I couldn't figure that one out.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: gdelfino on Aug 08, '10 08:53:06PM

For me, what I have found to work is to automatically generate anti-distractions recurrently.

Let me explain. Suppose that I need to get some work done using a set of applications, for example: TextMate, Skim, Mathematica, Finder, Illustrator and Photoshop. And my distractions come from the other applications (Safari, etc..)

This AppleScript will detect when you are using a distracting application every 2 minutes. And it will remind you get back to work, if you fail to swhtch your app after two reminders it activates your default work apps effectively interrupting your distraction.


tell application "GrowlHelperApp"
	set the allNotificationsList to {"Work Reminder"}
	
	set the enabledNotificationsList to {"Work Reminder"}
	
	register as application "GD Work Reminders" all notifications allNotificationsList default notifications enabledNotificationsList
	
	set cycles_lost to 0
	
	repeat
		delay 120
		set app_name to the name of (info for (path to frontmost application))
		set real_work_apps to {"TexShop.app", "Skim.app", "Textmate.app", "Finder.app", "Mathematica.app", "Adobe Illustrator.app", "Adobe Photoshop CS3.app"}
		if (real_work_apps does not contain app_name) then
			notify with name "Work Reminder" title "Get back to work" description "Remember to work on whatever need to do. " & app_name application name "GD Work Reminders" image from location "file:///Users/gdelfino/Pictures/Look_a_Distraction_Design_by_eecomics.jpg"
			set cycles_lost to cycles_lost + 1
		else
			set cycles_lost to 0
		end if
		if cycles_lost > 2 then
			tell application "TextMate" to activate
			tell application "Skim" to activate
		end if
	end repeat
end tell


[ Reply to This | # ]
Automate an Internet content filter
Authored by: thirstypixels on Aug 30, '10 10:36:11AM

I'm one of those, "I use Photoshop and other graphics programs heavily but I have no idea how they actually work," kinda computer user... so here's the question: I want to automate SelfControl so that it only lets me use the internet from 3-4 and 9-10 pm. what should I write in the code to make it do that? Thanks so much. I'm extremely excited about using this program - I have a fancy desktop with a gorgeous Wacon Cintiq screen that I do my work on and I hate how distracting it is! I tried to get my network card removed and learned that it's attached to the motherboard! A piece of paper never gives me the option to look up microcephaly and whatever else is related for hours on end. Thanks a million.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: thirstypixels on Sep 01, '10 02:17:51PM

I also had a hard time getting the script to work and I don't think I quite understood how to install the key-chain thingy... help!



[ Reply to This | # ]
Automate an Internet content filter
Authored by: karljam on Oct 04, '10 11:10:32PM

Once you've downloaded Usable Keychain Scripting and double-clicked on the .dmg file, go to Finder, click on the disk image named "UKS 1.0b3" in the left column, and there you will find a file named "Usable Keychain Scripting." To install, drag it to any folder on your computer. I keep mine in /System Folder/Scripting Additions/.


If you really don't want to deal with keychains, you could just save your admin password in the script itself, though I would not recommend it. In that case you could delete all the code from

tell application "Usable Keychain Scripting" 
all the way up to, but not including
tell application "SelfControl" to activate
and replace it with this line:
set myPass to "yourpassword"

Keep in mind "yourpassword" must be replaced with your admin password. It's simple, but dangerous, as it leaves your password out in the open. You might want to change the permissions of the directory containing your script to be only readable by you.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: gunnersmurf on Nov 21, '10 07:45:31PM

When I click on Usable Keychain Scripting nothing happens.

I have placed in in a folder under documents. I click... nothing. But if I try to delete it... it says it's running.

Dunno where tho.

Can someone explain?



[ Reply to This | # ]
Automate an Internet content filter
Authored by: karljam on Mar 15, '11 07:39:20AM

That's all you need to do. You don't need to run it. Just drop it somewhere on your computer, and your applescript will automagically find it.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: karljam on Oct 04, '10 10:31:42PM

Until someone figures out how to run the script with variables in iCal, I use the unix utility cron as mentioned in the hint. Here's how:

  1. Open Terminal and type crontab -e. (Terminal will then fire up a unix-based text editor.)
  2. In the next screen, move the cursor down to a blank line and paste this:
    0 16 * * 1,2,3,4,5 osascript /Applications/kerfi/Applescript/selfcontrol.scpt 300
    0 22 * * 1,2,3,4,5 osascript /Applications/kerfi/Applescript/selfcontrol.scpt 1080
  3. Edit both lines to reflect the actual path of the script on your computer.
  4. Exit the text editor and save. Depending on the editor, this command can vary; I'll venture to guess it's Pico or Nano, in which case you can just type Ctrl-X followed by Y.
You can now quit Terminal. These two lines you pasted translate into two scheduled jobs. The first one runs at 16:00 on every day of every month, as long as it's Monday-Friday (1,2,3,4,5) for 240 minutes (that is, until 21:00). The second one runs at 22:00 for 1020 minutes (until 15:00). To have the script run on weekends as well, you could replace 1,2,3,4,5 with a *. To delete these jobs, just go back into crontab and delete these two lines.

Cron is great for scheduling just about anything, including backing up certain folders to cleaning up your desktop. For more info check out this hint.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: karljam on Oct 04, '10 10:45:12PM

I forgot to mention: If you want to block all internet with SelfControl, go to its main window, click "Edit Blacklist," and choose the "Whitelist" option at the bottom right corner of the domain list.

Now, whenever you run your script, SelfControl will block all websites except those listed on your domain list. If there are no websites listed, you've got a perfect internet blocker, which makes it functionally the same as the commercial app Freedom. Who says Freedom isn't free? :)



[ Reply to This | # ]
Automate an Internet content filter
Authored by: redmacuser on Sep 03, '10 01:33:34PM

I couldn't get the script to work. I followed the directions exactly but got the text below in the replies section of AppleScript Editor (I replaced the password for my MacBook with PASSWORD below):



tell application "AppleScript Editor"
get item 1
end tell
tell application "Usable Keychain Scripting"
get password of generic item 1 of current keychain whose name contains "SelfControl"
--> "PASSWORD"
do shell script "echo 'PASSWORD' | perl -pe 's/[^[:print:]]//g'"
--> error number -1708
«event ascrgdut»
--> error number -1708
do shell script "echo 'PASSWORD' | perl -pe 's/[^[:print:]]//g'"
--> error number -10004
end tell
tell current application
do shell script "echo 'PASSWORD' | perl -pe 's/[^[:print:]]//g'"
--> "PASSWORD"
end tell
tell application "SelfControl"
activate
end tell
tell application "System Events"
set value of slider of window "SelfControl" of process "SelfControl" to 1
click button "Start" of window "SelfControl" of process "SelfControl"
--> button "Start" of window "SelfControl" of application process "SelfControl"
set value of text field 2 of scroll area 1 of group 1 of window 1 of process "SecurityAgent" to "PASSWORD"



Then the lines below just repeated endlessly.

set value of text field 2 of scroll area 1 of group 1 of window 1 of process "SecurityAgent" to "PASSWORD"
--> error number -1719 from window 1 of process "SecurityAgent"


Anyone know hot to fix the problem above?



[ Reply to This | # ]
Automate an Internet content filter
Authored by: karljam on Oct 04, '10 10:34:15PM
The latest version of SelfControl, released after this hint, allows only multiples of 15 minutes - so my original post will generate this error. I've submitted a fix, but for now, you can replace this line...
set defaultTime to 1
with this:
set defaultTime to 15
...or just choose a length of time over 15 (minutes). It may have to be a multiple of 15 as well. If you are just testing it out, make sure you don't need to surf in the next 15 minutes!

[ Reply to This | # ]
Automate an Internet content filter
Authored by: masterninja01 on Oct 16, '10 01:47:34AM

I am trying to run this script but get this error each time:

System Events got an error: Can’t set process "SelfControl" to 45



[ Reply to This | # ]
Automate an Internet content filter
Authored by: earl556089 on Nov 01, '10 08:34:03AM

Could someone please provide me with some more detailed instructions on setting this script to automatically run at a certain time? I am a novice to scripting and do not quite understand what a crontab is. Thanks.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: karljam on Nov 15, '10 07:57:35AM

Hi earl,

Check out my comment above for details on how to use cron to automatically run the internet filter at specific times.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: gbird on Feb 08, '11 09:29:25AM

To make this script work with iCal:

1.- create a new calendar
2.- create a new event
3.- select the time at you want the selfcontrol.scpt runs
4.- clic in 'alarm' select 'run script' option and select the selfcontrol.scpt.
5.- you can select when you want to repeat this event: each monday, all week days, etc. Or you can create one event in the morning and other in the evening, etc.
6.- done



[ Reply to This | # ]
Automate an Internet content filter
Authored by: carina_st on May 29, '11 03:44:36PM
Maybe there has been an OS X update in the meantime but for whatever reason, getting the password as described here did not work for me, I kept getting the following error message: "Usable Keychain Scripting got an error: AppleEvent timed out.". So I just tried to use the normal Keychain Scripting instead and removed everything that didn't seem absolutely necessary to me (to avoid errors for now) and now the script works. Maybe they finally fixed the Keychain Scripting.

I replaced
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'"
  end tell
 end tell
with
tell application "Keychain Scripting"
	tell current keychain
		set myPass to (password of first key whose name is "SelfControl")
	end tell
end tell


[ Reply to This | # ]
Automate an Internet content filter
Authored by: carina_st on May 29, '11 03:52:55PM
An addition to my last post: when I wanted to run my script via the shell it did not work at first. The error message was "Keychain Scripting got an error: Application isn’t running. ". The solution for this problem can be found here: http://support.apple.com/kb/TA23348?viewlocale=en_US So I added the following code before trying to get the password from the keychain and it works now also when being called from the shell.
	-- Quit Keychain Scripting if it's running, since opening this script may have implicitly launched it.	
	try
		tell application "System Events"
			set foundApp to first process whose name is "Keychain Scripting"
			set pid to id of foundApp -- this will exit the try block if the process isn't running
			quit foundApp -- we get here if the process is found
		end tell
	end try
	
	-- get Finder to launch Keychain Scripting
	tell application "Finder"
		open application file ((startup disk as string) & "System:Library:ScriptingAdditions:Keychain Scripting") as alias
	end tell


[ Reply to This | # ]
Error Message
Authored by: cmanna285 on Aug 24, '11 04:16:54PM

For some reason when I run the script it says "AppleSript Error" and "System Events got an error: Access for assistive devices is disabled." I don't know what the assistive device is that's disabled or how to go about changing it so that it will work. I know NOTHING about computers and this is the first time I've ever actually entered into a the AppleScript editor so I'm pretty much at a loss. I really like the idea of this and want to apply it to my iCal as well. I noticed that some of the responses are over a month old - if someone could give me a speedy response that would be MUCH appreciated. I really hope I can figure this out before school starts for me and I can be working with SelfControl while at school. Thanks for your help.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: macgrunt on Aug 25, '11 09:02:11PM
Hi cmanna

• go to your Applications folder and open System Preferences
• click on Universal Access (probably near the bottom right)
• check the box next to "Enable access for assistive devices"

hope this helps

m.

[ Reply to This | # ]
Automate an Internet content filter
Authored by: tangmusi on Sep 03, '11 01:42:09AM

I think I did everything right, yet I get this error from SelfControl: "Error -60031 received from the Security Server." And this error from applescript: "System Events got an error: Can’t get button "Start" of window "SelfControl" of process "SelfControl"."

I have the correct passwords in there.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: tangmusi on Sep 21, '11 04:32:41PM

OK, figured that one out. :)



[ Reply to This | # ]
Automate an Internet content filter
Authored by: kevindosi on Sep 18, '11 03:59:30AM

Sorry for being an idiot, but how do I make this repeat? For instance, I want it to run for 30 minutes, pause for 5 minutes then start again for 15 more minutes.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: oatmealsnap on Oct 18, '11 01:58:01PM

Sometimes when I run this script, SelfControl will open, but the "Start" button won't work. Even if I click it myself, nothing happens. I need to quick SelfControl, open it back up again, and then the script will press "Start" and fill in the password for me. Any ideas why this might be happening?

It also won't work at all if SelfControl is already running. Any idea if there is an updated version out there?



[ Reply to This | # ]
Automate an Internet content filter
Authored by: dmoore on Jan 27, '12 08:15:51AM

This doesn't seem to work for me in OSX Lion.

I have the Cron setup to start on 8am M-F for 3 hrs.
0 8 * * 1,2,3,4,5 osascript /Users/myname/Library/Scripts/selfcontrol.scpt 180

I have Usable Keychain Scripting installed into Users/myname/Library/ScriptingAdditions

I entered my password into Keychain Access for "SelfControl" item, always allowing access to Usable Keychain Scripting.

When the cron runs, SelfControl starts, sets itself to 3 hours, and then sits there. The "Start" button is not clicked. I have to manually click it. This defeats the purpose of the entire script, as I can just never click "Start" and go about wasting time.

If I go into AppleScript Editor, open the selfcontrol.scpt file, and click "Run", the script runs properly, opens SelfControl, presses Start, and all is well. Why doesn't it work with the Cron?



[ Reply to This | # ]
Password problem
Authored by: vinogd on Feb 23, '12 01:52:55AM

For me, everything seems to run OK when I run it from iCal. Except for one thing, the password when it starts SelfControl is wrong... I need to write it manually after it tried to open automatically and had a wrong password. Where am I doing something wrong?

Also, if my computer is off when it is scheduled to start is it already running if I open my computer after the scheduled start time or do I have to leave my computer running for it to start on the scheduled time?



[ Reply to This | # ]
password problem 2.0
Authored by: vinogd on Feb 23, '12 03:09:51AM

OK, I figured out that I had put the wrong password for the keychain. So it runs well now. I scheduled it to start at 8.30 in the morning for 6h. Then it starts again at 14.30 for 6h. I have access to the "bad" websites from 20.30 until 22.45, then it turns on again until 4.45 in the morning... This way I should go to bed early, get up early and be more productive during the day! This stuff is great! Hopefully it helps with this internet addiction thing...

Oh, and this way I will quickly see if it activates itself when my laptop is closed.

Thanks for all the help on this forum, it's been very helpful!



[ Reply to This | # ]
Automate an Internet content filter
Authored by: mnanda on Aug 21, '12 02:40:05PM

OMG!!! Finding this has CHANGED MY LIFE!!! I've been dying to find a way to set a timer to launch Self Control - should have searched it out ages ago! Thank you thank you thank you!

It works! Clearly, I am ecstatic!!



[ Reply to This | # ]
Automate an Internet content filter
Authored by: js87 on Sep 29, '12 02:13:43AM

This is great.

However, If I run it as described above in applescript editor I get the following error

Expected end of line but found “"”.

at

tell process "SelfControl"


thanks



[ Reply to This | # ]
Automate an Internet content filter
Authored by: oxm450 on Nov 12, '12 09:19:54PM

Thanks for this great tip.

I want to set a delay so that when I run the script, it doesn't run selfcontrol until 30 minutes later.

Would that be possible with the current script?



[ Reply to This | # ]
Automate an Internet content filter
Authored by: stuolddog on May 01, '13 01:46:45PM

stuck,

hi not sure if people are still looking at this page but i am getting stuck with the following error message if anyone can help;

error "Usable Keychain Scripting got an error: Can’t get generic item 1 of current keychain whose name contains \"SelfControl\". Invalid index." number -1719

much appriciated

stu



[ Reply to This | # ]
Automate an Internet content filter
Authored by: bjterry on May 16, '13 09:39:03AM

stuolddog,

I got the same error and the fix is that the SelfControl password you created in Keychain Access must be created in your "current keychain". If you go to Keychain Access, the current keychain is the one in bold. You can drag the password from whichever keychain it was created in into the bolded current keychain.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: bjterry on May 16, '13 09:48:44AM

I wanted to edit the script so that I could run it and then have it turn on only after a delay (so that I could turn on SelfControl automatically 30 minutes from now). The modified script is here: https://gist.github.com/BJTerry/5593180



[ Reply to This | # ]
Automate an Internet content filter
Authored by: Januz on Aug 04, '13 12:39:05AM

Thank you very much for this approach!! I modified the script to

1) be run by launchd twice a day on weekdays (at the beginning of the work day, 8.01, and after lunch break, 14.01) and automatically set hard coded end times (12.30 for the first working period; 18.00 for the second)

2) be run from command line or as an Alfred workflow, with different results depending on the use of an argument
a - no argument activates SelfControl with the end times specified in 1)
b - a number (e.g., 10) as argument activates SelfControl for the number of minutes (e.g., 10 minutes)
c - a float (e.g., 15.30) is interpreted as end time, and SelfControl is activated for the number of minutes until this time (minutes from actual time until 15.30)

Note that

1) and 2a) work only in the specified working times, so from 8.00-12.30 in the morning and from 14.00-18.00 in the afternoon, i.e. if you run the script within these time ranges, SelfControl will be activated until the end of the respective period (12.30 in the morning; 18.00 in the afternoon). If you run the script outside of these time ranges, the script will do nothing.

You can find the launchd file I use and the script here:

https://gist.github.com/Januzz/6149533#file-launchd-file-for-selfcontrol
https://gist.github.com/Januzz/6149535#file-applescript-for-selfcontrol




[ Reply to This | # ]
Automate an Internet content filter
Authored by: kudzulibrary on Jun 25, '14 09:02:23AM

The update of iCal makes it a little trickier to run a script, but you can do it through Automator. Here's how:

1. Open Automator
2. Choose Calendar Alarm
3. Choose Run Apple Script
4. Paste the original script above into the box. In the second line, after "set default time to" I put 480, because I want Self Control to run for 8 hours.
5. Click File and Save - I called mine "Run Self Control." Once you do this, it will immediately pop up on your calendar. Double click and hit edit.
6. Indicate the time frame in which you want it to come on. You need to do this because your script will not run if your computer is logged off. I set mine from 7 - 11 am, because that's when I'm most likely to log on in the morning. So if I log on at 8:30 am, it will run until 4:30 pm.
7. Under repeat, click Custom. I set mine to Weekly, MTWRF.
8. Make sure the Calendar is set to Automator, and that under “Alert” it says “Open File” and then the name of your script (i.e., “Run Self Control.”)

Another consideration: Sites like Facebook run on cookies and don't want you to block them. To make Self Control work, you should set your browser to run on private browsing mode, or to keep cookies only until you close your browser.



[ Reply to This | # ]
Automate an Internet content filter
Authored by: rulefollowing on Jul 13, '14 08:13:05AM

Thanks for posting this valuable information.

It almost works for me in Calendar.

The script will work if it is run in Automator.

It will work in Calendar if the number of minutes is set to 15.

It won't work if the number of minutes is set to 480. It won't work in Calendar if the number of minutes is set to 30.

Instead I get this error message:

"The action “Run AppleScript” encountered an error."

Thoughts?





[ Reply to This | # ]