-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatterIndicatorUpdated.vbs
More file actions
96 lines (84 loc) · 2.96 KB
/
Copy pathBatterIndicatorUpdated.vbs
File metadata and controls
96 lines (84 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
' Battery Monitor Script - Fully Compatible with VBScript
Dim iFull, bCharging, iRemaining, iPercent
Dim bNotifiedFull, bNotifiedLow
bNotifiedFull = False
bNotifiedLow = False
On Error Resume Next
Set oLocator = CreateObject("WbemScripting.SWbemLocator")
Set oServices = oLocator.ConnectServer(".", "root\wmi")
If Err.Number <> 0 Then
MsgBox "Failed to connect to WMI. Error: " & Err.Description, vbCritical, "WMI Error"
WScript.Quit
End If
On Error GoTo 0
If Not GetFullCapacity(oServices, iFull) Then
MsgBox "Unable to detect full battery capacity. Exiting.", vbExclamation, "Battery Monitor"
WScript.Quit
End If
' Main loop
Do While True
On Error Resume Next
Set oResults = oServices.ExecQuery("SELECT * FROM BatteryStatus")
If Err.Number <> 0 Then
WScript.Sleep 60000 ' Wait 1 minute on error
Err.Clear
Else
bCharging = False
iRemaining = 0
For Each oResult In oResults
iRemaining = oResult.RemainingCapacity
bCharging = oResult.Charging
Exit For ' Assume one battery
Next
If iRemaining > 0 Then
iPercent = Int((iRemaining / iFull) * 100)
If iPercent > 100 Then iPercent = 100
If iPercent < 0 Then iPercent = 0
' Check for full charge (charging and >=95%)
If bCharging And iPercent >= 95 Then
If Not bNotifiedFull Then
Call ShowMessage("Battery Full", "Battery is fully charged (" & iPercent & "%). You can unplug the charger now.", 64)
bNotifiedFull = True
bNotifiedLow = False
End If
Else
bNotifiedFull = False
End If
' Check for low battery (<21%, not charging)
If (Not bCharging) And iPercent <= 20 Then
If Not bNotifiedLow Then
Call ShowMessage("Battery Low", "Battery is low (" & iPercent & "%). Please connect the charger soon.", 48)
bNotifiedLow = True
bNotifiedFull = False
End If
Else
bNotifiedLow = False
End If
End If
End If
' Reset error and sleep
Err.Clear
WScript.Sleep 300000 ' 5 minutes
Loop
' ========================
' Function: Get full charged capacity
Function GetFullCapacity(services, ByRef fullCap)
On Error Resume Next
Set results = services.ExecQuery("SELECT * FROM BatteryFullChargedCapacity")
fullCap = 0
For Each r In results
If Not IsNull(r.FullChargedCapacity) Then
fullCap = r.FullChargedCapacity
If fullCap > 0 Then
GetFullCapacity = True
Exit Function
End If
End If
Next
GetFullCapacity = False
End Function
' ========================
' Sub: Show user message
Sub ShowMessage(title, text, icon)
CreateObject("WScript.Shell").Popup text, 0, title, 0 + icon
End Sub