Difference between revisions of "Using AutoIT with Python"

From PeformIQ Upgrade
Jump to navigation Jump to search
 
Line 2: Line 2:


* http://www.autoitscript.com/forum/topic/16540-call-autoit-from-python-script/
* http://www.autoitscript.com/forum/topic/16540-call-autoit-from-python-script/


=Sample Scripts=
=Sample Scripts=

Latest revision as of 08:58, 13 March 2013

Some Links

Sample Scripts

import time

from win32com.client import Dispatch

Auto = Dispatch("AutoItX3.Control")

Auto.Run("calc.exe")
Auto.Run("notepad.exe")

time.sleep(2)

title = 'Untitled'

Auto.WinActivate(title, '')

Auto.Send("AZERTYUIOP = ")

time.sleep(1)

title = 'Calculator'

Auto.WinActivate(title, '')

time.sleep(1)
Auto.Send("12345")

time.sleep(0.4)
Auto.Send("{+}")

time.sleep(0.4)
Auto.Send("54321")

time.sleep(0.4)
Auto.Send("{=}")

time.sleep(0.4)
Auto.Send("^c")

time.sleep(1)

Auto.WinClose(title , '')

title = 'Untitled'
Auto.WinActivate(title, '')
Auto.WinClose(title , '')

Auto.Send("^v")
Auto.Send("{ENTER}")
>>> import win32con
>>> import win32gui
>>>
>>> def isRealWindow(hWnd):
...     '''Return True iff given window is a real Windows application window.'''

...     if not win32gui.IsWindowVisible(hWnd):
...         return False
...     if win32gui.GetParent(hWnd) != 0:
...         return False
...     hasNoOwner = win32gui.GetWindow(hWnd, win32con.GW_OWNER) == 0
...     lExStyle = win32gui.GetWindowLong(hWnd, win32con.GWL_EXSTYLE)
...     if (((lExStyle & win32con.WS_EX_TOOLWINDOW) == 0 and hasNoOwner)
...       or ((lExStyle & win32con.WS_EX_APPWINDOW != 0) and not hasNoOwner)):
...         if win32gui.GetWindowText(hWnd):
...             return True
...     return False
...
>>> def getWindowSizes():
...     '''
...     Return a list of tuples (handler, (width, height)) for each real window.

...     '''
...     def callback(hWnd, windows):
...         if not isRealWindow(hWnd):
...             return
...         rect = win32gui.GetWindowRect(hWnd)
...         windows.append((hWnd, (rect[2] - rect[0], rect[3] - rect[1])))
...     windows = []
...     win32gui.EnumWindows(callback, windows)
...     return windows
...
>>> for win in getWindowSizes():
...     print win
...
(1378864, (677, 522))
(330382, (1541, 1232))
(131570, (1438, 860))
(1050722, (1080, 774))
(2753658, (900, 791))
(2689504, (1035, 677))
(329140, (836, 823))
(262866, (1035, 677))
(527250, (997, 677))
(65904, (799, 243))
(3148144, (857, 243))
(66128, (260, 450))
(724640, (836, 823))
(1312158, (1390, 745))
(329670, (1021, 800))
(330502, (677, 342))
(1183304, (677, 750))
(66820, (800, 600))
(134570, (1326, 646))
(329896, (412, 131))
>>> import win32com.client
>>> oAutoItX = win32com.client.Dispatch( "AutoItX3.Control" )
>>>
>>> oAutoItX.Opt("WinTitleMatchMode", 2)  # Match text anywhere in a window title
2
>>>
>>> width  = oAutoItX.WinGetClientSizeWidth("Firefox")
>>> height = oAutoItX.WinGetClientSizeHeight("Firefox")
>>>
>>> print width, height
1428 894
>>> autoIt = win32com.client.Dispatch("AutoItX3.Control")
>>> windowHandle = autoIt.WinGetHandle(knownWindowTitle)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'knownWindowTitle' is not defined
>>> returnedWindowTitle = autoIt.WinGetTitle(windowHandle)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'windowHandle' is not defined

>>> windowHandle = autoIt.WinGetHandle("FastStone Capture")
>>> print windowHandle
00140DB6
>>> returnedWindowTitle = autoIt.WinGetTitle(windowHandle)
>>> print returnedWindowTitle
0


>>> autoIt = win32com.client.Dispatch("AutoItX3.Control")
>>> mainWindowTitle = "Untitled"
>>> mainWindowHandle = autoIt.WinGetHandle(mainWindowTitle)
>>> print mainWindowHandle
>>> mainWindowHandle = autoIt.WinGetHandle(mainWindowTitle)
>>> print mainWindowHandle
00090FEC
>>> autoIt.WinGetTitle(mainWindowHandle)
u'0'

>>> testHandle = "[HANDLE:%s]" % mainWindowHandle
>>> autoIt.WinGetTitle(testHandle)
u'Untitled - Notepad'


>>> autoIt = win32com.client.Dispatch("AutoItX3.Control")
>>> autoIt.Run("notepad.exe")
4512
>>> mainWindowTitle = "Untitled"
>>> mainWindowHandle = autoIt.WinGetHandle(mainWindowTitle)
>>> testHandle = "[HANDLE:%s]" % mainWindowHandle
>>> autoIt.WinGetTitle(testHandle)
u'Untitled - Notepad'
>>> autoIt.Run("c:\\Program Files\\GE Energy\\PowerOn\\EnmacClient.exe")
7392