YoungRacoon Posted May 18, 2008 Share Posted May 18, 2008 Hi. Here is a piece of code to create a local user (Python). My goal was to check the result of every AutoItX method and exit immediately after first failure. It works but my question is the following: Is it possible to simplify this code in case I don't care which method was failed? I mean, is it possible not to use "if-raise" constructions with every method to exit correctly after first error or failure? It doesn't matter what language I use (Jscript, Python, whatever). I'm eager to understand how to do it properly...expandcollapse popupimport sys import win32com.client oAutoIt=win32com.client.Dispatch('AutoItX3.Control') mscpath = "c:\\windows\\system32\\" treeitem = "Computer Management (Local)|System Tools|Local Users and Groups" username = "testuser" try: oAutoIt.Run('cmd /c "compmgmt.msc"', mscpath, oAutoIt.SW_HIDE) rc = oAutoIt.WinWaitActive("Computer Management", "", 5) if rc == 0: raise UserWarning, 'AutoIt Error: WinWaitActive' oAutoIt.ControlTreeView ("Computer Management", "", 12785, "Expand", treeitem, "") if oAutoIt.error == 1: raise UserWarning, 'AutoIt Error: TreeView - Expand' treeitem = treeitem + "|Users" oAutoIt.ControlTreeView ("Computer Management", "", 12785, "Select", treeitem, "") if oAutoIt.error == 1: raise UserWarning, 'AutoIt Error: TreeView - Select' oAutoIt.Send ("{AppsKey}{Down}{Enter}") rc = oAutoIt.ControlSend("New User", "", "Edit1", username) if rc == 0: raise UserWarning, 'AutoIt Error: EditBox - UserName' rc = oAutoIt.ControlClick("New User","", 1170) if rc == 0: raise UserWarning, 'AutoIt Error: Create Button' rc = oAutoIt.ControlClick("New User","", 2) if rc == 0: raise UserWarning, 'AutoIt Error: Close Button' except UserWarning: raiseThanks in advance, Yuri Link to comment Share on other sites More sharing options...
amokoura Posted May 18, 2008 Share Posted May 18, 2008 (edited) My guess is that you have to check for errors after every AutoIt call. You could create an AutoIt wrapper that could raise errors/exceptions. It might be dirty but once it's done, it's beautiful in the main program code Edited May 18, 2008 by amokoura Link to comment Share on other sites More sharing options...
YoungRacoon Posted May 19, 2008 Author Share Posted May 19, 2008 My guess is that you have to check for errors after every AutoIt call. You could create an AutoIt wrapper that could raise errors/exceptions. It might be dirty but once it's done, it's beautiful in the main program code Do you mean instead of this piece of code... if rc == 0: raise UserWarning, 'AutoIt Error: EditBox - UserName' ... write something like that? AutoItCheckResult (rc, 'EditBox - UserName') where AutoItCheckResult funciton is defined as def AutoItCheckResult (rc, Desc) if rc == 0: Desc = 'AutoIt Error: ' + Desc raise UserWarning, Desc Actually, I don't think it is a good idea. Or did you mean something else? Thanks in advance, Yuri. Link to comment Share on other sites More sharing options...
amokoura Posted May 20, 2008 Share Posted May 20, 2008 Yes that's exactly what I meant. It's the world of wrappers On the other hand, you'll then have a reusable exception-raising AutoIt-megamachine. Link to comment Share on other sites More sharing options...
YoungRacoon Posted May 21, 2008 Author Share Posted May 21, 2008 Yes that's exactly what I meant. It's the world of wrappers On the other hand, you'll then have a reusable exception-raising AutoIt-megamachine.OK, thanks. Need to stick with it a little longer and see what happens. Link to comment Share on other sites More sharing options...
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