-- Save as Application with "Stay open after run handler" checked
property threshold : 25
property agentLabel : "com.getchannels.dvr"
on idle
set battInfo to do shell script "pmset -g batt"
if battInfo contains "Battery Power" then
set pct to (do shell script "pmset -g batt | grep -Eo '[0-9]+%' | head -1 | tr -d '%'") as integer
if pct ≤ threshold then
do shell script "launchctl bootout gui/$(id -u)/" & agentLabel & " 2>/dev/null; pkill -x channels-dvr; true"
display notification "Battery at " & pct & "%, Channels DVR stopped" with title "Battery Guard"
quit
end if
end if
return 60 -- check every 60s
end idle
-- Save as Application with "Stay open after run handler" checked
property threshold : 50
property agentLabel : "com.getchannels.dvr"
on idle
set pct to (do shell script "pmset -g batt | grep -Eo '[0-9]+%' | head -1 | tr -d '%'") as integer
if pct ≥ threshold then
set isRunning to (do shell script "pgrep -x channels-dvr >/dev/null && echo yes || echo no")
if isRunning is "no" then
do shell script "launchctl bootstrap gui/$(id -u) \"$HOME/Library/LaunchAgents/" & agentLabel & ".plist\" 2>/dev/null; launchctl kickstart gui/$(id -u)/" & agentLabel & " 2>/dev/null; true"
display notification "Battery at " & pct & "%, Channels DVR restarted" with title "Battery Guard"
end if
quit
end if
return 60 -- check every 60s
end idle
Something like this maybe. Unload the plst and then kill the process for good measure if the battery threshold hits 25%, and then if it reaches 50%, reload the plist.