Skip to content

Instantly share code, notes, and snippets.

@suranap
suranap / LowPowerMode.applescript
Created May 6, 2023 00:26
Low power mode for Macs
-- Check the battery level
set batteryLevel to do shell script "pmset -g batt | grep -Eo \"\\d+%\" | cut -d \"%\" -f 1"
set onBattery to do shell script "pmset -g batt | grep -q \"Battery Power\" && echo 1 || echo 0"
set onLowPowerMode to do shell script "pmset -g | grep lowpowermode | grep -o \"[0-9]*\" "
if onLowPowerMode = "0" and batteryLevel < 20 then
do shell script "pmset -a lowpowermode 1" with administrator privileges
else if onLowPowerMode = "1" and (batteryLevel > 80 or onBattery = "0") then
do shell script "pmset -a lowpowermode 0" with administrator privileges
end if
@suranap
suranap / strategy.py
Last active December 17, 2015 05:18
A Python implementation of tree combinators described in "Building Program Optimizers with Rewriting Strategies" (1998, Visser, Benaissa, Tolmach)
# Tree transformation combinators
# portable strategies
def seqS(*strategies):
def seqaux(tree):
tmp = tree
for s in strategies:
tmp = s(tmp)