Пример #1
0
#!python3
# -*- coding:utf-8 -*-
import os
import sys
import subprocess

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(
    __file__))))  # not required after 'pip install uiautomation'
from uiautomation import uiautomation as automation

if __name__ == '__main__':
    isTop = 1
    print(sys.argv)

    if len(sys.argv) == 2:
        isTop = int(sys.argv[1])

    note = automation.WindowControl(searchDepth=1, ClassName='Notepad')
    if note.Exists(0, 0):
        note.SetTopmost(isTop)
    else:
        subprocess.Popen('notepad')
        note.Refind()
        note.SetTopmost(isTop)
        note.Move(0, 0)
        note.Resize(400, 300)
        edit = automation.EditControl(searchFromControl=note)
        edit.Click()
        automation.SendKeys('I\'m a topmost window!!!')
Пример #2
0
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
    if automation.WaitForExist(window, 3):
        automation.Logger.WriteLine("Notepad exists now")
    else:
        automation.Logger.WriteLine("Notepad does not exist after 3 seconds",
                                    automation.ConsoleColor.Yellow)
    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()
    if automation.WaitForDisappear(window, 3):
        automation.Logger.WriteLine("Notepad closed")
    else:
        automation.Logger.WriteLine("Notepad still exists after 3 seconds",
                                    automation.ConsoleColor.Yellow)
    # 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 RenameTreeItem(tree, treeItem, bookMarks, depth, removeChapter=True):
    treeItem.ScrollIntoView()
    if depth > TreeDepth:
        return
    name = treeItem.Name
    if not name.strip():
        return
    newName, alert = RenameFunction(name, removeChapter)
    if newName.startswith('Appendix'):
        removeChapter = False
    bookMark = BookMark(name, newName)
    bookMarks.append(bookMark)
    if newName != name and depth not in TreeSkipDepthList:
        global Renamed
        Renamed = True
        time.sleep(0.1)
        find = False
        for i in range(3):
            treeItem.RightClick(simulateMove=False, waitTime=0.2)
            # FoxitReader书签右键菜单(BCGPToolBar,非Windows菜单)弹出后,枚举不到菜单,但从屏幕点上ControlFromPoint能获取到菜单, todo
            # 采用特殊处理获取重命名菜单
            x, y = automation.Win32API.GetCursorPos()
            menuItem = automation.ControlFromPoint(x + 2, y + 2)
            if isinstance(menuItem, automation.MenuItemControl):
                #鼠标右下方弹出菜单
                while 1:
                    if menuItem.Name == '重命名(R)' or menuItem.Name == 'Rename':
                        find = True
                        break
                    y += 20
                    menuItem = automation.ControlFromPoint(x + 2, y)
                    if not isinstance(menuItem, automation.MenuItemControl):
                        break
            else:
                #鼠标右上方弹出菜单
                menuItem = automation.ControlFromPoint(x + 2, y - 2)
                while 1:
                    if menuItem.Name == '重命名(R)' or menuItem.Name == 'Rename':
                        find = True
                        break
                    y -= 20
                    menuItem = automation.ControlFromPoint(x + 2, y)
                    if not isinstance(menuItem, automation.MenuItemControl):
                        break
            if find:
                break
            else:
                automation.SendKeys('{Esc}')
                time.sleep(0.5)
        if not find:
            automation.Logger.Log('this pdf not support editing')
            exit(0)
        menuItem.Click(simulateMove=False, waitTime=0.2)
        edit = automation.EditControl(searchFromControl=tree, searchDepth=1)
        edit.SetValue(newName, waitTime=0.05)
        automation.SendKeys('{Enter}', waitTime=0.05)
        automation.Logger.Write('rename: ')
        automation.Logger.WriteLine(name, automation.ConsoleColor.Green)
        automation.Logger.Write('    @@to : ' if alert else '    to: ')
        automation.Logger.WriteLine(
            newName, automation.ConsoleColor.Yellow
            if alert else automation.ConsoleColor.Green)
    if depth + 1 > TreeDepth:
        return
    if automation.ExpandCollapseState.Collapsed == treeItem.CurrentExpandCollapseState(
    ):
        treeItem.Expand(0.1)
    childItems = treeItem.GetChildren()
    if childItems:
        for child in childItems:
            RenameTreeItem(tree, child, bookMark.children, depth + 1,
                           removeChapter)
Пример #4
0
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):
    if automation.WaitForExist(window, 3):
        automation.Logger.WriteLine("Notepad exists now")
    else:
        automation.Logger.WriteLine("Notepad does not exist after 3 seconds",
                                    automation.ConsoleColor.Yellow)
    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()
    if automation.WaitForDisappear(window, 3):
        automation.Logger.WriteLine("Notepad closed")
    else:
        automation.Logger.WriteLine("Notepad still exists after 3 seconds",
                                    automation.ConsoleColor.Yellow)

    # 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)