ame1011 Posted January 12, 2012 Posted January 12, 2012 Hi, I'm just wondering if AutoIt has an event that is run at every line in the script that I can use to run custom code. Thanks! [font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]
BrewManNH Posted January 12, 2012 Posted January 12, 2012 Explain what it is you're trying to accomplish, and maybe someone can actually figure out what you're trying to say. What you asked was if AutoIt can run some other code whenever it executes a line of code in the script if I'm getting what you said right. Frankly, that doesn't seem to make much sense as written. 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 GudeHow 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
Moderators JLogan3o13 Posted January 12, 2012 Moderators Posted January 12, 2012 I know PHP has a register_tick feature, which allows you to call a function for each CPU tick, but I can't figure why you'd want a script to do that. The closest I would think you could come is some AdLibRegister call that always returns True (and thus executes every 250ms). "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
ame1011 Posted January 12, 2012 Author Posted January 12, 2012 We use a script that I created at work that runs overnight on several virtual machines. The issue is that if the internet "flickers" or is temporarily disconnected in the middle of the script, the program crashes with an object error. I have created a custom event handler for the IE automation library to combat this. However, in order to check the error flag, I would have to add an "if" statement or a function after every object-related call to check or errors. There are hundreds upon hundreds of object actions and adding a line of code after each one seems "clunky". Consider the following code: expandcollapse popupGlobal $oIE = _IECreate("Google.com", 0, 1, 1, 1) _IEErrorHandlerRegister("_IEErrorHandler") $result = main() if $result == "" Then ConsoleWrite("success" & @CRLF) Else ConsoleWrite("failed at step: " & $result & @CRLF) EndIf func main() if step1() == false then return "1" if step2() == false then return "2" if step3() == false then return "3" if step4() == false then return "4" if step5() == false then return "5" if step6() == false then return "6" if step7() == false then return "7" if step8() == false then return "8" if step9() == false then return "9" if step10() == false then return "10" return True EndFunc ;example step func step2() ;throughout this function there are error checks that return false if the step failed ;if step has not errors until the end, it succeeded and returns true $oIE.document.action1 $oIE.document.action2 $oIE.document.action3 $oIE.document.action4 $oIE.document.action5 ;... $oIE.document.action100 return True EndFunc func _IEErrorHandler() ; do something on oIE errors EndFunc Let's say there is a COM object error for IE on line 35 ("$oIE.document.action3"). How do I make the script return false on step2() immediately without putting an if statement after each one of those 100 actions (note that each action can have a COM object failure). [font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]
JohnOne Posted January 12, 2012 Posted January 12, 2012 If it were me, I'd probably put code in my error handler function to ensure that the internet is connected and $oIE is an object, however your function still would not return false immediately but you could set a global error flag and return false at the end of the function. You might think if statements are clunky, but if you want a robust script you should consider them. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
jaberwacky Posted January 12, 2012 Posted January 12, 2012 (edited) Consider using == only for case sensitive string comparision. Also, your comment for step2() says that a false is returned upon error but I'm not seeing it. Consider returning something else other than True on sucess because 1 and true are the same. Here is some more accurate based on what you gave us. expandcollapse popup#include <IE.au3> Global $oIE = _IECreate("Google.com", 0, 1, 1, 1) If @error Then Exit -2 _IEErrorHandlerRegister("_IEErrorHandler") Global $result = main() If $result = True Then ConsoleWrite("success" & @CRLF) Else ConsoleWrite("failed at step: " & $result & @CRLF) EndIf Func main() If Not step1() Then Return 1 If Not step2() Then Return 2 If Not step3() Then Return 3 If Not step4() Then Return 4 If Not step5() Then Return 5 If Not step6() Then Return 6 If Not step7() Then Return 7 If Not step8() Then Return 8 If Not step9() Then Return 9 If Not step10() Then Return 10 Return True EndFunc ;==>main ;example step Func step2() ;throughout this function there are error checks that return false if the step failed ;if step has not errors until the end, it succeeded and returns true $oIE.document.action1 $oIE.document.action2 $oIE.document.action3 $oIE.document.action4 $oIE.document.action5 ;... $oIE.document.action100 Return True EndFunc ;==>step2 Func _IEErrorHandler() ; do something on oIE errors EndFunc ;==>_IEErrorHandler Edited January 12, 2012 by LaCastiglione Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum?
JohnOne Posted January 12, 2012 Posted January 12, 2012 (edited) Or perhapsIf Not step1() Then Return SetError(1,1,0)If Not step2() Then Return SetError(1,2,0)If Not step3() Then Return SetError(1,3,0)etc...Return 1 Edited January 12, 2012 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
BrewManNH Posted January 13, 2012 Posted January 13, 2012 Take a look at the code in by Water that shows a custom error handler for object errors and how they can be handled safely. 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 GudeHow 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
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