Skip to content

Instantly share code, notes, and snippets.

@OKsign
Created May 14, 2021 11:05
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/4983684aab943a0afef61914d8e02424 to your computer and use it in GitHub Desktop.
Save OKsign/4983684aab943a0afef61914d8e02424 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"
-- 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
set kbat to keyboard & " is Not Connected!"
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
set mbat to mouse & " is Not Connected!"
end if
exit repeat
end if
end if
end repeat
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 mbat to mouse & ": " & (item j of aa_list)
exit repeat
end if
end if
end repeat
set text item delimiters to apple_std
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 kbat to keyboard & ": " & (item j of aa_list)
exit repeat
end if
end if
end repeat
set text item delimiters to apple_std
end if -- if s_key is 1 then
tell application "System Events"
display dialog kbat & "
" & mbat with title "Battery Level"
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment