Jump to content

Can someone check my logic? Goofing around with IF and ELSEIF statements.


Recommended Posts

I'm writing up something that I'm probably going to use as a function to check for a couple windows opening. There will be 1 or 2 windows opening during this process. The first window appears sometimes, the second always appears weather or not the first appears.

WinWait("Create Folder","",200) ;Waiting for either of the windows to pop up, can this be an OR statement to reflect either window? I put in the timeout value in case "Move Folder" appears instead of "Create Folder"
If WinExists("Create Folder") Then ;This is the first window, it sometimes appears, but always leads to the 2nd window
   WinWaitActive("Create Folder")
   ControlClick("Create Folder","","[CLASS Button; INSTANCE:1]")
   WinWait("Move Folder") ;This part I'm not too sure of, its the same code for the 2nd window, but I need to cover this because it always appears
   WinWaitActive("Move Folder","",)
   ControlClick("Move Folder","","[CLASS Button; INSTANCE:2]")   
ElseIf WinExists("Move Folder") Then ;This is the 2nd window, it always appears
   WinWaitActive("Move Folder","",)
   ControlClick("Move Folder","","[CLASS Button; INSTANCE:2]")
EndIf

Any advice is appreciated!

Link to comment
Share on other sites

$hWndCF = WinWait("Create Folder", "", 200)
If $hWndCF Then
    $hWndCF = WinWaitActive($hWndCF)
    ControlClick($hWndCF, "", "[CLASS Button; INSTANCE:1]")
    $hWndMF = WinWait("Move Folder")
    $hWndMF = WinWaitActive($hWndMF)
    ControlClick($hWndMF, "", "[CLASS Button; INSTANCE:2]")
ElseIf WinExists("Move Folder") Then
    $hWndMF = WinWaitActive("Move Folder")
    ControlClick($hWndMF, "", "[CLASS Button; INSTANCE:2]")
EndIf
$hWndCF = WinWait("Create Folder", "", 200)
If $hWndCF Then
    WinActivate($hWndCF)
    If WinWaitActive($hWndMF, "", 2) Then 
        ControlClick($hWndCF, "", "[CLASS Button; INSTANCE:1]")
        $hWndMF = WinWait("Move Folder", "", 5)
        WinActivate($hWndMF)
        If WinWaitActive($hWndMF, "", 2) Then ControlClick($hWndMF, "", "[CLASS Button; INSTANCE:2]")
    EndIf
ElseIf WinExists("Move Folder") Then
    $hWndMF = WinActivate("Move Folder", "", 2)
    If WinWaitActive($hWndMF) Then ControlClick($hWndMF, "", "[CLASS Button; INSTANCE:2]")
EndIf

Edited by AZJIO
Link to comment
Share on other sites

Apologies, I'm still really new to this. When you use $hWndCF, thats a variable right? Or is that a direct call to the window?

It looks like you are assigning "Create Folder" to that $hWndCF variable correct? Or does that first line assign everything after the "=" to that variable?

Link to comment
Share on other sites

$hWndCF is a variable. It is a way to shorten the names while still giving it meaning.

h = handle

Wnd = window

CF = create folder

It's for organization and its much easier for other programmers to read what you r doing.

Link to comment
Share on other sites

$hWndCF in this case contains a hex value that is the handle of the last Win Function.

If you assign a variable to a function, the return value of that function will be set to the variable.

Also on all native Win functions you can use Windowtitle, Title special definition or, like in AZJIOs example, a direct Handle.

Edited by qsek
Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite
Link to comment
Share on other sites

So I was able to find a bit of information about those variables here...

http://www.autoitscript.com/autoit3/docs/functions/HWnd.htm

It looks like you did something similar to what is described here, if I understand this correctly it takes the current window that you have focus to and assigns it as a variable. So the first set of code you posted shows me the conversion of what I did to those window handles.

I noticed this though, was this a typo?

If WinWaitActive($hWndMF, "", 2) Then

It looks like the variable was typed incorrectly, If I'm reading this right it should be $hWndCF right?

Edit: Was typing this out when the comments above appeared. So in his example code, the $hWnd variable grabs just the window used in that command? Or does it grab the entire command and run that every time the variable is invoked?

Edited by MeepZero
Link to comment
Share on other sites

the $hWnd variable grabs just the window used in that command.

To be more precise: The handle of the window (internal Address/Link) is stored in the variable.

You can use this address with other Win functions, so the script does not have to check the title everytime.

It looks like the variable was typed incorrectly, If I'm reading this right it should be $hWndCF right?

Yes it looks like this is a typo. Edited by qsek
Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite
Link to comment
Share on other sites

Using handles aren't any sort of requirement. It's just an efficiency thing.

Every time you reference a window or a window control by a method other than it's handle then behind-the-scenes lookups take place to resolve the window/control handle before the command can be executed. So, if you're only going to reference the "Move Folder" window once or maybe twice there's no necessity to create you own variable to hold the handle. But if you have a window title string or a control string like "CLASS:Button;INSTANCE:2]" hardcoded in multiple places in your code, then those would be good candidates to have their handle assigned to a variable early in the script, then you could avoid a bunch of unnecessary handle lookups by referencing that variable from then on. It does lend towards easier code maintenance as well, as if a window or control title changed, you'd only have to update your code in a single spot.

typo

Edited by Spiff59
Link to comment
Share on other sites

I was just thinking that actually, by using handles it makes it easier to apply the code to things that may follow the same process, just with different window names.

So you can use handles to actually reference buttons and other elements in a window too? How does the syntax for that work? Is it as simple as doing something like this?

ControlClick($hWndCF, "", "[CLASS Button; INSTANCE:1]")

;to this

$hWndButton1 = ControlClick($hWndCF, "", "[CLASS Button; INSTANCE:1]")

Or would I have to declare that outside of the line because of the other handle listed within?

Link to comment
Share on other sites

ControlGetHandle()

Global $hWndCF = WinGetHandle("Move Folder")
Global $hButton1 = ControlGetHandle($hWndCF, "", "[CLASS:Button;INSTANCE:1]")
ControlClick($hWndCF, "", $hButton1)

Edit: fix my botched paste

Edited by Spiff59
Link to comment
Share on other sites

I put the code into my script and made a couple modifications to it so it applies to what I'm running. Its reaching the point where it pulls the "Create Folder" window up and then doesn't seem to do anything else. The program then terminates itself a few seconds later.

I can't figure it out, it looks like it should be referencing the Yes button but it seems like it just buzzes right past it or something, any ideas?

AutoItSetOption("WinTitleMatchMode" ,2)

Func Propbox() ;Opens the properties box for the item selected and pulls up the location tab
Local $var = 3
Send("{APPSKEY}r")
$hWndProp = WinWait("Properties")
WinWaitActive($hWndProp)
Do
Send("+{TAB}")
$var = $var - 1
Until $var = 1
Send("{UP}{TAB}")
;Goes to the Location tab and selects the Location field, seems to not want to accept letting me reference the field in the ControlClick operation
Send("F:")
EndFunc

Func ApplyAndManagePopups () ;This is where the borrowed code from AZJIO starts

ControlClick("Properties","","[CLASS:Button; INSTANCE:4]")

$hWndCF = WinWait("Create Folder", "", 5000)
If $hWndCF Then
WinActivate($hWndCF)
If WinWaitActive($hWndCF, "", 5000) Then
ControlClick($hWndCF, "", "[CLASS Button; INSTANCE:1]")
$hWndMF = WinWait("Move Folder", "", 5)
WinActivate($hWndMF)
If WinWaitActive($hWndMF, "", 2) Then ControlClick($hWndMF, "", "[CLASS Button; INSTANCE:2]")
EndIf
ElseIf WinExists("Move Folder") Then
$hWndMF = WinActivate("Move Folder", "", 2)
If WinWaitActive($hWndMF) Then ControlClick($hWndMF, "", "[CLASS Button; INSTANCE:2]")
EndIf
EndFunc

ShellExecute(@UserProfileDir)
WinWait("Administrator")
WinWaitActive("Administrator")
;Administrator stuff above is just for testing, actual code lives below this line.
;WinWait(", ")
;WinWaitActive(", ")
Send("My P")

Propbox()

Send("My Pictures")

ApplyAndManagePopups()

MsgBox(0,"","2nd function done")

;beyond this point I'm looping in other folders like Downloads, My Videos, etc
;I'll add them in once I get the operations figured out for the first folder
Edited by MeepZero
Link to comment
Share on other sites

At the top of your script you can put Opt("TrayIconDebug", 1) and you can hover over the AutoIt icon in the system tray and it will tell you what line it's running at the time.

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

My Windows 7 machine isn't currently available, so I can't test your actual code, but...

You can directly manipulate the ListView and Tab controls as well instead of resorting to kludgy Send() calls.

This example works in Win XP:

#include <GuiListView.au3>
#include <GuiTab.au3>
$sFolder = "Favorites"

ShellExecute(@UserProfileDir)
$hWndUser = WinWaitActive(@UserName)
$hListView = ControlGetHandle($hWndUser, "", "[CLASSNN:SysListView321]")
$iLVRow = ControlListView($hWndUser, "", $hListView, "FindItem", $sFolder) ; search listview for a matching row
ControlListView($hWndUser, "", $hListView, "Select", $iLVRow) ; select a row of the listview
Send("{APPSKEY}r")
$hWndProp = WinWaitActive($sFolder & " Properties")
$hTab = ControlGetHandle($hWndProp, "", "[CLASSNN:SysTabControl321]")
_GUICtrlTab_ClickTab($hTab, 3, "left", True)
Sleep(1000)
_GUICtrlTab_ClickTab($hTab, 0, "left", True)
Sleep(1000)
_GUICtrlTab_ClickTab($hTab, 1, "left", True)
Exit
Link to comment
Share on other sites

make sure to include the semi colon in all your controls:

Change

If WinWaitActive($hWndMF, "", 2) Then ControlClick($hWndMF, "", "[CLASS Button; INSTANCE:2]")

To

If WinWaitActive($hWndMF, "", 2) Then ControlClick($hWndMF, "", "[CLASS:Button; INSTANCE:2]")

Also, for code that's easier to debug, you can set variables, and then verify those variables are as you expect...such as any of the is* functions:

$hButton = ControlGetHandle($hWndCF, "", "[CLASS:Button;INSTANCE:1]")
If IsHWnd($hButton) Then
     ConsoleWrite ( "Able to find button(make specific text)" & @crlf )
Else
     ConsoleWrite ( "UNABLE to find button(make specific text)" & @crlf )
EndIf
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.
Link to comment
Share on other sites

Ok, new fresh week, I fired up the script and...it borked again. Looks like the statement for ElseIf on here is not picking up the Move Folder window popping up after the ElseIf statement...

Func ApplyAndManagePopups ()

ControlClick("Properties","","[CLASS:Button; INSTANCE:4]")

$hWndCF = WinWait("Create Folder", "", 200)
If WinActive($hWndCF) Then
ConsoleWrite("I see the Create Folder window...")
;WinActivate("Create Folder") - - OLD CODE, NOT SURE IF NEEDED NOW
If WinWaitActive($hWndCF, "", 5) Then
ControlClick($hWndCF, "","&Yes")
$hWndMF = WinWait("Move Folder", "", 5)
ConsoleWrite("...and now I see the Move Folder window!")
WinActivate($hWndMF)
If WinWaitActive($hWndMF, "", 2) Then ControlClick($hWndMF, "", "&No")
EndIf
ElseIf WinExists("Move Folder") Then
ConsoleWrite("I see the Move Folder window!")
$hWndMF = WinActivate("Move Folder", "", 2)
If WinWaitActive($hWndMF) Then ControlClick($hWndMF, "", "&No")
EndIf
;possible problem where the folder is unavailable can occur here, may need to create if statment to check for this later
EndFunc

I feel like it worked fine last Friday, but I can't figure out what is causing this

Edit! It looks like its hanging on the "$hWndCF = WinWait("Create Folder", "", 200)" line. Shouldnt the 200 at the end there act as a time out value to continue stepping through the process? Editing the value doesn't seem to help there.

Edited by MeepZero
Link to comment
Share on other sites

You might want to read the help again for WinWaitActive. It doesn't take a handle as input.

Edit: You might also think about indenting your code. It's much easier to follow your logic if you can more easily visualize what is going on.

Func ApplyAndManagePopups ()
    ControlClick("Properties","","[CLASS:Button; INSTANCE:4]")
    $hWndCF = WinWait("Create Folder", "", 200)
    If WinActive($hWndCF) Then
        ConsoleWrite("I see the Create Folder window...")
        ;WinActivate("Create Folder") - - OLD CODE, NOT SURE IF NEEDED NOW
        If WinWaitActive($hWndCF, "", 5) Then
            ControlClick($hWndCF, "","&Yes")
            $hWndMF = WinWait("Move Folder", "", 5)
            ConsoleWrite("...and now I see the Move Folder window!")
            WinActivate($hWndMF)
            If WinWaitActive($hWndMF, "", 2) Then ControlClick($hWndMF, "", "&No")
        EndIf
    ElseIf WinExists("Move Folder") Then
        ConsoleWrite("I see the Move Folder window!")
        $hWndMF = WinActivate("Move Folder", "", 2)
        If WinWaitActive($hWndMF) Then ControlClick($hWndMF, "", "&No")
    EndIf
EndFunc
Edited by JohnQSmith

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

Code is indented in my editor, doesn't seem to want to let me indent when I copy paste it over to the forums, apologies for the poor readability.

I'll take another look at that part of the code, what about the "$hWndCF = WinWait("Create Folder", "", 200)" portion of the code near the top? It seems like thats where things are getting hung up. The AutoIT systray icon is showing thats where the script is actually hanging. The behavior occuring is something like the following...

Script is running

Instead of Create Folder appearing, Move Folder appears.

Process hangs on $hWndCF = WinWait("Create Folder", "", 200)

If I understand correctly, the 200 there at the end is supposed to be a timeout value so if the Create Folder never appears, then it just goes on to the next line...right?

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