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
;;http://www.machinereadable.net/2011/05/disable-dropbox-while-running-on.html


;;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 
 DropBoxExe=%A_AppData%\Dropbox\bin\Dropbox.exe 
 Run %DropBoxExe%
 ;MsgBox "AC Power On"
}

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

/*
Notes:
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".
*/
B_Threshold=95
/*
Made Possible by:
Wrapper to catch Power Management events by TheGood
 (http://www.autohotkey.com/forum/topic40008.html)
Shimanov's ReadInteger function:
 (http://www.autohotkey.com/forum/topic6144.html)
AC/Battery status by antonyb
 (http://www.autohotkey.com/forum/topic7633.html)
*/


ReadInteger( p_address, p_offset, p_size, p_hex=true )
{
  value = 0
  old_FormatInteger := a_FormatInteger
  if ( p_hex )
    SetFormat, integer, hex
  else
    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
Return

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)
 acLineStatus:=ReadInteger(&powerstatus,0,1,false)
 batteryFlag:=ReadInteger(&powerstatus,1,1,false)
 batteryLifePercent:=ReadInteger(&powerstatus,2,1,false)
 batteryLifeTime:=ReadInteger(&powerstatus,4,4,false)
 batteryFullLifeTime:=ReadInteger(&powerstatus,8,4,false)

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

Download the AHK Script

2 comments:

  1. Boaz Arad rocks! All the time. :)

    Thanks for sharing such an amazing thing

    ReplyDelete