Jump to content

If in If statement


Recommended Posts

All,

I've a question about my script. Mainly about the following part of my code:

AUTOITSETOPTION("SendKeyDelay",100) ;SET KEY DELAY
IF WINEXISTS("Microsoft Dynamics GP")=1 THEN ;CHECK IF GP IS RUNNING
WINACTIVATE("Microsoft Dynamics GP") ;ACTIVATE GP WINDOW
SEND("!ans{ENTER}") ;OPEN INVENTORY SERIES POSTING WINDOW
SLEEP(5000) ;WAIT 5 SECONDS TO LET THE INVENTORY SERIES POSTING WINDOW OPEN
CONTROLFOCUS("Microsoft Dynamics GP","","Button8") ;SELECT "MARK WMS BCHS" Button
CONTROLCLICK("Microsoft Dynamics GP","","Button8") ;CLICK "MARK WMS BCHS" Button
;CLICK CANCEL ON REPORT DESTINATION WINDOWS
WINWAITACTIVE("Report Destination","",10)
CONTROLFOCUS("Report Destination","","Button7")
CONTROLCLICK("Report Destination","","Button7")
WINWAITACTIVE("Report Destination","",10)
CONTROLFOCUS("Report Destination","","Button7")
CONTROLCLICK("Report Destination","","Button7")
WINWAITACTIVE("Report Destination","",10)
CONTROLFOCUS("Report Destination","","Button7")
CONTROLCLICK("Report Destination","","Button7")
WINWAITACTIVE("Report Destination","",10)
CONTROLFOCUS("Report Destination","","Button7")
CONTROLCLICK("Report Destination","","Button7")
WINWAITACTIVE("Report Destination","",10)
CONTROLFOCUS("Report Destination","","Button7")
CONTROLCLICK("Report Destination","","Button7")
WINWAITACTIVE("Report Destination","",10)
CONTROLFOCUS("Report Destination","","Button7")
CONTROLCLICK("Report Destination","","Button7")
WINWAITACTIVE("Report Destination","",10)
CONTROLFOCUS("Report Destination","","Button7")
CONTROLCLICK("Report Destination","","Button7")
WINWAITACTIVE("Report Destination","",10)
CONTROLFOCUS("Report Destination","","Button7")
CONTROLCLICK("Report Destination","","Button7")
ELSE ;IF GP IS NOT RUNNING BELOW CODE WILL BE EXECUTED

Instead of having 23 lines of code to only click a cancel button I should also be able to great a loop. When it runs the script and after it clicked "Button8" it should start the loop and keep scanning if the window with the name "Report Destination" pops up and if so it should press "Button7". I'm not sure if we can build in some sort of timeout that if the screen didn't popup for 10 seconds it will go out of the loop and wait until the script gets executed again(we don't want a loop that will eat up all CPU). I've no idea if this is possible with AutoIT and it would be great if someone can help me to get some better understanding of what the capabilities are of creating a loop. I was thinking of a another If / Else statement but this is probably not the "best" solution..

Thanks,

Edited by Khoneini
Link to comment
Share on other sites

  • Moderators

Khoneini,

How many times are you expecting a "Report Destination" dialog to appear? At the moment you are waiting 8 times for 10 seconds which seems a bit OTT unless you will have that many. ;)

I would suggest something like this might suit you better:

AutoItSetOption("SendKeyDelay", 100) ;SET KEY DELAY
If WinExists("Microsoft Dynamics GP") = 1 Then ;CHECK IF GP IS RUNNING
    WinActivate("Microsoft Dynamics GP") ;ACTIVATE GP WINDOW
    Send("!ans{ENTER}") ;OPEN INVENTORY SERIES POSTING WINDOW
    Sleep(5000) ;WAIT 5 SECONDS TO LET THE INVENTORY SERIES POSTING WINDOW OPEN
    ControlFocus("Microsoft Dynamics GP", "", "Button8") ;SELECT "MARK WMS BCHS" Button
    ControlClick("Microsoft Dynamics GP", "", "Button8") ;CLICK "MARK WMS BCHS" Button
    ;CLICK CANCEL ON REPORT DESTINATION WINDOWS
    ; Get a timestamp
    $iBegin = TimerInit()
    ; Start a loop
    Do
        ; Look for the dialog
        If WinExists("Report Destination") Then 
            ; If found then close it
            WinActivate("Report Destination")
            ControlFocus("Report Destination", "", "Button7")
            ControlClick("Report Destination", "", "Button7")
        EndIf
        ; Give the CPU a breather
        Sleep(10)
    Until TimerDiff($iBegin) > 10 * 1000 ; Keep looping until 10 secs have elapsed

Else ;IF GP IS NOT RUNNING BELOW CODE WILL BE EXECUTED

What do you think? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Looks good to me at least something to test. Everytime I expect the "Report Destination" dialog appear for 4 times. Maybe it would be just better to instead of letting the loop run for 10 seconds, just let it go through the loop 5 times(1 more time in case it did miss 1 dialog). OR letting it loop until it doesn't find another "Report Destination" dialog anymore. These dialogs are popping up right after you close one down. What I'm trying to achieve is a more efficient code where I can relay on instead of what we're having now. Your input is really appreciated thanks!

Edited by Khoneini
Link to comment
Share on other sites

  • Moderators

Khoneini,

If you are expecting 4 dialogs then this would be a better solution:

;CLICK CANCEL ON REPORT DESTINATION WINDOWS
; We expect 4 dialogs
$iDialog_Count = 0
; Start an infinite loop
While 1
    ; Look for the dialog
    If WinExists("Report Destination") Then 
        ; If found then close it
        WinActivate("Report Destination")
        ControlFocus("Report Destination", "", "Button7")
        ControlClick("Report Destination", "", "Button7")
        ; We closed a dialog, so increase the count
        $iDialog_Count +=1
        ; Have we closed all 4?
        If $iDialog_Count = 4 Then
            ; Yes, so exit
            ExitLoop
        EndIf
    EndIf
    ; Give the CPU a breather
    Sleep(10)
WEnd

If you would still like an overall time limit so that you do not hang around if there are fewer than 4 for some reason, then you could use the Do...Until loop from the last suggestion in place of the While...WEnd loop here. Do ask if you need a hand amalgamating the 2 scripts. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I can't thank you enough for this! I'm still identifying the process but it looks like it is not always 4 times. Sometimes it is also 6 times or even 8 times that the dialog pops up. It is always right after "Button8" is being pressed so we can start the loop after that. Which resolution do you advice in this situation? Just let the loop run for 10-15 seconds or let the loop run for X times?

Edited by Khoneini
Link to comment
Share on other sites

  • Moderators

Khoneini,

As you have no idea how many dialogs you will have to cancel, using a count is not a good idea. :(

So I suggest you wait for a certain time after cancelling each dialog to see if another pops up - if nothing appears after that time then you can continue:

;CLICK CANCEL ON REPORT DESTINATION WINDOWS
; Get a timestamp
$iBegin = TimerInit()
; Start a loop
Do
    ; Look for the dialog
    If WinExists("Report Destination") Then 
        ; If found then close it
        WinActivate("Report Destination")
        ControlFocus("Report Destination", "", "Button7")
        ControlClick("Report Destination", "", "Button7")
        ; Reset the timestamp
        $iBegin = TimerInit()
    EndIf
    ; Give the CPU a breather
    Sleep(10)
    ; Exit the loop if we go 15 secs without cancelling a dialog
Until TimerDiff($iBegin) > 15 * 1000

How does that sound? :)

M23

P.S. When you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - I know what I wrote and it just pads the thread unneccessarily. ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sorry about Quoting your posts. I think this will be the solution. Will try this and let you know if it's working or not! There is one more thing that frustrates me and that is the first part of the script. It doesn't need to fre-open the INVENTORY SERIES POSTING WINDOW time after time so I think it would be the best to put this in a Function:

SEND("!ans{ENTER}") ;OPEN INVENTORY SERIES POSTING WINDOW

And only execute IF the Inventory Series Posting window is not open. The CONTROL attributes of this window are:

>>>> Window <<<<
Title: Microsoft Dynamics GP
Class: TNTMAIN
Position: -4, -4
Size: 1032, 748
Style: 0x17CF0000
ExStyle: 0x00000100
Handle: 0x0004036C
>>>> Control <<<<
Class: TNT
Instance: 1
ClassnameNN: TNT1
Name:
Advanced (Class): [CLASS:TNT; INSTANCE:1]
ID: 10002
Text: .Inventory Series Posting.
Position: 187, 110
Size: 620, 362
ControlClick Coords: 319, 14
Style: 0x56CF0000
ExStyle: 0x00000140
Handle: 0x01780512
>>>> Mouse <<<<
Position: 506, 162
Cursor ID: 0
Color: 0x848284
>>>> StatusBar <<<<
>>>> ToolsBar <<<<
>>>> Visible Text <<<<
Dexterity Runtime
.Inventory Series Posting.
&Post
Redispla&y
Mark &All
U&nmark All
Mark WMS Bchs
Toolbar

The reason why I would like to put this in a function is because it might need to be executed somewhere else in the script too(in the ELSE statement at the bottom of my script) The problem why I'm unable to do this myself is because I do understand the WINACTIVATE, WINEXISTS and WINWAITACTIVE command. Although I'm not sure what to use if we need to filter on: Text: .Inventory Series Posting. and/or ID: 10002

Hmm, might be that I already found my solution, just curious if it would be advisible to have a IF statement within a IF statement. So between IF WINEXISTS("Microsoft Dynamics GP")=1 THEN ;CHECK IF GP IS RUNNING AND ELSE ;IF GP IS NOT RUNNING BELOW CODE WILL BE EXECUTED I'll add the following:

AUTOITSETOPTION("SendKeyDelay",100) ;SET KEY DELAY
IF WINEXISTS("Microsoft Dynamics GP")=1 THEN ;CHECK IF GP IS RUNNING
WINACTIVATE("Microsoft Dynamics GP") ;ACTIVATE GP WINDOW
IF WINEXISTS("Microsoft Dynamics GP", ".Inventory Series Posting.")=0 THEN
SEND("!ans{ENTER}") ;OPEN INVENTORY SERIES POSTING WINDOW
WINWAITACTIVE("Microsoft Dynamics GP", ".Inventory Series Posting.",30)
ELSE
CONTROLFOCUS("Microsoft Dynamics GP","","Button8") ;SELECT "MARK WMS BCHS" Button
CONTROLCLICK("Microsoft Dynamics GP","","Button8") ;CLICK "MARK WMS BCHS" Button
;CLICK CANCEL ON REPORT DESTINATION WINDOWS
$iBegin = TimerInit() ;GET A TIMESTAMP FOR THE LOOP
DO ;START THE LOOP
IF WINEXISTS("Report Destination") THEN ;LOOK FOR THE DIALOG
WINACTIVATE("Report Destination") ;ACTIVATE REPORT DESTINATION WINDOW
CONTROLFOCUS("Report Destination", "", "Button7") ;SELECT "CANCEL" Button
CONTROLCLICK("Report Destination", "", "Button7") ;CLICK "CANCEL" Button
$iBegin = TimerInit() ;RESET THE TIMESTAMP SO IT WILL START THE LOOP AGAIN
ENDIF
SLEEP(10) ;GIVE THE CPU A BREATHER
UNTIL TimerDiff($iBegin) > 5 * 1000 ; EXIT THE LOOP IF WE GO 5 SECS WITHOUT CANCELLING A DIALOG
ELSE ;IF GP IS NOT RUNNING BELOW CODE WILL BE EXECUTED

Can someone please let me know if above code is correct?

Thanks a million for all the help provided.

Edited by Khoneini
Link to comment
Share on other sites

  • Moderators

Khoneini,

I think your logic is a bit flawed in that snippet - does this amended version make sense to you? :huh:

AutoItSetOption("SendKeyDelay", 100) ;SET KEY DELAY
If WinExists("Microsoft Dynamics GP") = 1 Then ;CHECK IF GP IS RUNNING
    ; Check if INVENTORY SERIES POSTING WINDOW is open
    If Not WinExists("Microsoft Dynamics GP", ".Inventory Series Posting.") Then
        Send("!ans{ENTER}") ;OPEN INVENTORY SERIES POSTING WINDOW
        ; And wait for it to appear
        WinWait("Microsoft Dynamics GP", ".Inventory Series Posting.", 30)
    EndIf
    ; Now activate main window
    WinActivate("Microsoft Dynamics GP") ;ACTIVATE GP WINDOW
    ControlFocus("Microsoft Dynamics GP", "", "Button8") ;SELECT "MARK WMS BCHS" Button
    ControlClick("Microsoft Dynamics GP", "", "Button8") ;CLICK "MARK WMS BCHS" Button
    ;CLICK CANCEL ON REPORT DESTINATION WINDOWS
    $iBegin = TimerInit() ;GET A TIMESTAMP FOR THE LOOP
    Do ;START THE LOOP
        If WinExists("Report Destination") Then ;LOOK FOR THE DIALOG
            WinActivate("Report Destination") ;ACTIVATE REPORT DESTINATION WINDOW
            ControlFocus("Report Destination", "", "Button7") ;SELECT "CANCEL" Button
            ControlClick("Report Destination", "", "Button7") ;CLICK "CANCEL" Button
            $iBegin = TimerInit() ;RESET THE TIMESTAMP SO IT WILL START THE LOOP AGAIN
        EndIf
        Sleep(10) ;GIVE THE CPU A BREATHER
    Until TimerDiff($iBegin) > 5 * 1000 ; EXIT THE LOOP IF WE GO 5 SECS WITHOUT CANCELLING A DIALOG
Else ;IF GP IS NOT RUNNING BELOW CODE WILL BE EXECUTED

EndIf

As to making a function of the opening Inventory Series Posting code - how many times will you call it in the script? It is only 2 lines - and you need at least one to call a function! Quite frankly I would leave it as it is until you get the whole script running as you wish - then you can try and optimise further. Premature optimisation is never a good idea - get it working first!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

This is only tangentially related to this thread, no offense intended.

The original code had a lot of ControFocus commands before the ControlClicks. Is there a time when this is helpful? Doesn't ControlClick send a click to a control regardless of focus? Do I need to start using ControlFocus commands prior to my ControlClicks? Thanks for any reply.

Meds.  They're not just for breakfast anymore. :'(

Link to comment
Share on other sites

You only need to use ControlFocus if the application requires the control or window to have focus before it accepts the command you send it. If you (your application) don't need it, then you don't need to use it.

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