Jump to content

Counting


itauto3
 Share

Recommended Posts

My counter counts from 0 on, if it finds any Notepad window opened, but it counts double (if 2 windows are opened), triple (if 3 windows are opened)..., instead of 1, 2, 3... Any suggestion? 

#include <Array.au3>

$count = 0

While 1
$aWindows = WinList(("[REGEXPTITLE:(?i)(.*Notepad.*)]"))

For $i=1 To $aWindows[0][0]

   $iWndState =  WinGetState($aWindows[$i][1])
   $count += 1

   ToolTip("count = " & $count, 0, 0, "state = " & $iWndState)

Next
   Sleep(1000)
WEnd

 

Link to comment
Share on other sites

Technically you're only ever seeing the last item in the array and you're counting how many times you loop not the number of Windows example if you use the following you'll see the state for each window.

For $i = 1 To $aWindows[0][0]
    $iWndState =  WinGetState($aWindows[$i][1])
    $count += 1
    ToolTip("count = " & $count, 0, 0, "state = " & $iWndState)
    Sleep(1000)
Next

 

Link to comment
Share on other sites

You can check to see if a new notepad window using something like the following:

#include <Array.au3>

Global $aWindows, $iWndState, $iWinCount = 0

While 1
    $aWindows = WinList(("[REGEXPTITLE:(?i)(.*Notepad.*)]"))
    If $iWinCount <> $aWindows[0][0] Then
        $iWinCount = $aWindows[0][0]
        For $i = 1 To $aWindows[0][0]
            $iWndState =  WinGetState($aWindows[$i][1])
            ToolTip("count = " & $i, 0, 0, "state = " & $iWndState)
            Sleep(1000)
        Next
    EndIf
    Sleep(1000)
WEnd

 

Link to comment
Share on other sites

1 hour ago, Subz said:

You can check to see if a new notepad window using something like the following:

#include <Array.au3>

Global $aWindows, $iWndState, $iWinCount = 0

While 1
    $aWindows = WinList(("[REGEXPTITLE:(?i)(.*Notepad.*)]"))
    If $iWinCount <> $aWindows[0][0] Then
        $iWinCount = $aWindows[0][0]
        For $i = 1 To $aWindows[0][0]
            $iWndState =  WinGetState($aWindows[$i][1])
            ToolTip("count = " & $i, 0, 0, "state = " & $iWndState)
            Sleep(1000)
        Next
    EndIf
    Sleep(1000)
WEnd

 

With this code, count is always 1.

Link to comment
Share on other sites

It works fine for me although it would always keep the last Tooltip if all Notepad windows are closed since $i starts at 1 not 0.  You would just need to add an "if" statement as follows.  If no windows are found it will return count = 0 and state = -, if you open multiple notepad instances it will display the last window state in the loop.

Global $aWindows, $iWndState, $iWinCount = 999

While 1
    $aWindows = WinList(("[REGEXPTITLE:(?i)(.*Notepad.*)]"))
    If $iWinCount <> $aWindows[0][0] Then
        $iWinCount = $aWindows[0][0]
        If $iWinCount = 0 Then ContinueLoop ToolTip("count = 0", 0, 0, "state = -")
        For $i = 1 To $aWindows[0][0]
            $iWndState =  WinGetState($aWindows[$i][1])
            ToolTip("count = " & $i, 0, 0, "state = " & $iWndState)
            Sleep(1000)
        Next
    EndIf
    Sleep(1000)
WEnd

 

Edited by Subz
Link to comment
Share on other sites

Whats the point of the For/Next loop as the $iWndState will always be for the last window in the loop?

I think this is what you're after.

Global $aWindows, $iWndState, $iWinCount = 999, $iCount

While 1
    $aWindows = WinList(("[REGEXPTITLE:(?i)(.*Notepad.*)]"))
    If $iWinCount <> $aWindows[0][0] Then
        $iCount = 0
        $iWinCount = $aWindows[0][0]
        Switch $iWinCount
            Case 0
                $iWndState = "-"
            Case Else
                $iWndState =  WinGetState($aWindows[$aWindows[0][0]][1])
        EndSwitch
    EndIf
    Sleep(1000)
    ToolTip("count = " & $iCount, 0, 0, "state = " & $iWndState)
    $iCount += 1
WEnd

 

Link to comment
Share on other sites

$count = 0
$result = 0
.
.
.
.
$result = $result & ", " & $count += 1

Is that maybe what you are after?

That should give you - 0, 1, 2, 3, etc.

If you don't want 0, just trim it or adjust the code with an ElseIf.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

21 hours ago, Subz said:

Whats the point of the For/Next loop as the $iWndState will always be for the last window in the loop?

I think this is what you're after.

Global $aWindows, $iWndState, $iWinCount = 999, $iCount

While 1
    $aWindows = WinList(("[REGEXPTITLE:(?i)(.*Notepad.*)]"))
    If $iWinCount <> $aWindows[0][0] Then
        $iCount = 0
        $iWinCount = $aWindows[0][0]
        Switch $iWinCount
            Case 0
                $iWndState = "-"
            Case Else
                $iWndState =  WinGetState($aWindows[$aWindows[0][0]][1])
        EndSwitch
    EndIf
    Sleep(1000)
    ToolTip("count = " & $iCount, 0, 0, "state = " & $iWndState)
    $iCount += 1
WEnd

 

The state of the window is always 15?

Link to comment
Share on other sites

On 17. 1. 2019 at 12:45 PM, Subz said:

Whats the point of the For/Next loop as the $iWndState will always be for the last window in the loop?

I think this is what you're after.

Global $aWindows, $iWndState, $iWinCount = 999, $iCount

While 1
    $aWindows = WinList(("[REGEXPTITLE:(?i)(.*Notepad.*)]"))
    If $iWinCount <> $aWindows[0][0] Then
        $iCount = 0
        $iWinCount = $aWindows[0][0]
        Switch $iWinCount
            Case 0
                $iWndState = "-"
            Case Else
                $iWndState =  WinGetState($aWindows[$aWindows[0][0]][1])
        EndSwitch
    EndIf
    Sleep(1000)
    ToolTip("count = " & $iCount, 0, 0, "state = " & $iWndState)
    $iCount += 1
WEnd

Do I have to replace existing if statement or add a new one?

 

Link to comment
Share on other sites

Global $aWindows, $iWndState, $iWinCount = 999, $iCount

While 1
    $aWindows = WinList(("[REGEXPTITLE:(?i)(.*Notepad.*)]"))
    If $iWinCount <> $aWindows[0][0] Then $iCount = 0
    $iWinCount = $aWindows[0][0]
    Switch $iWinCount
        Case 0
            $iWndState = "-"
        Case Else
            $iWndState =  WinGetState($aWindows[$aWindows[0][0]][1])
    EndSwitch
    ToolTip("count = " & $iCount, 0, 0, "state = " & $iWndState)
    Sleep(1000)
    $iCount += 1
WEnd

 

Edited by Subz
Link to comment
Share on other sites

  • 2 weeks later...

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...