def testNotepadCN(): consoleWindow = automation.GetConsoleWindow() consoleWindow.SetActive() automation.Logger.ColorfulWriteLine( '\nI will open <Color=Green>Notepad</Color> and <Color=Yellow>automate</Color> it. Please wait for a while.' ) time.sleep(2) automation.ShowDesktop() #打开notepad subprocess.Popen('notepad') #查找notepad, 如果name有中文,python2中要使用Unicode window = automation.WindowControl(searchDepth=1, ClassName='Notepad', SubName='无标题 - 记事本') #可以判断window是否存在,如果不判断,找不到window的话会抛出异常 #if window.Exists(maxSearchSeconds = 3): screenWidth, screenHeight = automation.Win32API.GetScreenSize() window.MoveWindow(screenWidth // 4, screenHeight // 4, screenWidth // 2, screenHeight // 2) window.SetActive() #查找edit edit = automation.EditControl( searchFromControl=window) #or edit = window.EditControl() edit.Click(waitTime=0) #python2中要使用Unicode, 模拟按键 edit.SetValue('hi你好') edit.SendKeys('{Ctrl}{End}{Enter}下面开始演示{! 4}{ENTER}', 0.2, 0) edit.SendKeys(text) edit.SendKeys('{Enter 3}0123456789{Enter}', waitTime=0) edit.SendKeys('ABCDEFGHIJKLMNOPQRSTUVWXYZ{ENTER}', waitTime=0) edit.SendKeys('abcdefghijklmnopqrstuvwxyz{ENTER}', waitTime=0) edit.SendKeys('`~!@#$%^&*()-_=+{ENTER}', waitTime=0) edit.SendKeys('[]{{}{}}\\|;:\'\",<.>/?{ENTER}', waitTime=0) edit.SendKeys( '™®①②③④⑤⑥⑦⑧⑨⑩§№☆★○●◎◇◆□℃‰€■△▲※→←↑↓〓¤°#&@\^_―♂♀{ENTER}{CTRL}a') window.CaptureToImage('Notepad.png') edit.SendKeys('Image Notepad.png was captured, you will see it later.', 0.05) #查找菜单 window.MenuItemControl(Name='格式(O)').Click() window.MenuItemControl(Name='字体(F)...').Click() windowFont = window.WindowControl(Name='字体') windowFont.ComboBoxControl(AutomationId='1140').Select('中文 GB2312') windowFont.ButtonControl(Name='确定').Click() window.Close() # buttonNotSave = ButtonControl(searchFromControl = window, SubName = '不保存') # buttonNotSave.Click() # or send alt+n to not save and quit # automation.SendKeys('{Alt}n') # 使用另一种查找方法 buttonNotSave = automation.FindControl( window, lambda control, depth: control.ControlType == automation. ControlType.ButtonControl and '不保存' in control.Name) buttonNotSave.Click() subprocess.Popen('Notepad.png', shell=True) time.sleep(2) consoleWindow.SetActive() automation.Logger.WriteLine('script exits', automation.ConsoleColor.Cyan) time.sleep(2)
def testNotepadEN(): consoleWindow = automation.GetConsoleWindow() consoleWindow.SetActive() automation.Logger.ColorfulWriteLine( '\nI will open <Color=Green>Notepad</Color> and <Color=Yellow>automate</Color> it. Please wait for a while.' ) time.sleep(2) automation.ShowDesktop() subprocess.Popen('notepad') #search notepad window, if searchFromControl is None, search from RootControl #searchDepth = 1 makes searching faster, only searches RootControl's children, not children's children window = automation.WindowControl(searchDepth=1, ClassName='Notepad', SubName='Untitled - Notepad') #if window.Exists(maxSearchSeconds = 3): #check before using it #pass screenWidth, screenHeight = automation.Win32API.GetScreenSize() window.MoveWindow(screenWidth // 4, screenHeight // 4, screenWidth // 2, screenHeight // 2) window.SetActive() edit = automation.EditControl( searchFromControl=window) #or edit = window.EditControl() edit.Click(waitTime=0) edit.SetValue('hi你好') edit.SendKeys('{Ctrl}{End}{Enter}下面开始演示{! 4}{ENTER}', 0.2, 0) edit.SendKeys(text) edit.SendKeys('{Enter 3}0123456789{Enter}', waitTime=0) edit.SendKeys('ABCDEFGHIJKLMNOPQRSTUVWXYZ{Enter}', waitTime=0) edit.SendKeys('abcdefghijklmnopqrstuvwxyz{Enter}', waitTime=0) edit.SendKeys('`~!@#$%^&*()-_=+{Enter}', waitTime=0) edit.SendKeys('[]{{}{}}\\|;:\'\",<.>/?{Enter}{Ctrl}a') window.CaptureToImage('Notepad.png') edit.SendKeys('Image Notepad.png was captured, you will see it later.', 0.05) #find menu window.MenuItemControl(Name='Format').Click() window.MenuItemControl(Name='Font...').Click() windowFont = window.WindowControl(Name='Font') windowFont.ComboBoxControl(AutomationId='1140').Select('Western') windowFont.ButtonControl(Name='OK').Click() window.Close() # buttonNotSave = ButtonControl(searchFromControl = window, Name = 'Don\'t Save') # buttonNotSave.Click() # or send alt+n to not save and quit # automation.SendKeys('{Alt}n') # another way to find the button using lambda buttonNotSave = automation.FindControl( window, lambda control, depth: control.ControlType == automation. ControlType.ButtonControl and 'Don\'t Save' == control.Name) buttonNotSave.Click() subprocess.Popen('Notepad.png', shell=True) time.sleep(2) consoleWindow.SetActive() automation.Logger.WriteLine('script exits', automation.ConsoleColor.Cyan) time.sleep(2)
def download_song(task): print("download_song") TextToSpeech.get_instance().send_message("downloading song") import youtube_dl import uiautomation as automation control = automation.GetFocusedControl() controlList = [] while control: controlList.insert(0, control) control = control.GetParentControl() if len(controlList) == 1: control = controlList[0] else: control = controlList[1] address_control = automation.FindControl( control, lambda c, d: isinstance(c, automation.EditControl) and "Address and search bar" in c.Name) if address_control is None: return False try: song_url = address_control.CurrentValue() import os import sys import youtube_dl project_path = os.path.dirname(sys.argv[0]) os.chdir(project_path + "//ffprobe") ydl_opts = { 'format': 'bestaudio/best', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192', }], 'outtmpl': project_path + "//downloads//%(title)s.%(ext)s", } with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([song_url]) os.chdir(project_path) return True except AttributeError as error: return False
def get_chrome_url(focused=False): try: if not focused: control = automation.FindControl( automation.GetRootControl(), lambda c, d: c.ClassName.startswith('Chrome')) else: control = automation.GetFocusedControl() control_list = [] while control: control_list.insert(0, control) control = control.GetParentControl() if len(control_list) == 1: control = control_list[0] else: control = control_list[1] address_control = automation.FindControl( control, lambda c, d: isinstance(c, automation.EditControl) and "Address and search bar" in c.Name) return address_control.CurrentValue() except AttributeError: return None
def main(): firefoxWindow = automation.WindowControl(searchDepth=1, ClassName='MozillaWindowClass') if not firefoxWindow.Exists(0): automation.Logger.WriteLine('please run Firefox first', automation.ConsoleColor.Yellow) return firefoxWindow.ShowWindow(automation.ShowWindow.Maximize) firefoxWindow.SetActive() time.sleep(1) tab = firefoxWindow.TabControl() newTabButton = tab.ButtonControl(searchDepth=1) newTabButton.Click() edit = firefoxWindow.EditControl() # edit.Click() edit.SendKeys( 'http://global.bing.com/?rb=0&setmkt=en-us&setlang=en-us{Enter}') time.sleep(2) searchEdit = automation.FindControl( firefoxWindow, lambda c, d: (isinstance(c, automation.EditControl) or isinstance(c, automation.ComboBoxControl)) and c.Name == 'Enter your search term') # searchEdit.Click() searchEdit.SendKeys( 'Python-UIAutomation-for-Windows site:github.com{Enter}', 0.05) link = firefoxWindow.HyperlinkControl( SubName='yinkaisheng/Python-UIAutomation-for-Windows') automation.Win32API.PressKey(automation.Keys.VK_CONTROL) link.Click() #press control to open the page in a new tab automation.Win32API.ReleaseKey(automation.Keys.VK_CONTROL) newTab = tab.TabItemControl( SubName='yinkaisheng/Python-UIAutomation-for-Windows') newTab.Click() starButton = firefoxWindow.ButtonControl(Name='Star this repository') if starButton.Exists(5): automation.GetConsoleWindow().SetActive() automation.Logger.WriteLine( 'Star Python-UIAutomation-for-Windows after 2 seconds', automation.ConsoleColor.Yellow) time.sleep(2) firefoxWindow.SetActive() time.sleep(1) starButton.Click() time.sleep(2) else: unstarButton = firefoxWindow.ButtonControl( Name='Unstar this repository') if unstarButton.Exists(0, 0): automation.GetConsoleWindow().SetActive() automation.Logger.WriteLine('Thank you. You have starred.', automation.ConsoleColor.Yellow)
def chrome_get_url(): control = automation.GetFocusedControl() control_list = [] while control: control_list.insert(0, control) control = control.GetParentControl() if len(control_list) == 1: control = control_list[0] else: control = control_list[1] address_control = automation.FindControl( control, lambda c, d: isinstance(c, automation.EditControl) and "Address and search bar" in c.Name) return address_control.CurrentValue()
async def update_status(ctx): if __name__ == '__main__': # sleep function designed to hook the omnibox URL sleep(3) control: Control = automation.GetFocusedControl() controlList = [] while control: controlList.insert(0, control) control = control.GetParentControl() if len(controlList) == 1: control = controlList[0] else: control = controlList[1] address_control = { automation.FindControl( control, lambda c, d: not (not isinstance(c, automation.EditControl))) } URL = address_control.CurrentValue() # split the URL into a readable format sep = '/' split_URL = URL.split(sep, 1)[0] await ctx.send(split_URL)
# notepadWindow.SetTopmost(True) # edit = notepadWindow.EditControl() # edit.SetValue('Hello') # edit.SendKeys('{Ctrl}{End}{Enter}World') subprocess.Popen('C:\Program Files (x86)\PesticideModels\TOPRice\TOPrice.exe') window = auto.WindowControl(searchDepth=1, title_re='TOPrice*') if auto.WaitForExist(window, 3): auto.Logger.WriteLine("Notepad exists now") else: auto.Logger.WriteLine("Notepad does not exist after 3 seconds", auto.ConsoleColor.Yellow) window.SetActive() # windowFont = window.WindowControl(Name='File') # print(window.Name) # window.window_(title_re = u'File').Click() # # # window.SetTopmost(True) # window.ButtonControl(name="Projects").Click() # button = auto.ButtonControl(Name='Projects') # button.Click() button = auto.FindControl( window, lambda c: (isinstance(c, auto.EditControl) or isinstance( c, auto.ComboBoxControl)) and c.Name == 'Projects') button.Click() sleep(10) # os.system(r"python -m poetry run python C:\Users\snow\AppData\Local\pypoetry\Cache\virtualenvs\read-html-venv--Gq2blu2-py3.6\Scripts\automation.py -r –d1 –t3 -n")
def menuOperation(redWindow): menuMulItems = [u'历史记录(H)', u'书签(B)', u'更多工具(L)', u'退出(X) Ctrl+Shift+Q'] bookmarker = [ u'为此网页添加书签… Ctrl+D', u'显示书签栏(S) Ctrl+Shift+B', u'书签管理器(B) Ctrl+Shift+O', u'导入书签和设置...' ] moretools = [ u'网页另存为(A)... Ctrl+S', u'清除浏览数据(C)... Ctrl+Shift+Del', u'任务管理器(T) Shift+Esc', u'开发者工具(D) Ctrl+Shift+I' ] # moretools= [u'任务管理器(T) Shift+Esc'] for chindMenu in menuMulItems: time.sleep(1) if chindMenu == u'历史记录(H)': redWindow.MenuItemControl(searchDepth=5, searchWaitTime=10, foundIndex=2, name=u'红芯企业浏览器').Click() time.sleep(1) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance( c, automation.MenuControl)) and c.Name == chindMenu) searchEdit.Click() time.sleep(2) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance( c, automation.MenuControl) ) and c.Name == u'历史记录(H) Ctrl+H') #操作二级菜单 searchEdit.Click() time.sleep(3) if chindMenu == u'书签(B)': for index in range(len(bookmarker)): if bookmarker[index] == u'导入书签和设置...': redWindow.MenuItemControl(searchDepth=5, searchWaitTime=10, foundIndex=2, name=u'红芯企业浏览器').Click() time.sleep(1) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl ) or isinstance(c, automation.MenuControl) ) and c.Name == chindMenu) searchEdit.Click() #searchEdit = automation.FindControl(redWindow,lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance(c, automation.MenuControl)) and c.Name == bookmarker[0]) #操作二级菜单… Ctrl+D redWindow.MoveCursor(ratioY=0.2, ratioX=0.7) time.sleep(2) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl ) or isinstance(c, automation.MenuControl) ) and c.Name == bookmarker[index]) #操作二级菜单… Ctrl+D searchEdit.Click() time.sleep(2) redWindow.SendKeys('{esc}') else: searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.EditControl) and c.Name == u'地址和搜索栏')) searchEdit.Click() searchEdit.SendKeys('www.redcore.cn{Enter}', 0.05) time.sleep(2) redWindow.MenuItemControl(searchDepth=5, searchWaitTime=10, foundIndex=2, name=u'红芯企业浏览器').Click() time.sleep(1) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl ) or isinstance(c, automation.MenuControl) ) and c.Name == chindMenu) #操作一级菜单 searchEdit.Click() time.sleep(2) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl ) or isinstance(c, automation.MenuControl) ) and c.Name == bookmarker[0]) #操作二级菜单… Ctrl+D redWindow.MoveCursor(ratioY=0.2, ratioX=0.7) #searchEdit.MoveMoveCursor() searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl ) or isinstance(c, automation.MenuControl) ) and c.Name == bookmarker[index]) #操作二级菜单… Ctrl+D searchEdit.Click() time.sleep(2) redWindow.SendKeys('{Enter}') print("书签:" + moretools[index]) if chindMenu == u'更多工具(L)': for index in range(len(moretools)): if moretools[index] == u'网页另存为(A)... Ctrl+S': redWindow.MenuItemControl(searchDepth=5, searchWaitTime=10, foundIndex=2, name=u'红芯企业浏览器').Click() time.sleep(1) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl ) or isinstance(c, automation.MenuControl) ) and c.Name == chindMenu) searchEdit.Click() time.sleep(2) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl ) or isinstance(c, automation.MenuControl) ) and c.Name == moretools[index]) #操作二级菜单 searchEdit.Click() time.sleep(2) #另存为窗口处理 parentdWindow = automation.WindowControl( searchDepth=1, ClassName='Chrome_WidgetWin_1') saveas = parentdWindow.WindowControl(searchDepth=3, searchWaitTime=10, name=u'另存为') saveas.MoveToCenter() print(saveas) time.sleep(1) button = saveas.ButtonControl(searchDepth=1, searchWaitTime=10, AutomationId='2') print(button) button.Click() time.sleep(1) elif moretools[index] == u'任务管理器(T) Shift+Esc': redWindow.MenuItemControl(searchDepth=5, searchWaitTime=10, foundIndex=2, name=u'红芯企业浏览器').Click() time.sleep(1) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl ) or isinstance(c, automation.MenuControl) ) and c.Name == chindMenu) searchEdit.Click() time.sleep(2) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl ) or isinstance(c, automation.MenuControl) ) and c.Name == moretools[index]) #操作二级菜单 searchEdit.Click() time.sleep(2) taskWindow = automation.WindowControl( name=u'任务管理器 - 红芯企业浏览器', searchDepth=1, ClassName='Chrome_WidgetWin_1') closeButton = taskWindow.ButtonControl(searchDepth=2, Name=u'关闭') closeButton.Click() else: redWindow.MenuItemControl(searchDepth=5, searchWaitTime=10, foundIndex=2, name=u'红芯企业浏览器').Click() time.sleep(1) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl ) or isinstance(c, automation.MenuControl) ) and c.Name == chindMenu) searchEdit.Click() time.sleep(2) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl ) or isinstance(c, automation.MenuControl) ) and c.Name == moretools[index]) #操作二级菜单 searchEdit.Click() time.sleep(2) redWindow.SendKeys('{esc}') # searchEdit = automation.FindControl(redWindow,lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance(c, automation.MenuControl)) and c.Name == u'保存(S)') #searchEdit.Click() # redWindow.SendKeys('{Esc}') time.sleep(2) print("更多工具" + moretools[index]) print("多级菜单循环数据 " + chindMenu) else: print("没有循环数据!") print("多级菜单完成循环!") ''' 操作浏览器菜单, menuSingleItems包含的项,只操作没有下级菜单的选项 Operation redcore browser mainMenu, only include Level 1 menus ''' menuSingleItems = [ u'打开新的标签页(T)', u'下载内容(D) Ctrl+J', u'查找(F)... Ctrl+F', u'设置(S)', u'关于 红芯企业浏览器(C)', u'打印(P)... Ctrl+P', u'打开新的窗口(N) Ctrl+N', u'退出(X) Ctrl+Shift+Q' ] #,u'退出(X) Ctrl+Shift+Q' for chindMenu in menuSingleItems: time.sleep(1) if chindMenu == u'打印(P)... Ctrl+P1': redWindow.MenuItemControl(searchDepth=5, searchWaitTime=10, foundIndex=2, name=u'红芯企业浏览器').Click() time.sleep(1) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance( c, automation.MenuControl)) and c.Name == chindMenu) searchEdit.Click() time.sleep(3) redWindow.SendKeys('{Esc}') elif chindMenu == u'打开新的窗口(N) Ctrl+N': redWindow.MenuItemControl(searchDepth=5, searchWaitTime=10, foundIndex=2, name=u'红芯企业浏览器').Click() time.sleep(1) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance( c, automation.MenuControl)) and c.Name == chindMenu) searchEdit.Click() time.sleep(2) redWindow.SendKeys('{Alt}{F4}') elif chindMenu == u'退出(X) Ctrl+Shift+Q': redWindow = automation.WindowControl( searchDepth=1, ClassName='Chrome_WidgetWin_1') #取浏览器handle redWindow.MenuItemControl(searchDepth=5, searchWaitTime=10, foundIndex=2, name=u'红芯企业浏览器').Click() time.sleep(1) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance( c, automation.MenuControl)) and c.Name == chindMenu) searchEdit.Click() time.sleep(2) else: redWindow.MenuItemControl(searchDepth=5, searchWaitTime=10, foundIndex=2, name=u'红芯企业浏览器').Click() time.sleep(1) searchEdit = automation.FindControl( redWindow, lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance( c, automation.MenuControl)) and c.Name == chindMenu) searchEdit.Click() print("一级菜单循环数据 " + chindMenu) else: print("没有循环数据!") print("一级菜单完成循环!")
from time import sleep import uiautomation as automation if __name__ == '__main__': sleep(3) control = automation.GetFocusedControl() controlList = [] while control: controlList.insert(0, control) control = control.GetParentControl() if len(controlList) == 1: control = controlList[0] else: control = controlList[1] address_control = automation.FindControl( control, lambda c, d: isinstance(c, automation.EditControl) and "Address and search bar" in c.Name) print(address_control.CurrentValue())
def getCurrentApplication(): # Getting Chrome Tab code copied with minor modification from stack overflow: # https://stackoverflow.com/questions/11645123/how-do-i-get-the-url-of-the-active-google-chrome-tab-in-windows # Thanks @proggeo(https://stackoverflow.com/users/7594271/proggeo) control = automation.GetForegroundControl() # If the current window is chromium based if (control.ClassName == "Chrome_WidgetWin_1"): address_control = automation.FindControl( control, lambda c, d: isinstance(c, automation.EditControl) and "Address and search bar" in c.Name) # Yeah its Google Chrome if (address_control): url = address_control.CurrentValue() domain = urlparse(url).netloc # We are on youtube if (domain == "www.youtube.com"): yt_control = automation.FindControl( control, lambda c, d: isinstance(c, automation.TabItemControl ) and "- YouTube" in c.Name) # Probably on a video if (yt_control): video_name = yt_control.Name[:-10] return "YouTube - " + video_name # Might not be on a video else: # Nevermind actually in a video if ("/watch?v=" in url): return "YouTube - Unknown Video" # Probably on the youtube homepage or something else: return "YouTube" else: return domain # Not Google Chrome else: if ("— Atom" in control.Name): return "Atom" elif ("Slack -" in control.Name): return "Slack" elif ("- Discord" in control.Name): return "Discord" else: return control.Name # Other type of chromium based app elif (control.ClassName == "Chrome_WidgetWin_0"): spotify_test = automation.FindControl( control, lambda c, d: isinstance(c, automation.DocumentControl) and "Spotify" in c.Name) # Spotify if (spotify_test): return "Spotify" else: return control.Name # Not a chromium based window else: if (control.ClassName == "CabinetWClass"): file_exp_test = automation.FindControl( control, lambda c, d: isinstance(c, automation.TreeItemControl) and "OneDrive" in c.Name) if (file_exp_test and "This PC" in file_exp_test.GetNextSiblingControl().Name): return "File Explorer" else: return control.Name # Probably Native Microsoft Application elif (control.ClassName == "ApplicationFrameWindow"): enc_name = control.Name.encode("utf-8") if (b"- Mail" in enc_name): return "Mail" if (b"Microsoft Store" in enc_name): return "Microsoft Store" # Don't know what it is else: return str(enc_name)[2:-1] elif (control.ClassName == "vguiPopupWindow"): return control.Name else: if ("- paint.net" in control.Name): return "Paint.net" else: return control.ClassName