driverj

Help Using WinActive with a Changing window title

5 posts in this topic

Hello, Long time browser, first time poster

 

I can usually find everything I need by searching, but this time I seem to be falling short

I have been working on some code to use with UltraVNC to log in as a specific user.  I know... discrepancies.... but humor me and help me get this code working.  I would like to be lazy rather than touch many many many machines manually.  I know there will be times when the machine is logged in, but I plan to reboot them via Wake on Lan before issuing these commands!

 

I want AutoIT to launch the VNC window and wait for the window to be active, then if the window is active put in a username and password.

Otherwise I want it to close that window and move on to the next.  The problem is with the window name.  The title of the window is something like this:

CN100-123 ( 12.34.56.78 ) - service mode

in this example CN100-123 will be supplied from a csv file as $CR.  the IP Address in parenthesis will change for every machine, so how do i code for this?

I was hoping to be able to use the $CR since it will already have the computer name, but the window is not found and nothing happens.  I left a sleep command commented out and if the window isn't the failure window, it should automatically put in the username and password, but I do not like this, I would rather wait for the correct window and input the information if possible.

 

I have a spiffed up version of this code that pulls the computer names from a CSV file.  The code seems to work, but I didn't code in for an instance when the connection fails, so I am attempting to do so with a very played down and basic version of that code.

here is the code: 

;Computer name Will be pulled from CSV file later
$CR = 'COMPUTERNAME'
;Command to be executed calling UltraVNC using Comptuer name and supplying VNC password
$command = @ProgramFilesDir & "\UltraVNC\vncviewer.exe -connect " & $CR & " -password VNCPASSWORD"

Local $iPID = run($command)
   ;Sleep(20000)
   $stillLooking = True
      While $stillLooking
         $activeWindowTitle = WinGetTitle(WinActive(""))
         ;I want to find the window that is open for this instance
         If $activeWindowTitle == $CR Then
            ;Send Username
            Send("USERNAME")
            Send("{tab}")
            ; Wait for 1 second
            Sleep(1000)
            ;Send Password
            Send("PASSWORD")
            Send("{enter}")
            ; Wait for 2 seconds
            Sleep(2000)
            ;Close the window after typing username and password
            Send("!+{F4}")
            $stillLooking = False
            ;But what if the computer is offline and not responding?   Close and move on!
         ElseIf $activeWindowTitle == "VncViewer Message Box" Then
            ;Closes open dialogue
            Send("{space}")
            $stillLooking = False
            ;done with If statement
         EndIf
            sleep(5)
      WEnd

 

I appreciate any help you can offer!

Thank you in advance!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Have you tried setting WinTitleMatchMode?

And maybe try using a single "=" here:

If $activeWindowTitle == $CR Then

 

Edited by MuffinMan

Share this post


Link to post
Share on other sites
WinGetTitle("[REGEXPTITLE:(CN100-\d{1,3} \( \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} \))]")

https://www.autoitscript.com/autoit3/docs/intro/windowsadvanced.htm


NEW:     ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-11-12
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites
36 minutes ago, MuffinMan said:

Have you tried setting WinTitleMatchMode?

And maybe try using a single "=" here:

If $activeWindowTitle == $CR Then

 

Yes I have tried using different forms of the WinTitleMatchMode but I was not having any luck.

I also tried variants of the

If $activeWindowTitle == $CR Then

and 

If $activeWindowTitle = $CR Then

Neither had any affect at all.

 

33 minutes ago, mLipok said:
WinGetTitle("[REGEXPTITLE:(CN100-\d{1,3} \( \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} \))]")

https://www.autoitscript.com/autoit3/docs/intro/windowsadvanced.htm

I will have to dig deeper into this when I have time a Simple copy and paste did not work (of course I edited the CN100- bit to match a real machine leaving off the end portion for the script to finish it, but I am out of time today to really troubleshoot it.)

 

Thank you both for your replies I will be working more on this the rest of the week.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

On 11/15/2016 at 2:59 PM, mLipok said:
WinGetTitle("[REGEXPTITLE:(CN100-\d{1,3} \( \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} \))]")

https://www.autoitscript.com/autoit3/docs/intro/windowsadvanced.htm

 

I ended up using a variant of your suggestion 

WinGetTitle("[REGEXPTITLE:( " & $CR & " \( \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} \))]")

 

This is now working perfectly, when it is ran for a one at a time instance, thank you!

 

Now I need to figure out why my failure window is not being closed O.o  But that is a whole new story :D

 

Thank you so much for your help!

Working code

This is the complete code I am working with in case anyone has ideas while I fiddle with it,

#include <array.au3>
#include <file.au3>
#include <constants.au3>

Global $command= 0
Global $command2= 0

Dim $oneDarray
_FileReadToArray("C:\Temp\Lab1.csv", $oneDarray,$FRTA_NOCOUNT)
For $y =0 To UBound($oneDarray) -1
   $columnsCounter = stringsplit($oneDarray[$y],",")
   For $x= 1 To UBound($columnsCounter)-1
      Dim $RowCounter=$columnsCounter[$x]
         ;save code space added variable to house the launch command.
         $command= @ProgramFilesDir & "\UltraVNC\vncviewer.exe -connect " & $RowCounter & " -password VNCPASSWORD"
         ;save code space added variable to hold code for window selection.
         $ultraVNC= "[REGEXPTITLE:( " & $RowCounter &" \( \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} \))]"
            Local $iPID = run($command)
               $stillLooking = True
                  While $stillLooking
                     $activeWindowTitle = WinGetTitle(WinActive($ultraVNC))
                     ;I want to find the window that is open for this instance, sleep for 6 seconds if no response
                     ;it's okay to close the window or enter credentials.
                        Sleep(6000)
                        ;If the window is found, run the log in part.
                     If $activeWindowTitle == WinGetTitle($ultraVNC) Then
                           Send("USERNAME")
                           Send("{tab}")
                           Sleep(1000)
                           Send("PASSWORD")
                           Send("{enter}")
                           Sleep(2000)
                        $stillLooking = False
                        ;But what if the computer is offline and not responding?   Close and move on!
                     Else
                        ;Closes open dialogue
                        Send("{space}")
                        $stillLooking = False
                        ;done with If statement
                     EndIf
                        sleep(1000)
                  WEnd
   Next
Next
;command to kill the open vncviewer windows
$command2= "taskkill /F /IM vncviewer.exe"
Local $iPID = run($command2)
sleep(2000)
;wait 2 seconds, give it time to close the open processes, then display the message
MsgBox(0, "Completed", "Job Finished!")

I'm sure there are some checks I am missing, and I plan to add an error log/tracker at some point as well.

 

Thanks again for all your help!

Edited by driverj
added full script I am working with

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