Jump to content

Problem with WinWaitNotActive


Recommended Posts

Trying to get a script to terminate when the next window doesnt show up..

EG..

WinActivate("[CLASS:XLMAIN]", "")

;move to master case Number (2nd cell)
Send("{HOME}")
Send("{tab 3}")

;copy's master case Number (2nd cell)
Send("{CTRLDOWN}c{CTRLUP}")


WinActivate("N-FOCUS - Search for Master Case - \\Remote, 128-bit SSL/TLS.", "")
WinWait("N-FOCUS - Search for Master Case - \\Remote, 128-bit SSL/TLS.", "")
Send("{DEL}")
Send("{CTRLDOWN}v{CTRLUP}")
Send("!s")


IF WinWaitNotActive("N-FOCUS - Detail Master Case - \\Remote, 128-bit SSL/TLS.", "", 300)
Call("Terminate")
Endif

While 1
    Sleep(100)
WEnd

Func Terminate()
WinClose("Microsoft Excel")
    Exit 0
EndFunc

So basically it'll copy the number from excel & then input into "N-FOCUS - Search for Master Case" & then normally it go straight to "N-FOCUS - Detail Master Case", however if its not a valid number or a blank number then the "N-FOCUS - Detail Master Case" wont show, therefore it should terminate at that point.

However it'll do all of it fine, then just sits there when the terminate function is meant to be called.

Any Ideas on why this is not working??

Thanks in advance..

Link to comment
Share on other sites

I guess WinWaitNotActive times out and the script just enters the endless loop.

What is the output if you run it like this?

WinActivate("[CLASS:XLMAIN]", "")

;move to master case Number (2nd cell)
Send("{HOME}")
Send("{tab 3}")

;copy's master case Number (2nd cell)
ConsoleWrite("Copying." & @CRLF)
Send("^c") ;Ctrl+c


WinActivate("N-FOCUS - Search for Master Case - \\Remote, 128-bit SSL/TLS.", "")
WinWaitActive("N-FOCUS - Search for Master Case - \\Remote, 128-bit SSL/TLS.", "") ;WinWait didn't realy make any sense at this point.
ConsoleWrite("Pasting." & @CRLF)
;add some sleeps between the sends if things move too fast.
Send("{DEL}")
Send("^v") ;Ctrl+v
Send("!s")

If WinWaitNotActive("N-FOCUS - Detail Master Case - \\Remote, 128-bit SSL/TLS.", "", 300) Then
    ConsoleWrite("Terminating." & @CRLF)
    _Terminate()
Endif

ConsoleWrite("WinWaitNotActive timed out. Going into idle loop." & @CRLF)
While 1
    Sleep(100)
WEnd

Func _Terminate() ;fixed typo.
    WinClose("Microsoft Excel")
    Exit 0
EndFunc

P.s. Have a look at the _Excel...() functions.

Edit: modified the script a little.

Edited by Tvern
Link to comment
Share on other sites

hmmm that was different..

It exited before it could copy the master case #..

>Running:(3.3.6.0):C:\Program Files\AutoIt3\autoit3.exe "C:\testing on\missing verification sheets - Good Cause  - Non Compliance\testing.au3"    
Copying.
Pasting.
Terminating.
+>11:23:41 AutoIT3.exe ended.rc:0
>Exit code: 0    Time: 3.629

However didnt get to the N-FOCUS - Detail Master Case - \\Remote, 128-bit SSL/TLS.

It just exited Excel & didnt paste.. lol

Ok found the problem there for part of it, now it pastes.. but still exits even when it goes to "N-FOCUS - Detail Master Case"

If WinWaitNotActive("N-FOCUS - Detail Master Case - \\Remote, 128-bit SSL/TLS.", "", 300) Then
    ConsoleWrite("Terminating." & @CRLF)
    _Terminate() ;no need to use Call()
Endif

ConsoleWrite("WinWaitNotActive timed out. Going into idle loop." & @CRLF)
While 1
    Sleep(100)
WEnd

_Terminate()
Func _Terminate()
    WinClose("Microsoft Excel")
    Exit 0

>Running:(3.3.6.0):C:\Program Files\AutoIt3\autoit3.exe "C:\testing on\missing verification sheets - Good Cause  - Non Compliance\testing.au3"    
Copying.
Pasting.
Terminating.
+>11:45:34 AutoIT3.exe ended.rc:0
>Exit code: 0    Time: 4.107
Edited by bb01
Link to comment
Share on other sites

I made an error with the name of "Terminate" and realised you used WinWait, where WinWaitActive would probably be more appropriate.

It could be that the changes I made to the Send()'s made the script run a little faster, sending the keys before the window is active.

If you could use AU3info to check the properties of the control you are trying to copy the string to, it might be possible to make the script much more reliable. (especially a usable CLASSNN would be great)

I've made a small adjustment to the script posted above, which you could try.

Edit: Exiting is the expected behavior isn't it? Before you said the script " just sits there"

Edited by Tvern
Link to comment
Share on other sites

nope, it exits no matter what..

EG when the next screen shows (N-FOCUS - Detail Master Case) it exits excel

& when it doesn't show (N-FOCUS - Detail Master Case ) it exits excel..

Trying to get it so when it doesn't show it exits, otherwise it'll carry on threw the script...

also it makes it hard, as all the windows it uses are

Class: Transparent Windows Client

Edited by bb01
Link to comment
Share on other sites

also it makes it hard, as all the windows it uses are

Class: Transparent Windows Client

You might still be able to get the ID's of the controls with a script. I'll try to get back to you on that.

I suspect the "Detail Master Case" is taking too long to become active, or the title doesn't match.

what does this do?

WinActivate("[CLASS:XLMAIN]", "")

;move to master case Number (2nd cell)
Send("{HOME}")
Send("{tab 3}")

;copy's master case Number (2nd cell)
Send("^c") ;Ctrl+c


WinActivate("N-FOCUS - Search for Master Case - \\Remote, 128-bit SSL/TLS.", "") ;You might want to use the window handle from this point on.
WinWaitActive("N-FOCUS - Search for Master Case - \\Remote, 128-bit SSL/TLS.", "") ;This should make sure the application is active before proceeding.

Send("{DEL}")
Send("^v")
Send("!s")

$hWnd = WinActivate("N-FOCUS - Detail Master Case - \\Remote, 128-bit SSL/TLS.")
If Not ($hWnd And WinWaitActive($hWnd, "", 300)) Then
    ConsoleWrite("Window has not activated in time." & @CRLF & "Handle of the window found: " & $hWnd & @CRLF & "Terminating." & @CRLF)
    _Terminate() ;no need to use Call()
Endif

ConsoleWrite("WinWaitNotActive timed out. Going into idle loop." & @CRLF)
While 1
    Sleep(100)
WEnd

Func _Terminate()
    WinClose("Microsoft Excel")
    Exit 0
EndFunc

Edit:

You might be able to get some info about the window controls using this script:

It you're trying to find something like an edit control, be aware that the text in the edit control shows up as it's title. It's something I havn't gottent around to finishing yet.

#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Constants.au3>
#include <Misc.au3>
#include <Array.au3>
#Include <GuiListView.au3>
#Include <GuiTreeView.au3>
#include <Process.au3>


Opt("GUIOnEventMode",1)
Global $tPoint = DllStructCreate($tagPoint)
Global $hGUI, $hTreeView, $hListView, $ahTreeViewItems[1] = [0], $ahWindows[1] = [0], $hSelectedItem

;~ _OpenDSSubCls(True)
_Gui_Build()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
_TreeView_Refresh()
While 1
    If $hSelectedItem Then _ControlInfo()
    Sleep(10)
WEnd

Func _Gui_Build()
    $hGUI = GUICreate("Extended Window Info",800,500)
    GUISetOnEvent(-3,"_Exit")
    GUICtrlCreateButton("Refresh",10,10,50)
    GUICtrlSetOnEvent(-1,"_TreeView_Refresh")
    $hTreeView = _GUICtrlTreeView_Create($hGUI,10,40,200,400)
    $hListView = _GUICtrlListView_Create($hGUI,"",220,40,410,400)
    _GUICtrlListView_InsertColumn($hListView, 1, "Property", 100)
    _GUICtrlListView_InsertColumn($hListView, 1, "Value", 400)
    Local $asProperties = StringSplit("Title,Class,Text,Position,Size,Style,ExStyle,Handle,ProcessID,ProcessName",",")
    For $i = 1 To $asProperties[0]
        _GUICtrlListView_AddSubItem($hListView,_GUICtrlListView_AddItem($hListView, $asProperties[$i]),"",1)
    Next
    GUISetState()
EndFunc

Func _TreeView_Refresh()
    GUIRegisterMsg($WM_NOTIFY, "")

    ;Clear ListView
    _GUICtrlListView_BeginUpdate($hListView)
    For $i = 0 To 9
        _GUICtrlListView_SetItemText($hListView,$i,"",1)
    Next
    _GUICtrlListView_EndUpdate($hListView)

    ;Refill TreeView
    _GUICtrlTreeView_BeginUpdate($hTreeView)
    _GUICtrlTreeView_DeleteAll($hTreeView)
    Global $ahTreeViewItems[1] = [0], $ahWindows[1] = [0]
    Local $ahWnd = WinList()
    For $i = 1 To $ahWnd[0][0]
        If BitAnd(WinGetState($ahWnd[$i][1]), 2) Then _CreateNodeRec($ahWnd[$i][1], $hTreeView)
    Next
    _GUICtrlTreeView_EndUpdate($hTreeView)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
EndFunc

Func _CreateNodeRec($hWnd, $hParentTreeItem)
    $hParentTreeItem = _AddTreeItem($hWnd, $hParentTreeItem)
    Local $ahWnd = _WinAPI_EnumWindows(False, $hWnd)
    For $i = 1 To $ahWnd[0][0]
        If _WinAPI_GetParent($ahWnd[$i][0]) = $hWnd Then
            _CreateNodeRec($ahWnd[$i][0], $hParentTreeItem)
        EndIf
    Next
EndFunc

Func _AddTreeItem($hWnd, $hParentTreeItem)
    _ArrayAddValue($ahWindows, $hWnd)
    If $hParentTreeItem = $hTreeView Then
        _ArrayAddValue($ahTreeViewItems,_GUICtrlTreeView_Add($hTreeView,0,_WinAPI_GetClassName($hWnd)))
    Else
        _ArrayAddValue($ahTreeViewItems,_GUICtrlTreeView_AddChild($hTreeView,$hParentTreeItem,_WinAPI_GetClassName($hWnd)))
    EndIf
    Return $ahTreeViewItems[$ahTreeViewItems[0]]
EndFunc

Func _ArrayAddValue(ByRef $avArray, $vValue)
    $avArray[0] += 1
    ReDim $avArray[$avArray[0]+1]
    $avArray[$avArray[0]] = $vValue
EndFunc

Func _ControlInfo()
    _GUICtrlListView_BeginUpdate($hListView)
;~  _ProcessDettach($DSSubClsPointers[1][0])
    For $i = 1 To $ahTreeViewItems[0]
        If $hSelectedItem <> $ahTreeViewItems[$i] Then ContinueLoop
        If WinExists($ahWindows[$i]) Then
            Local $aPos = WinGetPos($ahWindows[$i])
            _GUICtrlListView_SetItemText($hListView,0,WinGetTitle($ahWindows[$i]),1)
            _GUICtrlListView_SetItemText($hListView,1,_WinAPI_GetClassName($ahWindows[$i]),1)
            _GUICtrlListView_SetItemText($hListView,2,WinGetText($ahWindows[$i]),1)
            _GUICtrlListView_SetItemText($hListView,3,$aPos[0] & "x" & $aPos[1],1)
            _GUICtrlListView_SetItemText($hListView,4,$aPos[2] & "x" & $aPos[3],1)
            _GUICtrlListView_SetItemText($hListView,5,Hex(_WinAPI_GetWindowLong($ahWindows[$i], $GWL_STYLE),8),1)
            _GUICtrlListView_SetItemText($hListView,6,Hex(_WinAPI_GetWindowLong($ahWindows[$i], $GWL_EXSTYLE),8),1)
            _GUICtrlListView_SetItemText($hListView,7,$ahWindows[$i],1)
            Local $PID = WinGetProcess($ahWindows[$i])
            _GUICtrlListView_SetItemText($hListView,8,$PID,1)
            _GUICtrlListView_SetItemText($hListView,9,_ProcessGetName($PID),1)
;~          _ProcessAttach($ahWindows[$i], "_MyCallback")
        Else
            _GUICtrlListView_SetItemText($hListView,0,"Window no longer exists",1)
            For $i0 = 1 To 9
                _GUICtrlListView_SetItemText($hListView,$i0,"",1)
            Next
        EndIf
        ExitLoop
    Next
    _GUICtrlListView_EndUpdate($hListView)
    $hSelectedItem = False
    Return
EndFunc

Func _Pos()
    Local $aPos = MouseGetPos()
    DllStructSetData($tPoint, "x", $aPos[0])
    DllStructSetData($tPoint, "y", $aPos[1])
    Return
EndFunc

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    #forceref $hWnd, $Msg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $tInfo
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hTreeView
            Switch $iCode
                Case $TVN_SELCHANGEDW, $TVN_SELCHANGEDA
                        $hSelectedItem = _GUICtrlTreeView_GetSelection($hTreeView)
            EndSwitch
    EndSwitch
    Return 0
EndFunc

Func _MyCallback($uiMsg, $wParam, $lParam)
    ConsoleWrite($uiMsg & @TAB & $wParam & @TAB & $lParam & @CRLF & @CRLF)
EndFunc   ;==>_MyCallback

Func _Exit()
;~  _CloseDSSubCls()
    Exit
EndFunc
Edited by Tvern
Link to comment
Share on other sites

ok 1st part, it still exits.. even if i knock the wait time up to 3000000, it shuts excel down & doesn't paste at all...

2nd thats a good little script..

Title           N-FOCUS - Search for Master Case - \\Remote, 128-bit SSL/TLS.
Class           Transparent Windows Client
Text
possition       -32000x-32000
size            160x31
style           B60A0000
ExStyle         00000000C
Handle          0x008A0858
ProcessID       380
ProcessName     wifica32.exe
Link to comment
Share on other sites

ok 1st part, it still exits.. even if i knock the wait time up to 3000000, it shuts excel down & doesn't paste at all...

I thought you got pasting working again 3 posts ago?

Does the message in the console give you a handle for the window? If it doesn't, it doesn't find a matching window. (title of the window might have a hidden space in it or something like that)

If it does give a handle, then it sees the window, but the window isn't active (yet). If this is the case I'm not sure what's going on. (I'm sure it can be fixed though)

2nd thats a good little script..

...snip...

Thank you. :unsure: Does that window show any child windows? More specifically are you able to identify the control you are trying to paste to?

(gotta go for a bit, perhaps someone else has some idea's to help)

Link to comment
Share on other sites

Ok, got round the Winwait Error...

However now getting undefinded function...

& syntax errors...

Any ideas???

WinActivate("N-FOCUS - Search for Master Case - \\Remote, 256-bit SSL/TLS.","")
WinWaitActivate2("N-FOCUS - Search for Master Case - \\Remote, 256-bit SSL/TLS.","")


WinActivate("Microsoft Excel","")
WinWaitActivate2("Microsoft Excel","")
Send("{HOME}")
Send("{TAB}{CTRLDOWN}c{CTRLUP}")

WinActivate("N-FOCUS - Search for Master Case - \\Remote, 256-bit SSL/TLS.","")
WinWaitActivate2("N-FOCUS - Search for Master Case - \\Remote, 256-bit SSL/TLS.","")
Send("{DEL}")
Send("{CTRLDOWN}v{CTRLUP}{ENTER}")
Sleep(30)


If WinExists("Error - \\Remote, 256-bit SSL/TLS.", "") Then
_Errorscript()
Else
_scriptloopPT1()
Endif


Func _scriptloopPT1
If WinExists("N-FOCUS - Detail Master Case - \\Remote, 256-bit SSL/TLS", "") Then
_Continuescriptloop()
Else
_Terminate()
Endif




; Contiuescript loop
Func _Continuescriptloop()
WinWaitActivate2("N-FOCUS - Detail Master Case - \\Remote, 256-bit SSL/TLS.","")
Send("{ALTDOWN}gn{ALTUP}")
WinWaitActivate2("N-FOCUS - Search Narrative - \\Remote, 256-bit SSL/TLS.","")

Send("{RIGHT}")
Sleep(10)
Send("{TAB}")
Send("{SPACE}")
Sleep(10)
Send("{TAB 3}")
Sleep(10)
Send("{DOWN 4}")
Send("{SPACE}")
Send("{ALTDOWN}o{ALTUP}")

WinWaitActivate2("N-FOCUS - Detail Narrative - \\Remote, 256-bit SSL/TLS.","")
Send("{DOWN}{DOWN}{DOWN}{DOWN}{SPACE}")
WinWaitActivate2("N-FOCUS - Additional Narrative Detail - \\Remote, 256-bit SSL/TLS.","")

WinActivate("Microsoft Excel","")
WinWaitActivate2("Microsoft Excel","")
Send("{HOME}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{CTRLDOWN}c{CTRLUP}")

WinActivate("N-FOCUS - Additional Narrative Detail - \\Remote, 256-bit SSL/TLS.","")
WinWaitActivate2("N-FOCUS - Additional Narrative Detail - \\Remote, 256-bit SSL/TLS.","")
Send("{CTRLDOWN}v{CTRLUP}{TAB}{ENTER}")
WinWaitActivate2("N-FOCUS - Detail Narrative - \\Remote, 256-bit SSL/TLS.","")
Send("{ALTDOWN}fa{ALTUP}")
WinWaitActivate2("N-FOCUS - Search Narrative - \\Remote, 256-bit SSL/TLS.","")
Send("{CTRLDOWN}c{CTRLUP}{ALTDOWN}c{ALTUP}")
WinWaitActivate2("N-FOCUS - Detail Master Case - \\Remote, 256-bit SSL/TLS.","")
Send("{ALTDOWN}fc{ALTUP}")
WinWaitActivate2("N-FOCUS - Search for Master Case - \\Remote, 256-bit SSL/TLS.","")


WinActivate("Microsoft Excel","")
WinWaitActivate2("Microsoft Excel","")
Send("{HOME}{DOWN}")
Exit
EndFunc

Func WinWaitActivate2($title,$text,$timeout=0)
    WinWait($title,$text,$timeout)
    If Not WinActive($title,$text) Then WinActivate($title,$text)
    WinWaitActive($title,$text,$timeout)
EndFunc



;----------- Error Script
Func _Errorscript()
WinActivate("Microsoft Excel","")
WinWaitActivate2("Microsoft Excel","")
Send("{HOME}")
Send("{TAB 9}")
Send("Not Imput Due To Error")
Send("{HOME}{DOWN}")
EndFunc



;------------ _Terminate()
Func _Terminate()

WinSetState ( "N-FOCUS - Search for Master Case - \\Remote, 256-bit SSL/TLS.", "", @SW_MINIMIZE  )

MsgBox(0,"Script Is Finished", "Finished" & _
@CRLF & @CRLF & " The Script Is Now Finished "  & _
 @CRLF & " It'll Shut Down Excel" & _
@CRLF & "And Then Close This Script && Open" & _
@CRLF & "Up The Script Loader For You", 60)
    WinClose("Microsoft Excel")
Exit
EndFunc

& The Console:-

>Running AU3Check (1.54.19.0)  from:C:\Program Files\AutoIt3
C:\Testing\Program Specialist Appointment\test.au3(31,13) : ERROR: syntax error
_Errorscript
~~~~~~~~~~~~^
C:\Testing\Program Specialist Appointment\test.au3(15,84) : ERROR: WinWaitActivate2(): undefined function.
WinWaitActivate2("N-FOCUS - Search for Master Case - \\Remote, 256-bit SSL/TLS.","")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Testing\Program Specialist Appointment\test.au3 - 2 error(s), 0 warning(s)
!>11:25:51 AU3Check ended.rc:2
>Exit code: 0    Time: 9.701

I've been trying to get this to work correctly for 2 days now... giving me a headache...

Had to re-write most of the scripts, as for some reason they now wouldnt work..

But having errors really is causing my head to explode...

Edited by bb01
Link to comment
Share on other sites

You have this function:

Func _scriptloopPT1
If WinExists("N-FOCUS - Detail Master Case - \\Remote, 256-bit SSL/TLS", "") Then
_Continuescriptloop()
Else
_Terminate()
Endif

This isn't formatted correctly, you need the "()" after the function's name. Also, you never closed the function with an EndFunc, so all your other functions are seen as being inside this one, which is a problem. correct those 2 problems to start off and see how it works.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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