Jump to content

Help with a loop


Recommended Posts

Hello! I'm kinda ashamed my first post is asking for help, but that's it :D

I've been looking in AutoIt for a couple of weeks, and I'm slowly starting to learn some things. However, I'm havng a problem with a loop statement; I know how to use while/wend in a gui, but I don't understand how to use it without a gui.

What I'm trying to achieve is to have a small script running in the background, which should warn me each time I disconnect the AC adapter from my laptop, and each time I reconnect. I found the batteryinfo udf, http://www.autoitscript.com/forum/index.php?showtopic=10993. How can I loop the script untill one of these events occur, display a msgbox, then loop again untill next time the event occurs, and so on?...

If I do this

#include <_BatteryQuery.au3>
$battery    = _BatteryQuery()

While 1
    Select
    case $battery[0] = 0
        MsgBox (48,"Power status", "On battery power")
    case $battery[0] = 1
        MsgBox (48,"Power status", "On AC power")
    EndSelect
WEnd

the message box pops up all the time, as expected :D

Thanks!

Link to comment
Share on other sites

Maybe...

#include <_BatteryQuery.au3>

While 1
    $battery = _BatteryQuery()
    Select
        Case $battery[0] = 0
            ToolTip("On battery power", 10, 10, "Power status", 1)
            ;MsgBox (48,"Power status", "On battery power")
        Case $battery[0] = 1
            ToolTip("On AC power", 10, 10, "Power status", 1)
            ;MsgBox (48,"Power status", "On AC power")
    EndSelect
    Sleep(1000)
WEnd

8)

NEWHeader1.png

Link to comment
Share on other sites

This is better, it will only display when needed

#include <_BatteryQuery.au3>

$Connect = _BatteryQuery()

While 1
    $battery = _BatteryQuery()

    If $battery[0] = 0 And $Connect = 0 Then
        ToolTip("On battery power", 10, 10, "Power status", 1)
        $Connect = 1
    ElseIf $battery[0] = 1 And $Connect = 1 Then
        ToolTip("On AC power", 10, 10, "Power status", 1)
        $Connect = 0
    Else
        ToolTip("")
    EndIf
    Sleep(1000)
WEnd

8)

NEWHeader1.png

Link to comment
Share on other sites

Thanks, Valuater. The first example works great, however the tooltip stays on permanently. As for the second one, it runs without any errors, but it doesn't display anything... I'll look more into it, try to understand what you did there, and try to make it work :D

Thank you!

Link to comment
Share on other sites

This seems to be working...I added the $Connect = "" by trial&error. However, I did not understood the process and I'd be thankfull if someone can explain.

#include <_BatteryQuery.au3>

$Connect = _BatteryQuery()
$Connect = ""

While 1
    $battery = _BatteryQuery()

    If $battery[0] = 0 And $Connect = 0 Then
        ToolTip("On battery power", 10, 10, "Power status", 1)
            Sleep(2000)
        $Connect = 1
    ElseIf $battery[0] = 1 And $Connect = 1 Then
        ToolTip("On AC power", 10, 10, "Power status", 1)
            Sleep(2000)
        $Connect = 0
    Else
        ToolTip("")
    EndIf
    Sleep(1000)
WEnd

Added the sleep(2000), to keep the tooltip displayed long enough to read it.

Edited by gfreeman
Link to comment
Share on other sites

  • 2 weeks later...

Ok, I've got to this point, where I want to have the script checking for battery status and also have a tray menu with some items... I don't know how to approach this... My code:

#include <_BatteryQuery.au3>

Opt("TrayAutoPause",0)
Opt("TrayMenuMode",1)

$Connect = _BatteryQuery()
$Connect = ""
$settings=TrayCreateItem("Settings")
$exit=TrayCreateItem("Exit")
TraySetState()

While 1
    $traymsg=TrayGetMsg()
    $battery = _BatteryQuery()
    
    If $battery[0] = 0 And $Connect = 0 Then
        ToolTip("On battery power", 10, 10, "Power status", 1)
        $Connect = 1
    ElseIf $battery[0] = 1 And $Connect = 1 Then
        ToolTip("On AC power", 10, 10, "Power status", 1)
        $Connect = 0
    Else
        ToolTip("")
    EndIf
    Sleep(1000)
    
    Select
    Case $traymsg = $settings
        MsgBox(-1,"test","ok");some GUI statements will come here instead
        Case $traymsg = $exit
        Exit
    EndSelect
WEnd

Battery status detection works, but the tray menu options get executed only after a 25-30 seconds delay, unrelated to the 1000ms sleep.

PS: does the wrap in Autoit code button works? I'm using Firefox 3.10 and only the normal "wrap in code tags" seem to be working.

Regards!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...