Beispiel #1
0
def send_keys_to_copy_location_bar_text():
    # Send "Ctrl+l" to move focus to location bar
    SendKeys(r'<^l')()

    # Sleep to keep from being too fast
    time.sleep(0.05)

    # Send "Ctrl+c" to copy the URL in location bar
    SendKeys(r'<^c')()

    # Sleep to keep from being too fast
    time.sleep(0.05)
Beispiel #2
0
def send_paste_hotkey():
    """
    Hotkey function that sends keys for pasting.

    :return: None.
    """
    # Get window title
    window_title = win32gui.GetWindowText(win32gui.GetForegroundWindow())

    # If the window title contains `VNC Viewer`
    if 'VNC Viewer' in window_title:
        # Send keys
        SendKeys(r'#v')()
    else:
        # Send keys
        SendKeys(r'+{Insert}')()
Beispiel #3
0
def close_foreground_program():
    """
    Close foreground program.

    :return: None.
    """
    # Get foreground window handle
    fg_hwnd = GetForegroundWindow().GetSafeHwnd()

    # Get pywinauto window object
    window = pywinauto.application.Application().window_(handle=fg_hwnd)

    # Get window title
    window_title = win32gui.GetWindowText(win32gui.GetForegroundWindow())

    # If window class is `ConsoleWindowClass`
    if window.Class() == 'ConsoleWindowClass':
        #
        if 'aoikhotkey' in window_title.lower():
            # Print message
            print('Ignore window title with `aoikhotkey`')

            # Return
            return

        # Use "pywinauto" to close because "ConsoleWindowClass" do not respond
        # to "Alt+F4".
        window.Close()
    # If window class is not `ConsoleWindowClass`
    else:
        # If the window title contains `VNC Viewer`
        if 'VNC Viewer' in window_title:
            # Send keys
            SendKeys('#q')()

            # Return
            return

        # If the window title contains `Sublime Text`
        if 'Sublime Text' in window_title:
            # Print message
            print('Ignore window title with `Sublime Text`')

            # Return
            return

        # Close the window
        window.Close()
Beispiel #4
0
def paste_text(text):
    """
    Set text to clipboard and paste.

    :param text: the text to paste.

    :return: None.
    """
    # Set text to clipboard
    clipboard_set_text(text)

    # Sleep
    time.sleep(0.05)

    # Send "Ctrl+v" to paste
    SendKeys('^v')()
Beispiel #5
0
def id_generate_paste():
    """
    Generate a random base62-encoded text, set it to clipboard and paste.

    :return: None.
    """
    # Generate ID text
    id_text = base62_text_gen(text_length=5)

    # Set to clipboard
    clipboard_set_text(id_text)

    # Send "Ctrl+v" to paste
    SendKeys('^v')()

    # Sleep to keep from being too fast
    time.sleep(0.03)
Beispiel #6
0
def browser_url_save():
    """
    Read URL from a browser's location bar, save as a `.url` file.

    :return: None.
    """
    #
    send_keys_to_copy_location_bar_text()

    # Get the url from clipboard
    text = clipboard_get_text()

    # If is not Python 2
    if not _IS_PY2:
        # Convert text to Unicode
        text = text.decode('gbk')

    # Move focus off the location bar
    SendKeys('{LAlt}')()

    # Create ".url" file
    create_url_file(url=text, output_dir=r'D:\SoftwareData\URL')
Beispiel #7
0
def open_parallel_dir(base_dir, create=False):
    """
    Open parallel directory of the current directory in Windows Explorer.
    E.g. jump from "D:\Study\Dev\Lang\Python" to "D:\Software\Dev\Lang\Python".

    :param base_dir: the base directory of the destination directory.

    :param create: create if the destination directory is not existing.

    :return: None.
    """
    # Send "Ctrl+l" to move focus to location bar
    SendKeys(r'<^l')()

    # Sleep to keep from being too fast
    time.sleep(0.05)

    # Send "Ctrl+c" to copy the current directory path
    SendKeys(r'<^c')()

    # Sleep to keep from being too fast
    time.sleep(0.05)

    # Get the current directory path from clipboard
    dir_path_in_bar = clipboard_get_text()

    # If current directory is empty
    if not dir_path_in_bar:
        # Exit
        return

    # If is not Python 2
    if not _IS_PY2:
        # Convert to Unicode
        dir_path_in_bar = dir_path_in_bar.decode('gbk')

    # Remove the "drive" part from the directory path.
    # E.g. "D:\Study" becomes "\Study"
    _, dir_path = splitdrive(dir_path_in_bar)

    # Print message
    print('Origin: {}'.format(dir_path))

    # Strip slashes
    dir_path = dir_path.strip('\\/')

    # Whether the current directory path starts with any of the prefixes below
    start_with_prefix = False

    # Start to find a matching prefix
    for prefix in [
            'Study',
            'Software',
            'SoftwareData',
            'All\\Software2\\SoftwareBig',
            'All\\Software2\\SoftwareSmall',
    ]:
        # If the current directory path starts with the prefix
        if (dir_path + '\\').startswith(prefix + '\\'):
            # Set the boolean
            start_with_prefix = True

            # Remove the prefix
            rel_path = dir_path[len(prefix):]

            # Left-strip slashes
            rel_path = rel_path.lstrip('\\/')

            # Create a new directory path
            dir_path_new = os.path.join(base_dir, rel_path)

            # Replace backslash with forward slash
            dir_path_new = dir_path_new.replace('\\', '/')

            # If to create a destination path if it is not existing
            if create:
                # If the destination path is not existing
                if not os.path.isdir(dir_path_new):
                    # Create destination path
                    os.makedirs(dir_path_new)

                # Message
                print('Open: {}'.format(dir_path_new))

                # Open the destination path
                webbrowser.open(dir_path_new)

                # Exit
                return
            # If not to create a destination path if it is not existing
            else:
                # Start to find the closest upper directory
                while True:
                    # If the destination path is existing
                    if os.path.isdir(dir_path_new):
                        # Message
                        print('Open: {}'.format(dir_path_new))

                        # Open the destination path
                        webbrowser.open(dir_path_new)

                        # Exit
                        return
                    # If the destination path is not existing
                    else:
                        # Get the parent directory
                        dir_path_new = os.path.dirname(dir_path_new)

                        # Remove the "drive" part
                        path_part = os.path.splitdrive(dir_path_new)[1]

                        # If the parent directory is partition root
                        if path_part in ('', '/', '\\'):
                            # Message
                            print('Ignore: {}'.format(dir_path_in_bar))

                            # Exit
                            return
                        else:
                            # Try the parent directory in the next loop
                            continue

    # If the current directory path not starts with the any of the prefixes
    if not start_with_prefix:
        # Message
        print('Open: {}'.format(base_dir))

        # Open the base destination directory
        webbrowser.open(base_dir)

        # Exit
        return
Beispiel #8
0
def send_keys_to_copy():
    # Send "Ctrl+c" to copy the URL in location bar
    SendKeys(r'<^c')()

    # Sleep to keep from being too fast
    time.sleep(0.05)
Beispiel #9
0
#where user = '******'
""")),
    ('::{ESC}8',
     partial(
         paste_text, r"""SELECT `AUTO_INCREMENT`
FROM  INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '_DB_'
AND   TABLE_NAME   = '_TABLE_';
""")),
    ('::{ESC}9',
     partial(paste_text, r"""ALTER TABLE _TABLE_ AUTO_INCREMENT = 1;""")),

    # ----- F1 -----
    # Reserve: ^F1 -> Sublime Text `toggle comment`
    ('F1', 'https://www.google.com/'),
    ('+F1', SendKeys('^c'), Sleep(0.02), clipboard_to_lowercase,
     SendKeys('^v')),
    ('#+F1', 'C:/'),
    ('!F1', partial(open_parallel_dir, base_dir=r'D:/Software')),
    ('#!F1', partial(open_parallel_dir, base_dir=r'D:/Software', create=True)),

    # ----- F2 -----
    # Reserve: ^F2 -> Goto paired bracket (Sublime Text)
    ('F2', 'http://global.bing.com/?setmkt=en-us&setlang=en-us'),
    ('#+F2', 'D:/'),
    ('+F2', SendKeys('^c'), Sleep(0.02), clipboard_to_uppercase,
     SendKeys('^v')),
    ('!F2',
     partial(open_parallel_dir, base_dir=r'F:/all/Software2/SoftwareSmall')),
    ('#!F2',
     partial(open_parallel_dir,