Keep Mac Awake (AppleScript + Alfred + Amphetamine)

Keep Mac Awake (AppleScript + Alfred + Amphetamine)

Context:

  • MacBook (rightly) goes to sleep often
  • This means some of my remote sessions get terminated, causing errors
  • There are times when I'd like the screen to turn off and the MacBook to lock, but not go to sleep
  • For this I use a free app/ utility called Amphetamine

Problem:

  • App requires me to navigate menus at the top of the screen to set my timer and keep the Mac awake.
  • I do this frequently enough that the annoyance adds up - especially as this use case coincides with me having to leave quickly and focus on something other than being on the Mac.

Solution:

  • Avoid the mouse gymnastics by using the Keyboard to command the app!

User Story/ User Experience:

  • I want to be able to trigger a new keep-awake session of X hours, by typing 'aa X' into Alfred (launcher)

Upskilling approach/ Implementation steps:

(rapid deployment for a small annoyance = ❤️)

  1. Found out that the app uses ApplesScript and discovered the right syntax for it
  2. Looked up a cheat sheet to understand the lexicon of AppleScript and what I can do with it -> settled for the information on Wikipedia (very well written actually)
  3. Alfred Workflow basics to trigger on a keyboard with an argument -> run AppleScript

Outcome:

  1. At least 3 minutes saved per day
  2. Better understanding on AppleScript, which will most definitely unlock future optimisations
  3. An easy automation - here's how I did it, in case you want to replicate this (you would need Alfred PowerPack and Amphetamine):

References:

  1. Amphetamine Application
  2. Alfred App
  3. AppleScript Cheat Sheet/ Documentation
  4. Amphetamine AppleScript Examples

Read more