Thursday, October 10, 2019

Blink the screen when your Mac needs to charge


  1. Enable invert colors shortcut:
    System Preferences... -> Keyboard -> Accessibility -> Invert colors [x]
  2. Allow macscript to modify Accessibility
    System Preferences... -> Security & Privacy -> Accessibility -> Privacy -> + -> osascript
  3. Test that it's working from your shell:
    osascript -e "tell application \"System Events\"" -e "key code 28 using {command down, option down, control down}" -e "end tell"
  4. Create the script
    sudo vim /etc/batteryScript.applescript

    set Cap to (do shell script "ioreg -w0 -l | grep ExternalChargeCapable")
    tell Cap to set {wallPower} to {last word of paragraph 1}
    if wallPower = "Yes" then
     return 0
    else
     set Cap to (do shell script "ioreg -wO -l | grep AppleRaw")
     tell Cap to set {Max, Available} to {last word of paragraph 2, last word of paragraph 1}
     set Pct to round (100 * Available / Max)
     if Pct <= 5 then
      tell application "System Events" key code 28 using {command down, option down, control down}
      end tell
      delay 1
      tell application "System Events"
       key code 28 using {command down, option down, control down}
      end tell
      if Pct <= 3 then
       delay 1
       tell application "System Events"
        key code 28 using {command down, option down, control down}
       end tell
       delay 1
       tell application "System Events"
        key code 28 using {command down, option down, control down}
       end tell
       delay 1
       tell application "System Events"
        key code 28 using {command down, option down, control down}
       end tell
       delay 1
       tell application "System Events"
        key code 28 using {command down, option down, control down}
       end tell
       if Pct <= 1 then
        display notification "Computer is about to shutdown!" with title "---=== WARNING ===---"
        beep 5
        do shell script "say -v \"Alex\" \"PLUG ME IN NOW\""
       end if
      end if
     end if
    end if
  5. Configure script access:
    sudo chmod +rx /etc/batteryScript.applescript
  6. Invoking script
    vim ~/Library/LaunchAgents/batteryAlert.plist

    <?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>KeepAlive</key>
      <false/>
      <key>Label</key>
      <string>batteryAlert</string>
      <key>LowPriorityIO</key>
      <true/>
      <key>ProgramArguments</key>
      <array>
       <string>/usr/bin/osascript</string>
       <string>/etc/batteryScript.applescript</string>
      </array>
      <key>RunAtLoad</key>
      <true/>
      <key>ServiceDescription</key>
      <string>Battery Alert</string>
      <key>StartInterval</key>
      <integer>30</integer>
     </dict>
    </plist>

  7. Reboot the OS or manually load the launchd module.
    launchctl load ~/Library/LaunchAgents/batteryAlert.plist

No comments:

Post a Comment