1

So I have been using an app called Self Control to block certain sites globally on my machine so I'm not distracted when I work. The method this program uses is edit your /etc/hosts file and redirect your blacklist of sites (facebook.com, youtube.com, etc) to 0.0.0.0. This is cool, but I was thinking that I could make a Keyboard Maestro macro for this instead.

I'm new to Keyboard Maestro and it's a bit intimidating to get into. Does anyone have any recommendations for creating a macro that will open, append, save, and then close your system's hosts file?

2

Have Keyboard Maestro run an AppleScript, which runs a shell script, which changes your hosts file:

do shell script "cp ~/hosts.tmp /etc/hosts" with administrator privileges

Simple! :)

The "with administrator privileges" part will cause it to prompt you for the sudo password.

0

[This is not an answer, and I wanted to post this as comment, but my low “reputation” score doesn’t allow it:]

Keyboard Maestro has an excellent forum now. A quick search in the Keyboard Maestro forum showed me that you didn’t post your question there, so I think it’s worth mentioning it.

To add some content:

The macro won’t be a problem, but the sudo password:

  • It is tempting, but don’t just store your sudo password in a Keyboard Maestro variable or in a named clipboard.

  • Keyboard Maestro provides some special treatment for “password variables”. From the KM documentation:

    Variables with names that start or end with “Password” or “PW” are considered passwords – their values will not be stored (except in memory) and they cannot be read directly by shell scripts or AppleScripts, though their consequences can easily be extracted, eg with the Set Clipboard to Text action. The Prompt For User Input dialog will display such variables in a password field.

So this may be a possibility to access the hosts file in an automated way: Get the sudo password with the “Prompt for User Input” action and store it for example in a variable PW. But I can’t tell how secure this really is.

  • Another possibility would be to manipulate the sudoers file with visudo, so that you can write to the hosts file without entering the sudo password. But I wouldn’t do that in case of the hosts file!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .