2

I am attempting to use launchd to have SelfControl be active from 8am to 8pm. My first attempt was to just use launchd and have a little bash script which starts self control (for 12 hours) at 8am, but this isn't really flexible enough because if my laptop is off at 8am then obviously nothing happens.

Any advice on a better way to approach this problem or modify my current approach?

Here is the .plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
     <string>my.selfControlScheduler</string>
  <key>Program</key>
     <string>/Users/ira/Documents/coding/self_control/sc_sched.sh</string>
  <key>StartCalendarInterval</key>
     <dict>
     <key>Hour</key>
     <integer>8</integer>
     <key>Minute</key>
     <integer>0</integer>
     </dict>
</dict>
</plist>

And here is the bash script

sudo /Applications/SelfControl.app/Contents/MacOS/org.eyebeam.SelfControl $(id -u $(whoami)) --install

The length of time to block the blacklist in SelfControl for can be set by doing

defaults write org.eyebeam.SelfControl "BlockDuration"=xxx

I think that's all the relevant information

  • Please add the content of the launchd plist and the bash script – klanomath Feb 22 '16 at 15:52
1

You should be able to control this with two different launchd jobs.

  • one job to start daily at 8 am
  • another job to unload the first at quitting time (8 pm)

If the application doesn't expect to be loaded/unloaded (like mongodb does if you install it from homebrew), then change the second script to call your "kill" script.

I'm of the mind to just call /usr/bin/killall SelfControl if that works rather than mucking with wrapping a mode convoluted AppleScript example with /usr/bin/osascript like in the example here:

/usr/bin/osascript -e 'tell application "Mathematica" to quit'

This is far more robust than making a helper script that needs to stay running since launchd keeps track of the time that passes when powered off and/or sleeping and should fire things later (say 23:30) if you wake the Mac from sleep starting at 6 pm.

I use LaunchControl or Lingon app to manage my plist files - but you can hand roll them too like your question.

The second is shorter, so I've named it org.you.control.unload and it simply calls launchctl unload org.you.control.load at 20:00 and set the RunAtLoad to /false so it only triggers per the clock.

<dict>
    <key>Label</key>
    <string>org.you.control.unload</string>
    <key>ProgramArguments</key>
    <array>
        <string>launchctl</string>
        <string>unload</string>
        <string>org.you.control.load</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>20</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
</dict>

Just decide on the names you want: org.you.control.load or ** my.selfControlScheduler** are both fine, but pick an idiom and stick with it throughout.

Your startup plist looks fine but there could be permissions or other issues that a tool to debug things without needing a handy reference like http://launchd.info

Rather than script the opening of the app, just call /usr/bin/open -a SelfControl would be my suggestion if yours isn't working presently.

<string>org.you.control.load</string>
<key>ProgramArguments</key>
<array>
    <string>/usr/bin/open</string>
    <string>-a</string>
    <string>SelfControl</string>
</array>

You must log in to answer this question.

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