check my automator apple script. The gole is to recive notifications on low batterie of…
- User
check my automator apple script. The gole is to recive notifications on low batterie of connected bluetooth devices like my mouse and keyboard
property warnThreshold : 60
on run try display notification "Bluetooth Batterie Check startet..." with title "Test"
set btData to do shell script "ioreg -r -c AppleDeviceManagementHIDEventService -a" set xml to do shell script "echo " & quoted form of btData & " | plutil -convert xml1 -o - -- - | xmllint --format -" set deviceList to paragraphs of (do shell script "echo " & quoted form of xml & " | xmllint --xpath '//dict[key=\"BatteryPercent\"]' - | grep -E '<string>|<integer>' | sed -E 's/<[^>]+>//g'") display notification "Gefundene Einträge: " & (count of deviceList) with title "Debug" set namesList to {} repeat with i from 1 to (count of deviceList) by 2 set end of namesList to item i of deviceList end repeat display notification "Geräte: " & (namesList as string) with title "Debug" set lowBatteryDevices to {} repeat with i from 1 to (count of deviceList) by 2 set devName to item i of deviceList set devLevelRaw to item (i + 1) of deviceList if containsOnlyDigits(devLevelRaw) then set devLevel to devLevelRaw as integer if devLevel < warnThreshold then set end of lowBatteryDevices to "„" & devName & "“: " & devLevel & "%" end if end if end repeat if (count of lowBatteryDevices) > 0 then set messageText to "Folgende Bluetooth-Geräte haben niedrigen Akku:" & return & (lowBatteryDevices as string) display dialog messageText buttons {"OK"} default button "OK" display notification messageText with title "🔋 Bluetooth Batterie Warnung" subtitle "Bitte Geräte aufladen" sound name "Funk" else display notification "Alle Bluetooth-Geräte haben genügend Akku." with title "Bluetooth Batterie Check" end if on error errMsg display dialog "Fehler beim Prüfen der Bluetooth-Batterien: " & errMsg buttons {"OK"} default button "OK" with icon caution end tryend run
on containsOnlyDigits(theText) set allowedChars to "0123456789" repeat with i from 1 to length of theText if allowedChars does not contain character i of theText then return false end repeat return true end containsOnlyDigits
Assistant
Looking at your AppleScript for monitoring Bluetooth device battery levels, I can see a few issues and areas for improvement. Here's my analysis:
Issues Found:
-
XML Parsing Logic: The current approach assumes device names and battery percentages alternate in the output, but this might not always be reliable depending on the XML structure.
-
Device Name Extraction: The script is extracting all strings and integers from XML nodes containing "BatteryPercent", but it's not specifically targeting device names.
-
String Concatenation: When displaying multiple devices in notifications, you're converting lists directly to strings which may not format nicely.
Here's an improved version:
property warnThreshold : 60 on run try -- Get Bluetooth device data set btData to do shell script "ioreg -r -c AppleDeviceManagementHIDEventService -a" -- Parse the data to find devices with battery info set deviceInfo to {} -- Extract device names and battery levels using a more reliable method set shellCmd to "ioreg -r -c AppleDeviceManagementHIDEventService | grep -E '\"Product\"|\"BatteryPercent\"' | sed 's/.*= //g' | sed 's/\"//g'" set rawData to paragraphs of (do shell script shellCmd) -- Process the data in pairs (Product name followed by BatteryPercent) set currentDevice to "" repeat with i from 1 to count of rawData set currentLine to item i of rawData -- Check if this line is a number (battery percentage) if my isNumber(currentLine) and currentDevice is not "" then set batteryLevel to currentLine as integer set end of deviceInfo to {deviceName:currentDevice, batteryLevel:batteryLevel} set currentDevice to "" else if currentLine is not "" then -- This should be a device name set currentDevice to currentLine end if end repeat -- Check for low battery devices set lowBatteryDevices to {} repeat with device in deviceInfo if batteryLevel of device < warnThreshold then set end of lowBatteryDevices to (deviceName of device) & ": " & (batteryLevel of device) & "%" end if end repeat -- Display notifications if (count of lowBatteryDevices) > 0 then set messageText to "Folgende Bluetooth-Geräte haben niedrigen Akku:" & return & return repeat with device in lowBatteryDevices set messageText to messageText & "• " & device & return end repeat display notification messageText with title "🔋 Bluetooth Batterie Warnung" subtitle "Bitte Geräte aufladen" sound name "Funk" -- Optional: Also show a dialog for immediate attention display dialog messageText buttons {"OK"} default button "OK" with title "Bluetooth Batterie Warnung" else display notification "Alle Bluetooth-Geräte haben genügend Akku (>" & warnThreshold & "%)" with title "✓ Bluetooth Batterie Check" end if on error errMsg display dialog "Fehler beim Prüfen der Bluetooth-Batterien:" & return & return & errMsg buttons {"OK"} default button "OK" with icon caution end try end run on isNumber(someText) try set someText to someText as number return true on error return false end try end isNumberAlternative Approach Using System Information:
Here's a simpler alternative that uses system_profiler:
property warnThreshold : 60 on run try -- Get Bluetooth info using system_profiler set btInfo to do shell script "system_profiler SPBluetoothDataType -xml" -- Parse for connected devices with battery info set deviceList to {} -- This command extracts device names and battery levels more reliably set shellCmd to "system_profiler SPBluetoothDataType | grep -A 20 'Connected: Yes' | grep -E 'Battery Level:|^[[:space:]]+[^:]+:$' | sed 's/://g' | sed 's/^[[:space:]]*//' | grep -v 'Services' | grep -v 'Major Type'" set rawOutput to do shell script shellCmd set outputLines to paragraphs of rawOutput set currentDevice to "" repeat with line in outputLines if line contains "Battery Level" then set batteryText to text ((offset of "Battery Level" in line) + 13) thru -1 of line set batteryLevel to (text 1 thru -2 of batteryText) as integer if currentDevice is not "" and batteryLevel < warnThreshold then set end of deviceList to currentDevice & ": " & batteryLevel & "%" end if else if line is not "" and line does not contain "Battery Level" then set currentDevice to line end if end repeat -- Show results if (count of deviceList) > 0 then set notificationText to "Niedrige Batterie:" & return repeat with device in deviceList set notificationText to notificationText & "• " & device & return end repeat display notification notificationText with title "🔋 Bluetooth Batterie Warnung" sound name "Funk" else display notification "Alle Geräte OK (>" & warnThreshold & "%)" with title "✓ Bluetooth Check" end if on error errMsg display notification "Fehler: " & errMsg with title "Bluetooth Check Fehler" end try end runTips for Automation:
-
Save the script as an application in Script Editor
-
Use
launchdto run it periodically:- Create a plist file in
~/Library/LaunchAgents/ - Set it to run every hour or at your preferred interval
- Create a plist file in
-
Consider adding logging to track battery levels over time
Would you like me to help you set up the automated scheduling as well?
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.