Msb12i Posted July 21, 2015 Posted July 21, 2015 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
SadBunny Posted July 21, 2015 Posted July 21, 2015 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.
Exit Posted July 21, 2015 Posted July 21, 2015 (edited) Welcome to the forum 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 July 21, 2015 by Exit App: Au3toCmd UDF: _SingleScript()
Msb12i Posted July 21, 2015 Author Posted July 21, 2015 (edited) Welcome to the forum 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)WEndwhile 2 WinWaitActive("File Download") ControlClick("File Download", "", 4427) WinWaitActive("File Download") Send("{ENTER}") WinWaitActive("Save As") ControlClick("Save As", "", 1)WEnd Edited July 21, 2015 by Msb12i
kylomas Posted July 21, 2015 Posted July 21, 2015 (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.kylomasEdit: 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 July 21, 2015 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
Exit Posted July 21, 2015 Posted July 21, 2015 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 linesIf 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) WEndI hope you learned a bit of the GuyWaitUnderstandChinese App: Au3toCmd UDF: _SingleScript()
Msb12i Posted July 22, 2015 Author Posted July 22, 2015 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 linesIf 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) WEndI hope you learned a bit of the GuyWaitUnderstandChinese 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
Msb12i Posted July 22, 2015 Author Posted July 22, 2015 Msb12I, Yes, but you need to provide a way to exit the do...while loop, perhaps, ExitLoop. See the Help file.kylomasEdit: 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
TrickyDeath Posted July 26, 2015 Posted July 26, 2015 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: Adlibregisterand: 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now