Disable Dropbox While Running on Battery Power

This Autohotkey script allows you to run custom commands based on your systems AC power status.
By default, the script will disable Dropbox when the battery is discharging, and restart it once AC power is restored.

The script can be easily modified to preform any task on either "AC Power On" or "AC Power Off" events by modifying the corresponding function. Please note that if your Dropbox daemon is not installed in the default directory, you may have to modify the script appropriately.

I was inspired to write this after reinstalling my laptop when I suddenly discovered a dramatic improvement in battery life. With Dropbox as a prime suspect, I was looking for a simple way to turn it on and off depending on the laptops power state. I hope you find this script useful for keeping Dropbox, and any other power hungry apps you may have in check.

If you've found any other original uses for the script, I'd love to hear about them in the comments!

Download the AHK Script

Source Code:

;; Close Programs Based On Laptop Power State
;; By Boaz Arad
;; http://www.boazarad.com

;;Define Custom commands to be run while switching from battery-
;;power to AC power or vice versa

ACPowerOn() ;Run when plugged in
 ;Default Dropbox install dir - modify if necessary 
 Run %DropBoxExe%
 ;MsgBox "AC Power On"

ACPowerOff() ;Run when unplugged
 Process, close, Dropbox.exe
 ;MsgBox "AC Power Off"

Since "On AC Power" and "On AC Power, Charging" are two seperate events
"ACPowerOn" may be run twice When the is plugged in and it's battery is
already charged to over 95% of capacity. This issue can be solved by
setting "B_Threshold" (below) to the maximum charge level of your laptop
battery. This is necessary since if your battery is at full capacity,
plugging in your laptop will not set the battery status to "Charging".
Made Possible by:
Wrapper to catch Power Management events by TheGood
Shimanov's ReadInteger function:
AC/Battery status by antonyb

ReadInteger( p_address, p_offset, p_size, p_hex=true )
  value = 0
  old_FormatInteger := a_FormatInteger
  if ( p_hex )
    SetFormat, integer, hex
    SetFormat, integer, dec
  loop, %p_size%
    value := value+( *( ( p_address+p_offset )+( a_Index-1 ) ) << ( 8* ( a_Index-1 ) ) )
  SetFormat, integer, %old_FormatInteger%
  return, value
OnMessage(536, "OnPBMsg")     ;WM_POWERBROADCAST

OnPBMsg(wParam, lParam, msg, hwnd) {
      If (wParam = 10) {   ;Power state change detected
 VarSetCapacity(powerstatus, 1+1+1+1+4+4)
 success := DllCall("kernel32.dll\GetSystemPowerStatus", "uint", &powerstatus)

 output=AC Status: %acLineStatus%`nBattery Flag: %batteryFlag%`nBattery Life (percent): %batteryLifePercent%`nBattery Life (time): %batteryLifeTime%`nBattery Life (full time): %batteryFullLifeTime%
 ;MsgBox %output% ;Debug - show full powerstatus info

 If (acLineStatus=0)
 Else ;If (batteryFlag>=8 | batteryLifePercent>%B_Threshold%) ;Plugged in and charging (http://msdn.microsoft.com/en-us/library/aa373232(v=vs.85).aspx)
   ;Must return True after message is processed
   Return True

Download the AHK Script