Trigger a batch script when power cable is disconnected?

FuturDreamz

Spacemonaut
Member
Messages
740
Long story short, my Asus has a bug where if I unplug it when the screen is closed the touchscreen's driver is disabled. requiring me to manually reenable it. Everything that you would logically think would stop this bug does not work, not even explicitly telling Windows to not disable USB devices EVER. I would like the problem to go away or resolve itself automatically, but there seems to be no solution to this and Microsoft has Clippy doing technical support on their forums. I can create a script or batch file, but I would like it to trigger automatically, without having it running all the time. Seeing that the problem occurs only when the power is unplugged, I would like to try to have the script run shortly after unplugging.

Is there a way to trigger a script when the power cable is unplugged, ideally in a clean and efficient manner? thanks.
 
I got this from AutoIt3 forums
https://www.autoitscript.com/forum/...inor-addition-to-psaltydss-batteryquery-udf/?

You will want to tweak it a bit so it only fires when you haven't run the batch file yet etc..

As it is it will sit in the tray and check if the Laptop is on AC power every 5 seconds. You specify the batch file(e.g. mybatch.cmd) on the command line

AutoIt3 is a free programming language for Windows. I recommend getting the package with Scite4AutoIt3. You can compile the script to exe, add custom icons etc..

Edit: also you will want to get rid of the Msgbox saying the Laptop is plugged in. It's just for test. Otherwise all you will be doing is clicking OK forever. :)

Edit2: to exit the infinite loop right click on the tray icon and quit the program.

Code:
#Region Example
#include <array.au3>
$batch = ""
If $CmdLine[0] = 1 Then
    $batch = $CmdLine[1]
EndIf

While 1
    $batt = _BatteryQueryEx()
    ;_ArrayDisplay($batt)
    If $batt[0] = 0 Then
        ;MsgBox(0, "", "The laptop is NOT plugged in")
        If $batch Then
            ShellExecute($batch)
        EndIf
    ElseIf $batt[0] = 1 Then
        MsgBox(0, "", "The laptop IS plugged in")
    EndIf
    Sleep(5000) ; check every 5 seconds
WEnd
#EndRegion

#include-once
;======================================================
;    _BatteryQueryEx()
;      original author PsaltyDS http://www.autoitscript.com/forum/index.php?showtopic=10993&view=findpost&p=531988
;    Rajesh V R - addition of BatteryFullLifeTime
;    Return information on the Battery
;    Sets @Error on error
;    Returns an array:
;        $array[0]    = ACPower(0=offline, 1=online, 255=unknown)
;        $array[1]    = BatteryFlag(1=High>66%, 2=Low<33%, 4=Critical<5%,
;                      8=Charging 128=No Battery, 255=Unknown
;                      Use BitAnd to test, ie BitAnd($array[1],128)
;        $array[2]    = BatteryLife %(0-100, 255=unknown)
;        $array[3]    = Seconds left of charge, estimate(4294967295=unknown)
;          $array[4]       = BatteryLife @ Full Capacity (Seconds of battery life if fully charged -1 if info not available Not applicable for AC Source)
;     @Link :            http://msdn.microsoft.com/en-us/library/aa373232(VS.85).aspx
;======================================================
Func _BatteryQueryEx()
    Local $SystemPower, $ret, $array[5]

    ; Setup $array and $SystemPower
    $SystemPower = DllStructCreate("ubyte;ubyte;ubyte;ubyte;ulong;ulong")
    If @error Then
        SetError(-1)
        Return $array
    EndIf

    ; make the DllCall
    $ret = DllCall("kernel32.dll", "int", "GetSystemPowerStatus", "ptr", DllStructGetPtr($SystemPower))
    If @error Then;DllCall Failed
        SetError(-2)
        $SystemPower = 0
        Return $array
    EndIf

    If Not $ret[0] Then; GetSystemPowerStatus Failed
        SetError(-3)
        $SystemPower = 0
        Return $array
    EndIf

    ; Fill the array
    $array[0] = DllStructGetData($SystemPower, 1);    AC
    $array[1] = DllStructGetData($SystemPower, 2);    Battery Charge
    $array[2] = DllStructGetData($SystemPower, 3);    Battery Charge %
    $array[3] = DllStructGetData($SystemPower, 5);    Sec Battery Left
    $array[4] = DllStructGetData($SystemPower, 6);    Sec Battery Capacity @ Full Charge

    ; free the struct
    $SystemPower = 0

    Return $array
EndFunc   ;==>_BatteryQueryEx
 
Back
Top