Jump to content
Sign in to follow this  
AnotherNamePlease

Problem with recognising a certain window [Solved]

Recommended Posts

AnotherNamePlease

Hi there,

Introduction

I have written a AutoIt v3-Script to start a programm and click on the programms start button, all takes place on one distant server. This is necessary since the programm doesn't support parameter arguments via command line. The AutoIt v3-Script purpose is to start that programm after rebooting which is a regular sceduled reboot every 24h. The AutoIt v3-Script and the programm are on the same server, which I can reach via RDP or by car. The AutoIt v3-Script should work even if there is nobody connected via RDP.

Problem

My script doesn't find the window if the program start takes to long. The output "Move Window ..." stays visible, so I guess the problem is in the 4 lines after that output and before the next output. I'm also not sure if it works when there is nobody connected via RDP, since I read that I can't use all AutoIt-Functions in that case i.e. ControlSend vs Send. But I can't find a full description which functions work and which not. What am I doing wrong? Here is the Script with my comments.

#Region                                                                                         ; **** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_Icon=my_icon.ico
    #AutoIt3Wrapper_Compression=0
    #EndRegion                                                                                      ; **** Directives created by AutoIt3Wrapper_GUI ****
    ; ############################################
    ; ############### Preparations ###############
    ; ############################################
    #include <Misc.au3>
    _Singleton("start_my_programm")                                                                 ; prevents multiple starts of this script
    ;#NoEnv                                                                                         ; recommended for performance and compatibility with future AutoHotkey releases
    ;#Warn                                                                                          ; enable warnings to assist with detecting common errors
    Opt("WinTitleMatchMode", 1)                                                                     ; 1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=force lower case match according to other type of match.
    Global $PIDNotFound = True
     
    ; #########################################################
    ; ############# FUNCTION: _GetWinTitleByPID() #############
    ; #########################################################
    Func _GetWinTitleByPID ($PID)
            Sleep(850)
            $List = WinList ()
            For $A = 1 To $List[0][0]
                    If BitAnd (WinGetState ($List[$A][0]), 2) And $List[$A][1] <> '' Then
                            If WinGetProcess ($List[$A][0]) = $PID Then Return $List[$A][1]
                    Else
                            Sleep(150)
                    EndIf
            Next
            Sleep(850)
    EndFunc
     
     
    ; ############################################
    ; ############# FUNCTION: Main() #############
    ; ############################################
    ToolTip("Start Programm", 0, 0)
    $PID_APBi = Run("C:\Path\to\Programm.exe", "C:\Path\to")
    While $PIDNotFound
            If $PID_APBi = 0 Then
                    Sleep(10)
            Else
                    ToolTip("Move Window ...", 0, 0)
                    $windowTitle =_GetWinTitleByPID($PID_APBi)
                    WinWait($windowTitle, "", 60)                                                   ; pauses script execution until the requested window exists or until 60 seconds are over.
                    $PIDNotFound = False
                    WinMove($windowTitle, "", 0, 0)
                    ToolTip("Press the Startbutton.", 0, 0)
                    Sleep(150)
                    While 0 = ControlCommand($windowTitle, "", "[TEXT:Start]", "IsEnabled", "")     ; loop and search until control is ready
                            Sleep(150)
                    WEnd
                    Sleep(150)
                    ControlClick ($windowTitle, "", "[TEXT:Start]", "left")
                    ToolTip("", 0, 0)
            EndIf
    Wend

Thanks for your help and please be gentle, as I am a AutoIt beginner. :)

Edited by AnotherNamePlease

Share this post


Link to post
Share on other sites
Valiante

Hi AnotherNamePlease,

Quick question.. is the title of the window always different?  I'm just trying to establish why you need to get the window title via _GetWinTitleByPID as opposed to just specifying the title as plain text.

Thanks,

Val.

Share this post


Link to post
Share on other sites
AnotherNamePlease

Hi there,

Quick question.. is the title of the window always different?  I'm just trying to establish why you need to get the window title via _GetWinTitleByPID as opposed to just specifying the title as plain text.

 

In this case the window title changes sometimes, when a new version of the programm is out. As I plan to reuse my script it would be cool, if the script could be able to detect the actual window title. That would make my script running without any human care, even after my university work there is finished.

Share this post


Link to post
Share on other sites
jdelaney

WinWait waits for only the winstate = exits (1).  In your prior function call, you wait for a window of the process to be visible (2).  If it's visible, it exists.

I'd add a timer loop, in your function, to continually check until you find your window:

Func _WaitForWinTitleByPID ($PID,$iMaxWait_MilSec=Default)
    If $iMaxWait_MilSec = Default Then $iMaxWait_MilSec = 20000
    Local $iTimer = TimerInit()
    While TimerDiff($iTimer) < $iMaxWait_MilSec
        $List = WinList ()
        For $A = 1 To $List[0][0]
            If WinGetProcess ($List[$A][0]) = $PID Then
                If BitAnd (WinGetState ($List[$A][0]), 2) And $List[$A][1] <> '' Then Return $List[$A][1]
            EndIf
        Next
    WEnd
    Return False
EndFunc

I changed around somethings...no need to check a windows state if it's not created by your process.  Some processes launch other processes.  Those will not be found by the above function.

After calling the function, validate that a handle is returned, to proceed with the next steps...ishwnd()

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
AnotherNamePlease

Thank you very much for all replies, especially jdelaney your post works wonderfull. Question is solved, I hope this is helpfull to later googlers. :thumbsup:

Edited by AnotherNamePlease

Share this post


Link to post
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
Sign in to follow this  

×