Msb12i

^_^ (A REAL NOOB)ELSEIF doens't seem to work

9 posts in this topic

Hi Guys,

I wanna learn how to use AutoIT because I have to do allot of repetitive keystroke's and allready impressed my boss make a small script.

However, I'm having some trouble with the Elseif function.

I created some (still) basic scripts to auto accept file downloads. They work sepperate but when I try to put them together to form one script only the first work.

I don't need a working version. Just some guideline's :)

   While 1
      If WinWaitActive("Message from webpage") Then
        Send("{ENTER}")
        WinWaitActive("File Download")
        ControlClick("File Download", "", 4427)
        WinWaitActive("File Download")
        Send("{ENTER}")
        WinWaitActive("Save As")
        ControlClick("Save As", "", 1)
      Elseif WinWaitActive("File Download") Then
        WinWaitActive("File Download")
        ControlClick("File Download", "", 4427)
        WinWaitActive("File Download")
        Send("{ENTER}")
        WinWaitActive("Save As")
        ControlClick("Save As", "", 1)
         EndIf
   WEnd 

Share this post


Link to post
Share on other sites



The script will wait forever for the first window ("Message from webpage") to become active. That's what WinWaitActive does. And when it does, it executes that first piece of code. So it never gets to that elseif. There are several ways to fix/change this. Two ways that come to mind are:

  • You could introduce a timeout in the WinWaitActive call (see helpfile for WinWaitActive). It would be something like WinWaitActive("Message from webpage", "", 5) to wait only for 5 seconds instead of forever. If that window doesn't appear for 5 seconds, it will go through to the elseif (because the WinWaitActive will return 0 in that case).
  • You could put both pieces of code in separate functions, then write a loop waiting for any of these windows to appear (repeatedly calling WinActive or WinExists) and calling one or the other function based on which window appears first.

I would go with the second, but it requires slightly more code change than the first.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Welcome to the forum :D

 

Here my 2 cents:

While 1
    $hWin = WinWaitActive("[REGEXPTITLE:Message from webpage|File Download]")
    If WinExists("Message from webpage") Then Send("{ENTER}")
    WinWaitActive("File Download")
    ControlClick("File Download", "", 4427)
    WinWaitActive("File Download")
    Send("{ENTER}")
    WinWaitActive("Save As")
    ControlClick("Save As", "", 1)
WEnd

 

Edited by Exit

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Welcome to the forum :D

 

Here my 2 cents:

While 1
    $hWin = WinWaitActive("[REGEXPTITLE:Message from webpage|File Download]")
    If WinExists("Message from webpage") Then Send("{ENTER}")
    WinWaitActive("File Download")
    ControlClick("File Download", "", 4427)
    WinWaitActive("File Download")
    Send("{ENTER}")
    WinWaitActive("Save As")
    ControlClick("Save As", "", 1)
WEnd

 

Thanks Exit,

 

But I'm gonna work with SadBunny's tips.

 

I tested you're code and it works like a sharm... However It doesn't help my learning process ^_^ so I'm gonna continue editing my code.

I still have allot to learn... $hWin = GuyWaitUnderstandChinese to me ;) 

 

PS: This was my first try:

Is it even possible to have 2 While???

 

 

while 1
   WinWaitActive("Message from webpage")
   Send("{ENTER}")
   WinWaitActive("File Download")
   ControlClick("File Download", "", 4427)
   WinWaitActive("File Download")
   Send("{ENTER}")
   WinWaitActive("Save As")
   ControlClick("Save As", "", 1)
WEnd
while 2
   WinWaitActive("File Download")
   ControlClick("File Download", "", 4427)
   WinWaitActive("File Download")
   Send("{ENTER}")
   WinWaitActive("Save As")
   ControlClick("Save As", "", 1)
WEnd

 

Edited by Msb12i

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Msb12I,

Is it even possible to have 2 While???

 

Yes, but you need to provide a way to exit the do...while loop, perhaps, ExitLoop.  See the Help file.

kylomas

Edit: After a further look at your code...read SadBunny's suggestions more closely.  What you are trying to implement is not what he is suggesting.

 

Edited by kylomas
clarification

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

I tested you're code and it works like a sharm... However It doesn't help my learning process ^_^ so I'm gonna continue editing my code.

I still have allot to learn... $hWin = GuyWaitUnderstandChinese to me ;) 

You should not insist on your code, but learn from other coders.

Your code in the if and the elseif branch are nearly equal.

The first has only this two additional lines

If WinWaitActive("Message from webpage") Then

        Send("{ENTER}")

the remaining code is identical.

So you can merge the two branches to one branch with a prefix code to check for "Message from Webpage"

 

Here a commented flow.

While 1   ;  1 means true, so always execute the following code

    $hWin = WinWaitActive("[REGEXPTITLE:Message from webpage|File Download]")
    ; I still have allot to learn... $hWin = GuyWaitUnderstandChinese to me  
    ; So try to understand it. Variable $hWin receives the Windows handle for further processing.

    ; See the helpfile for Winwaitactive "Title special Definition" --> REGEXPTITLE.
    ; ("[REGEXPTITLE:Message from webpage|File Download]")
    ; This means to wait for Title "Message from Webpage" OR "File Download"

    If WinExists("Message from webpage") Then Send("{ENTER}")
    ; So, in the first case send ENTER

    ; from here on it is the commom code
  
    WinWaitActive("File Download")
    ControlClick("File Download", "", 4427)
    WinWaitActive("File Download")
    Send("{ENTER}")
    WinWaitActive("Save As")
    ControlClick("Save As", "", 1)
WEnd

I hope you learned a bit of the GuyWaitUnderstandChinese  :D


My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites

You should not insist on your code, but learn from other coders.

Your code in the if and the elseif branch are nearly equal.

The first has only this two additional lines

If WinWaitActive("Message from webpage") Then

        Send("{ENTER}")

the remaining code is identical.

So you can merge the two branches to one branch with a prefix code to check for "Message from Webpage"

 

Here a commented flow.

While 1   ;  1 means true, so always execute the following code

    $hWin = WinWaitActive("[REGEXPTITLE:Message from webpage|File Download]")
    ; I still have allot to learn... $hWin = GuyWaitUnderstandChinese to me  
    ; So try to understand it. Variable $hWin receives the Windows handle for further processing.

    ; See the helpfile for Winwaitactive "Title special Definition" --> REGEXPTITLE.
    ; ("[REGEXPTITLE:Message from webpage|File Download]")
    ; This means to wait for Title "Message from Webpage" OR "File Download"

    If WinExists("Message from webpage") Then Send("{ENTER}")
    ; So, in the first case send ENTER

    ; from here on it is the commom code
  
    WinWaitActive("File Download")
    ControlClick("File Download", "", 4427)
    WinWaitActive("File Download")
    Send("{ENTER}")
    WinWaitActive("Save As")
    ControlClick("Save As", "", 1)
WEnd

I hope you learned a bit of the GuyWaitUnderstandChinese  :D

Thanks for you're input,

I know, but I'm a proud guy... I want to have it right with some help but in the end doing it myself. 

Seems like I need to learn more because I didn't know about the REGEXPTITLE.

The most frustrating part about the script writing is, that I know what I want it to do but can't give the right orders...

 

Anyway,

 

Thanks for all the help guy. I'm gonna experiment some more and perhaps in a few months I will the one replying to a noob.

 

Glad you liked my GuyWaitUnderstandChinese ;) 

Share this post


Link to post
Share on other sites

Msb12I,

 

Yes, but you need to provide a way to exit the do...while loop, perhaps, ExitLoop.  See the Help file.

kylomas

Edit: After a further look at your code...read SadBunny's suggestions more closely.  What you are trying to implement is not what he is suggesting.

 

I know,

 

read my comment more closely, that was my very first try before SadBunny's tips ;) 

Share this post


Link to post
Share on other sites

You can have 2 While loop ofc, and they can semi run next to each other, but you should put them into a Function each of them, to be able to call them 1 by 1 with an execution delay.

 

Take a look on Help file for: Func()

and: Adlibregister

and: Adlibunregister

 

If: If the event is succesful, then something will hapen what you code down.

If you put elseif after that, it mean, If the first event does not apire / succes, then jump on second "Elseif" line and check if that event is succes or not. If succes, then your code will run what you wrote below.

If that is not succes then jumping down again. If your first event have like in your example: WinWaitActive, then your script will stuck in this posicion until the event will be succesful, cause the order is "Wait".

 

I recomanded to put a Sleep() function in your code, otherwise your CPU will work harder then it is nessesery. You also can use: Controlsend function, instead of clickig on the button. For this you can use the AutoIT tool called: Au3Info.exe or Au3Info_x64.exe to find the data for your control (Button).

 

With send function, i had some issue. It is work only on active window (So you have to use a function SendKeepActive to work it safe). If something pop up, example your antivir download / report, or anything else, it grab the "active" status from your download window, and your script will send the "Enter" to that window, instead of your Download window. With Controlsend, no mater what will hapen, it will always send to the right window / Object.

 

P.S.:

Sry if i am writeing kinda noobish way, but as you can read my description below in my post, i am also learning how to use AutoIT by myself as useing Help file much as i can, and i am still just at the begining. :)

I am also started from 0, like you do now.

 

Tricky


Sry for my bad English, and double sry, but I am learning AutoIT language by myself. :)

[u]Tricky[/u]

You can't teach a man anything, you can only help him, find it within himself. (Galileo Galilei)

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