def _get_control_infos(self, seconds=3): """获取焦点所在应用的所有窗体控件,并便利输出控件信息 Args: seconds (int, optional): 点击后n秒开始分析. Defaults to 3. Yields: [tuple]: 窗体上所有控件的信息 """ time.sleep(seconds) control = auto.GetFocusedControl() if control: for c, d in auto.WalkControl(control, True): mlog.instance().logger.info(c.ControlTypeName + " " + str(d)) yield ( c.ControlType, c.ClassName, "[{},{},{},{}]".format( c.BoundingRectangle.left, c.BoundingRectangle.top, c.BoundingRectangle.right, c.BoundingRectangle.bottom, ), c.Name, "0x{0:X}({0})".format(c.NativeWindowHandle), str(d), )
def getActiveWindowControls(): #Reset control list to just get the currently selected window control = auto.GetFocusedControl() controlList = [] while control: controlList.insert(0, control) control = control.GetParentControl() if len(controlList) == 1: control = controlList[0] else: control = controlList[1] # Get controls of the target window # Draw bounding box of active controls # detectControls = ['TextControl', 'ButtonControl', 'EditControl'] # for c, d in auto.WalkControl(control, True, 1000): # if c.ControlTypeName in detectControls: # print(c.ControlTypeName, c.ClassName, c.AutomationId, c.BoundingRectangle, c.Name, '0x{0:X}({0})'.format(c.NativeWindowHandle)) # # if object_from_db.lower() in c.Name.lower(): # dc.Clear() # dc.SetPen(wx.Pen("blue")) # dc.SetBrush(wx.Brush("blue", wx.TRANSPARENT)) # ControlBoundingRect = c.BoundingRectangle # xScale = 1 #1.25 # yScale = 1 #1.25 # width = (ControlBoundingRect.right * xScale) - (ControlBoundingRect.left * xScale) # height = (ControlBoundingRect.bottom * yScale) - (ControlBoundingRect.top * yScale) # dc.DrawRectangle((ControlBoundingRect.left * xScale),(ControlBoundingRect.top * yScale),width,height) # return control
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 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()
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
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)
def main(): import getopt auto.Logger.Write('UIAutomation {} (Python {}.{}.{}, {} bit)\n'.format( auto.VERSION, sys.version_info.major, sys.version_info.minor, sys.version_info.micro, 64 if sys.maxsize > 0xFFFFFFFF else 32)) options, args = getopt.getopt(sys.argv[1:], 'hrfcand:t:', [ 'help', 'root', 'focus', 'cursor', 'ancestor', 'showAllName', 'depth=', 'time=' ]) root = False focus = False cursor = False ancestor = False foreground = True showAllName = False depth = 0xFFFFFFFF seconds = 3 for (o, v) in options: if o in ('-h', '-help'): usage() exit(0) elif o in ('-r', '-root'): root = True foreground = False elif o in ('-f', '-focus'): focus = True foreground = False elif o in ('-c', '-cursor'): cursor = True foreground = False elif o in ('-a', '-ancestor'): ancestor = True foreground = False elif o in ('-n', '-showAllName'): showAllName = True elif o in ('-d', '-depth'): depth = int(v) elif o in ('-t', '-time'): seconds = int(v) if seconds > 0: auto.Logger.Write('please wait for {0} seconds\n\n'.format(seconds), writeToFile=False) time.sleep(seconds) auto.Logger.Log('Starts, Current Cursor Position: {}'.format( auto.GetCursorPos())) control = None if root: control = auto.GetRootControl() if focus: control = auto.GetFocusedControl() if cursor: control = auto.ControlFromCursor() if depth < 0: while depth < 0 and control: control = control.GetParentControl() depth += 1 depth = 0xFFFFFFFF if ancestor: control = auto.ControlFromCursor() if control: auto.EnumAndLogControlAncestors(control, showAllName) else: auto.Logger.Write( 'IUIAutomation returns null element under cursor\n', auto.ConsoleColor.Yellow) else: indent = 0 if not control: control = auto.GetFocusedControl() controlList = [] while control: controlList.insert(0, control) control = control.GetParentControl() if len(controlList) == 1: control = controlList[0] else: control = controlList[1] if foreground: indent = 1 auto.LogControl(controlList[0], 0, showAllName) auto.EnumAndLogControl(control, depth, showAllName, startDepth=indent) auto.Logger.Log('Ends\n')
def DemoCN(): """for Chinese language""" thisWindow = auto.GetConsoleWindow() auto.Logger.ColorfullyWrite( '我将运行<Color=Cyan>cmd</Color>并设置它的<Color=Cyan>屏幕缓冲区</Color>使<Color=Cyan>cmd</Color>一行能容纳很多字符\n\n' ) time.sleep(3) auto.SendKeys('{Win}r') while not isinstance(auto.GetFocusedControl(), auto.EditControl): time.sleep(1) auto.SendKeys('cmd{Enter}') cmdWindow = auto.WindowControl(RegexName='.+cmd.exe') cmdWindow.TitleBarControl().RightClick() auto.SendKey(auto.Keys.VK_P) optionWindow = cmdWindow.WindowControl(SubName='属性') optionWindow.TabItemControl(SubName='选项').Click() optionTab = optionWindow.PaneControl(SubName='选项') checkBox = optionTab.CheckBoxControl(AutomationId='103') if checkBox.GetTogglePattern().ToggleState != auto.ToggleState.On: checkBox.Click() checkBox = optionTab.CheckBoxControl(AutomationId='104') if checkBox.GetTogglePattern().ToggleState != auto.ToggleState.On: checkBox.Click() optionWindow.TabItemControl(SubName='布局').Click() layoutTab = optionWindow.PaneControl(SubName='布局') layoutTab.EditControl(AutomationId='301').GetValuePattern().SetValue('300') layoutTab.EditControl( AutomationId='303').GetValuePattern().SetValue('3000') layoutTab.EditControl(AutomationId='305').GetValuePattern().SetValue('140') layoutTab.EditControl(AutomationId='307').GetValuePattern().SetValue('30') optionWindow.ButtonControl(AutomationId='1').Click() cmdWindow.SetActive() rect = cmdWindow.BoundingRectangle auto.DragDrop(rect.left + 50, rect.top + 10, 50, 30) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '我将运行<Color=Cyan>记事本</Color>并输入<Color=Cyan>Hello!!!</Color>\n\n') time.sleep(3) subprocess.Popen('notepad') notepadWindow = auto.WindowControl(searchDepth=1, ClassName='Notepad') cx, cy = auto.GetScreenSize() notepadWindow.MoveWindow(cx // 2, 20, cx // 2, cy // 2) time.sleep(0.5) notepadWindow.EditControl().SendKeys('Hello!!!', 0.05) time.sleep(1) dir = os.path.dirname(__file__) scriptPath = os.path.abspath(os.path.join(dir, '..\\automation.py')) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '运行"<Color=Cyan>automation.py -h</Color>"显示帮助\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -h'.format(scriptPath) + '{Enter}', 0.05) time.sleep(3) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '运行"<Color=Cyan>automation.py -r -d1</Color>"显示所有顶层窗口, 即桌面的子窗口\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -r -d1 -t0'.format(scriptPath) + '{Enter}', 0.05) time.sleep(3) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '运行"<Color=Cyan>automation.py -c</Color>"显示鼠标光标下的控件\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -c -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.MoveCursorToMyCenter() time.sleep(3) cmdWindow.SetActive(waitTime=2) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '运行"<Color=Cyan>automation.py -a</Color>"显示鼠标光标下的控件和它的所有父控件\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -a -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.MoveCursorToMyCenter() time.sleep(3) cmdWindow.SetActive(waitTime=2) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '运行"<Color=Cyan>automation.py</Color>"显示当前激活窗口和它的所有子控件\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.EditControl().Click() time.sleep(3) cmdWindow.SetActive(waitTime=2) time.sleep(3) thisWindow.SetActive() auto.Logger.WriteLine('演示结束,按Enter退出', auto.ConsoleColor.Green) input()
def DemoEN(): """for other language""" thisWindow = auto.GetConsoleWindow() auto.Logger.ColorfullyWrite('I will run <Color=Cyan>cmd</Color>\n\n') time.sleep(3) auto.SendKeys('{Win}r') while not isinstance(auto.GetFocusedControl(), auto.EditControl): time.sleep(1) auto.SendKeys('cmd{Enter}') cmdWindow = auto.WindowControl(SubName='cmd.exe') rect = cmdWindow.BoundingRectangle auto.DragDrop(rect.left + 50, rect.top + 10, 50, 10) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'I will run <Color=Cyan>Notepad</Color> and type <Color=Cyan>Hello!!!</Color>\n\n' ) time.sleep(3) subprocess.Popen('notepad') notepadWindow = auto.WindowControl(searchDepth=1, ClassName='Notepad') cx, cy = auto.GetScreenSize() notepadWindow.MoveWindow(cx // 2, 20, cx // 2, cy // 2) time.sleep(0.5) notepadWindow.EditControl().SendKeys('Hello!!!', 0.05) time.sleep(1) dir = os.path.dirname(__file__) scriptPath = os.path.abspath(os.path.join(dir, '..\\automation.py')) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'run "<Color=Cyan>automation.py -h</Color>" to display the help\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -h'.format(scriptPath) + '{Enter}', 0.05) time.sleep(3) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'run "<Color=Cyan>automation.py -r -d1</Color>" to display the top level windows, desktop\'s children\n\n' ) time.sleep(3) cmdWindow.SendKeys('"{}" -r -d1 -t0'.format(scriptPath) + '{Enter}', 0.05) time.sleep(3) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'run "<Color=Cyan>automation.py -c</Color>" to display the control under mouse cursor\n\n' ) time.sleep(3) cmdWindow.SendKeys('"{}" -c -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.MoveCursorToMyCenter() time.sleep(3) cmdWindow.SetActive(waitTime=2) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'run "<Color=Cyan>automation.py -a</Color>" to display the control under mouse cursor and its ancestors\n\n' ) time.sleep(3) cmdWindow.SendKeys('"{}" -a -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.MoveCursorToMyCenter() time.sleep(3) cmdWindow.SetActive(waitTime=2) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'run "<Color=Cyan>automation.py</Color>" to display the active window\n\n' ) time.sleep(3) cmdWindow.SendKeys('"{}" -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.EditControl().Click() time.sleep(3) cmdWindow.SetActive(waitTime=2) time.sleep(3) thisWindow.SetActive() auto.Logger.WriteLine('press Enter to exit', auto.ConsoleColor.Green) input()
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())