I spend most of my time working at my desk, but occasionally I’ll bring my MacBook out.

It’s mostly due to my habit from the older days before Apple released Apple Silicon/M1/M2 — the batteries don’t last as Xcode/iOS Simulator and AppCode are very power intensive1; I had to kill unnecessary apps to conserve the battery power to work outside for a few hours.

I wrote 2 scripts:

  • mobile: run this to kill apps and make macOS run in Low Power Mode
  • dock: run this to launch a few more important apps and make macOS run in High Power Mode

mobile. The first half just kills apps:

killall Mail
killall Crashlytics
killall Maestral
#killall more here…

The 2nd half of dock is more interesting:

PASS=$(security find-generic-password -l "script admin" -a foo -w)
echo 'Stop Spotlight indexing:'
echo $PASS | sudo -S mdutil -i off /
echo $PASS | sudo -S pmset -b lowpowermode 1

You’ll need to create a password in Keychain Access.app with File > New Password Item and save your macOS account password. Give it a name (I call mine "script admin"), give it an account (I call mine foo), and save it to the login keychain.

Then you can read the password with:

security find-generic-password -l "script admin" -a foo -w

We want the password so we can sudo to make macOS run in Low Power Mode as well as stop Spotlight indexing. You can verify this by clicking on the battery icon on the macOS menu bar after running the script.

dock: This starts apps that are critical. For eg. I start Maestral so the Dropbox file sync works, resumes Spotlight indexing and make macOS run in High Power Mode:

open -a Maestral
open -g -a /Applications/Fantastical.app
#open more important ones here…

PASS=$(security find-generic-password -l "script admin" -a foo -w)
echo 'Start Spotlight indexing:'
echo $PASS | sudo -S mdutil -i on /
echo $PASS | sudo -S pmset -b lowpowermode 2

(the -g switch runs apps without bringing them to the foreground)

  1. who am I kidding, they still don’t last so long as you run Xcode/iOS Simulator/AppCode 😭