Ever feel the need to close certain apps that drain your battery and then later on forget to relaunch them when connected to power?
I use an app called Rewind that records my screen much like the new Microsoft Copilot Recall. Unfortunately Rewind has been discontinued. But it still works great. The only issue is that it uses a fair amount of power and runs down my battery and so I turn it off when on Battery. I also have an app called Wave Link that connects to my Elgato Wave:3 Studio Mic. It runs audio filtering whether I am actively using the mic or not and again, runs down my battery.
Thus, I wrote an apple script that runs every minute and automatically launches or quits these apps. Hope this helps:
- Open Automator
- Create a New Application
- Search and add Run AppleScript
- Update the script below to fit your needs
-- Get current battery status
set batteryInfo to do shell script "pmset -g batt"
-- List of apps
set appsToClose to {"Rewind", "WaveLink"}
set appsToOpen to {"Rewind", "WaveLink"}
-- Check if on battery power
if batteryInfo contains "Battery Power" then
-- Loop through apps and quit them
repeat with appName in appsToClose
tell application "System Events"
-- Check if the app is running by its name
if exists (processes whose name is appName) then
tell application "System Events" to set appRunning to true
else
tell application "System Events" to set appRunning to false
end if
end tell
if appRunning then
tell application appName to quit
end if
end repeat
else
-- Loop through apps and open them
repeat with appName in appsToOpen
tell application "System Events"
-- Check if the app is running by its name
if exists (processes whose name is appName) then
if (appName is "WaveLink") then
set appName to "Elgato Wave Link"
end if
tell application "System Events" to set appRunning to true
else
tell application "System Events" to set appRunning to false
end if
end tell
if not appRunning then
tell application appName to activate
end if
end repeat
end if
Plist file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.jflow.closeapp</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/open</string>
<string>/Users/jflowerhome/Documents/CloseOnBatteryV2.app</string>
<string>--args</string>
<string>--run-in-background</string>
</array>
<key>StartInterval</key>
<integer>60</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Load the plist file so that macOS runs the app regularly
launchctl load ~/Library/LaunchAgents/com.jflow.closeapp.plist
Tips:
One key was to avoid a relative file path to the apple script.
One strange issue I ran into is that the WaveLink has a different launch name than it’s running name.
When updating the plist file unload and then load it again:
launchctl unload ~/Library/LaunchAgents/com.jflow.closeapp.plist
Troubleshooting
Search for the app in launchctl and note the second column. If it is a “1” then it errored out.
launchctl list | grep com.jflow
- 0 com.jflow.closeapp
Top comments (0)