Skip to content

Instantly share code, notes, and snippets.

@OKsign
Created May 26, 2021 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OKsign/fb1da76a6d62f3900a5ec1f6f0c6152b to your computer and use it in GitHub Desktop.
Save OKsign/fb1da76a6d62f3900a5ec1f6f0c6152b to your computer and use it in GitHub Desktop.
-- user input (name of magic keybord and magic mouse: system preferences > bluetooth)
set keyboard to "Magic Keyboard"
set mouse to "Magic Mouse 2"
set warn_if_lower_than_percent to 74
-- end of user input
set aa to do shell script "system_profiler SPBluetoothDataType | grep '" & keyboard & "\\|Connected'"
set apple_std to text item delimiters
set text item delimiters to {keyboard, ":"}
set aa_list to aa's text items
repeat with i from 1 to count aa_list
if (item i of aa_list) contains "Services" then
set n1 to i
exit repeat
end if
end repeat
repeat with j from 1 to count aa_list
if j > n1 or j = n1 then
if (item j of aa_list) contains "Connected" then
set n2 to j
exit repeat
end if
end if
end repeat
repeat with y from 1 to count aa_list
if y > n2 or y = n2 then
if (item y of aa_list) contains "Yes" or (item y of aa_list) contains "No" then
if (item y of aa_list) contains "Yes" then
set s_key to 1
else
set s_key to 0
end if
exit repeat
end if
end if
end repeat
set text item delimiters to apple_std
set aa to do shell script "system_profiler SPBluetoothDataType | grep '" & mouse & "\\|Connected'"
set apple_std to text item delimiters
set text item delimiters to {mouse, ":"}
set aa_list to aa's text items
--return item 4 of aa_list
repeat with i from 1 to count aa_list
if (item i of aa_list) contains "Services" then
set n1 to i
exit repeat
end if
end repeat
repeat with j from 1 to count aa_list
if j > n1 or j = n1 then
if (item j of aa_list) contains "Connected" then
set n2 to j
exit repeat
end if
end if
end repeat
repeat with y from 1 to count aa_list
if y > n2 or y = n2 then
if (item y of aa_list) contains "Yes" or (item y of aa_list) contains "No" then
if (item y of aa_list) contains "Yes" then
set s_mouse to 1
else
set s_mouse to 0
end if
exit repeat
end if
end if
end repeat
set numlist to {"0", "1", "2,", "3", "4", "5", "6", "7", "8", "9"}
if s_mouse is 1 then
set aa to do shell script "system_profiler SPBluetoothDataType | grep '" & mouse & "\\|Battery'"
set apple_std to text item delimiters
set text item delimiters to {mouse, ":", "Battery Level"}
set aa_list to aa's text items
repeat with i from 1 to count aa_list
if (item i of aa_list) contains "Services" then
set n1 to i
exit repeat
end if
end repeat
repeat with j from 1 to count aa_list
if j > n1 or j = n1 then
if (item j of aa_list) contains "%" then
set Lena to length of (item j of aa_list) as integer
set pertext to ""
repeat with i from 1 to Lena
if character i of (item j of aa_list) is in numlist then
set pertext to pertext & character i of (item j of aa_list)
end if
end repeat
set per to pertext as integer
if warn_if_lower_than_percent > per then
set disdia_m to 1
set mbat to mouse & ": " & (item j of aa_list)
else
set disdia_m to 0
end if
exit repeat
end if
end if
end repeat
set text item delimiters to apple_std
else
set disdia_m to 0
end if --s_mouse is 1
if s_key is 1 then
set aa to do shell script "system_profiler SPBluetoothDataType | grep '" & keyboard & "\\|Battery'"
set apple_std to text item delimiters
set text item delimiters to {keyboard, ":", "Battery Level"}
set aa_list to aa's text items
repeat with i from 1 to count aa_list
if (item i of aa_list) contains "Services" then
set n1 to i
exit repeat
end if
end repeat
repeat with j from 1 to count aa_list
if j > n1 or j = n1 then
if (item j of aa_list) contains "%" then
set Lena to length of (item j of aa_list) as integer
set pertext to ""
repeat with i from 1 to Lena
if character i of (item j of aa_list) is in numlist then
set pertext to pertext & character i of (item j of aa_list)
end if
end repeat
set per to pertext as integer
if warn_if_lower_than_percent > per then
set disdia_k to 1
set kbat to keyboard & ": " & (item j of aa_list)
else
set disdia_k to 0
end if
exit repeat
end if
end if
end repeat
set text item delimiters to apple_std
else
set disdia_k to 0
end if -- if s_key is 1 then
if disdia_m is 1 and disdia_k is 1 then
tell application "System Events"
display dialog kbat & "
" & mbat with title "Low Battery Level (< " & warn_if_lower_than_percent & " %)"
end tell
return
end if
if disdia_m is 1 and disdia_k is 0 then
tell application "System Events"
display dialog mbat with title "Low Battery Level (< " & warn_if_lower_than_percent & " %)"
end tell
end if
if disdia_k is 1 and disdia_m is 0 then
tell application "System Events"
display dialog kbat with title "Low Battery Level (< " & warn_if_lower_than_percent & " %)"
end tell
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment