Thursday 28 February 2019

macos - How can I get SelfControl to always run?


I use the SelfControl app.


Is there a way to make it always run?



Answer



Yes. Simply open System Preferences from the /Applications folder, select "Accounts" and, with your user name highlighted in the list to the left, select the "Login items" tab. Either drag and drop SelfControl into the list, or click the "+" button and navigate to its location.


Then, SelfControl will always start up when you log in.




You'll need to download and install Daniel Jalkut's Usable Keychain Scripting to quickly and securely access your admin password in order to activate SelfControl.


Add password to Keychain


Open Keychain Access.app. 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.


Create the AppleScript


Paste the following contents (originally from here)to a new window of AppleScript Editor.app:


on run argv
set defaultTime to 1440

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

We set defaultTime to 1440 (i.e. 24 hours) here so you can automatically kick off SelfControl for the maximum time.


Now, from AppleScript Editor, save this file somewhere by pressing File » Save, e.g. /Users/your-username/SelfControl.scpt, which would be in our home folder. Remember that path.


Create the launchd entry


Then open your favourite text editor or the Property List Editor.app that comes with the Mac OS X Dev tools on Snow Leopard or is integrated into Xcode on Lion.


Either way, paste the following contents, but make sure to change the path to the script accordingly (it't the one we remembered above):






Label
my.selfcontrol.launcher
ProgramArguments

osascript
/Users/your-username/SelfControl.scpt

StartInterval
86400
RunAtLoad

KeepAlive




Save this file with the .plist extension under /Users/your-username/Library/LaunchAgents/my.selfcontrol.launcher.plist. If the Library folder is hidden, you can open Finder, press CmdShiftG and paste ~/Library to open it.


Load the launchd entry


Finally, open a terminal and run:


launchctl load ~/Library/LaunchAgents/my.selfcontrol.launcher.plist

This will tell launchd to open the AppleScript and start SelfControl for twenty four hours every twenty four hours.


Since it's in ~/Library/LaunchAgents it will be loaded automatically when you log in. If you want your computer back, replace load in the above command with unload, then wait for the remaining SelfControl time to expire.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...