beqa 0 Posted June 17, 2014 hello all. i want to make a script which will play sounds when charger is connected or disconnected. below is the code whic works only on script start $bsound = _BatteryQueryEx() Switch $bsound[0] Case 0 SoundPlay(@WindowsDir & "mediatada.wav", 1) Case 1 SoundPlay(@WindowsDir & "mediading.wav", 1) EndSwitch after that is udf code. how i can to make it to monitor status and if charger wil disconnect it'll play a sound. thanks Share this post Link to post Share on other sites
JohnOne 1,603 Posted June 17, 2014 Undefined function -> _BatteryQueryEx() AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Share this post Link to post Share on other sites
water 2,365 Posted June 17, 2014 You need to download the >UDF from the forum. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - WikiTask Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - WikiTutorials:ADO - Wiki, WebDriver - Wiki Share this post Link to post Share on other sites
beqa 0 Posted June 17, 2014 i have an udf i didn't paste the whole code. scripts works but only if you run script, but aftr that connecting disconnecting cable doesn't give efect Share this post Link to post Share on other sites
beqa 0 Posted June 17, 2014 this is whole code. $batt = _BatteryQueryEx() Switch $batt[0] Case 0 SoundPlay(@WindowsDir & "mediatada.wav", 1) Case 1 SoundPlay(@WindowsDir & "mediading.wav", 1) EndSwitch #include-once ;====================================================== ; _BatteryQueryEx() ; original author PsaltyDS <a href='#531988' class='bbc_url' title=''>http://www.autoitscript.com/forum/index.php?showtopic=10993&view=findpost&p=531988</a> ; 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 : <a href='http://msdn.microsoft.com/en-us/library/aa373232(VS.85' class='bbc_url' title='External link' rel='nofollow external'>http://msdn.microsoft.com/en-us/library/aa373232(VS.85</a>).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 Share this post Link to post Share on other sites
JohnOne 1,603 Posted June 17, 2014 You'll need to save the initial state into a variable then loop the code until that state changes. Save the new state. That's the loop. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Share this post Link to post Share on other sites
JohnOne 1,603 Posted June 17, 2014 $batt = _BatteryQueryEx() $State = $batt[0] While 3 ;loop here ;$batt = _BatteryQueryEx() ;If $batt[0] changes set $State to $batt[0] and play appropriate sound ;add a sleep WEnd AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Share this post Link to post Share on other sites
beqa 0 Posted June 17, 2014 While 1 Switch $batt[0] Case 0 SoundPlay(@WindowsDir & "mediatada.wav", 1) Case 1 SoundPlay(@WindowsDir & "mediading.wav", 1) EndSwitch sleep(10) WEnd is it right? Share this post Link to post Share on other sites
JohnOne 1,603 Posted June 17, 2014 Almost. You have to test $batt[0] against $State and only play sound if it changes. As it stands that will continue to play the sound. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Share this post Link to post Share on other sites
beqa 0 Posted June 17, 2014 and can't understand what is going $batt = _BatteryQueryEx() $state = $batt[0] While 1 if $batt[0]=0 then $state=0 elif batt[0]=1 then $state=1 endif sleep(10) wend Switch $state Case 0 SoundPlay(@WindowsDir & "mediatada.wav", 1) Case 1 SoundPlay(@WindowsDir & "mediading.wav", 1) EndSwitch Share this post Link to post Share on other sites
Jos 2,165 Posted June 17, 2014 (edited) Just ask yourself a question: Where in the loop am I updating $batt ? Edited June 17, 2014 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Share this post Link to post Share on other sites
JohnOne 1,603 Posted June 17, 2014 Try this. $batt = _BatteryQueryEx() $state = $batt[0] While 1 $batt = _BatteryQueryEx() If $batt[0] <> $state Then $state = $batt[0] Switch $state Case 0 SoundPlay(@WindowsDir & "\media\tada.wav", 1) Case 1 SoundPlay(@WindowsDir & "\media\ding.wav", 1) EndSwitch EndIf Sleep(10) WEnd AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Share this post Link to post Share on other sites
beqa 0 Posted June 17, 2014 thanks, it works. sorry, missed var in the loop. Share this post Link to post Share on other sites
JohnOne 1,603 Posted June 17, 2014 Ace. I'd recommend a longer sleep, maybe 1000. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Share this post Link to post Share on other sites
JohnOne 1,603 Posted June 17, 2014 (edited) In my world, 1 second is enough to test such a thing, I'm a minimalist. Edited June 17, 2014 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Share this post Link to post Share on other sites