For limiting a MacBook battery's charge to prolong its life I find bclm a lot more straightforward than AlDente.
sudo bclm write 81 keeps the hardware battery percentage at 80% on both my MacBooks (I haven't found a reason to use persist, yet).
I would often set the bclm to 100 for a period of time to get a full charge before taking one to a coffee shop. With this script I don't have to remember to reset it.
I currently combine it with telling DropBox to quit, which seems to smooth out issues with the machine orienting itself to the coffee shop's WiFi open waking up, as well as connecting the VPN ahead of time.
#!/bin/bash
wait_for() {
func="$1"
timeout_seconds=10
elapsed_seconds=0
while [ $elapsed_seconds -lt "$timeout_seconds" ]; do
if "$func"; then
return 0
fi
sleep 1
elapsed_seconds=$((elapsed_seconds + 1))
done
return 1
}
vpn_is_connected() {
mullvad status | grep "Connected" >/dev/null
}
tell() {
local some_app="$1"
local what="$2"
is_running() {
pgrep -x "$1" >/dev/null
}
case "$what" in
quit)
is_running "$some_app" || return 0
;;
activate)
! is_running "$some_app" || return 0
;;
esac
echo "telling $some_app to $what.."
osascript -e "tell application \"$some_app\" to $what" || return 1
}
tell "Dropbox" "activate" || exit 1
echo "starting full charge cycle.."
bclm-cc || exit 1
tell "Docker Desktop" "quit" || exit 1
tell "Dropbox" "quit" || exit 1
if ! vpn_is_connected; then
# tell_app "Mullvad VPN" "activate"
echo "telling mullvad to connect.."
mullvad connect || exit 1
fi
echo "checking vpn connection.."
if wait_for "vpn_is_connected"; then
echo "vpn connected; undock!, undock!"
else
echo "vpn failed to connect" >&2
exit 1
fiThanks to a tip from this very informative gist, I now have passwordless sudo set up.
sudo visudo
..opens /etc/sudoers in vim. I added this to the bottom:
[my username] ALL=(ALL) NOPASSWD: /usr/local/bin/bclm
Now sudo bclm no longer requires a password.