Пример #1
0
def make_screenshot(num, folder=''):
    all_apps = CG.CGWindowListCopyWindowInfo(CG.kCGWindowListOptionAll, CG.kCGNullWindowID)
    tables = []
    for app in all_apps:
        try:
            if "No Limit Hold'em" in app['kCGWindowName']:
                window_name = app['kCGWindowName'].split(' ')
                if window_name[2] == '-':
                    table_name = ''.join([*window_name[:2]])
                    blinds = [x.replace('$', '') for x in window_name[3].split('/')]
                else:
                    table_name = window_name[0]
                    blinds = [x.replace('$', '') for x in window_name[2].split('/')]
                table = {'Name': '-'.join([table_name, str(num), *blinds]) + '.bmp',
                         'ID': app['kCGWindowNumber']}
                tables.append(table)
        except KeyError:
            continue
    for table in tables:
        file_name = os.path.join(os.path.dirname(__file__), 'scr', folder, table['Name'])
        os.system('screencapture -l {} -o {}'.format(table['ID'], file_name))
    try:
        return file_name
    except UnboundLocalError:
        return ''
Пример #2
0
 def __init__(self, pid: int, timeout: float):
     """ Create a new instance from main application window of a process """
     super().__init__(pid, timeout)
     deadline = time.time() + timeout
     while True:
         if time.time() > deadline:
             raise WindowDarwinError("Main process window not found")
         options = CG.kCGWindowListOptionAll
         relativeToWindow = CG.kCGNullWindowID
         windows = CG.CGWindowListCopyWindowInfo(options, relativeToWindow)
         if windows is None:
             time.sleep(0.1)
             continue
         windows = [
             window for window in windows
             if window['kCGWindowOwnerPID'] == pid
         ]
         if len(windows) == 0:
             time.sleep(0.1)
             continue
         if windows[0]['kCGWindowBounds'] == {
                 'X': 0,
                 'Y': 0,
                 'Width': 0,
                 'Height': 0
         }:
             time.sleep(0.1)
             continue
         break
     self.window = windows[0]
Пример #3
0
def find_window(title):
    window_list = CG.CGWindowListCopyWindowInfo(
        CG.kCGWindowListOptionOnScreenOnly & CG.kCGWindowListExcludeDesktopElements,
        CG.kCGNullWindowID)

    target_window_list = list(filter(lambda w: w.get('kCGWindowName') is not None and w.get('kCGWindowName').find(title) >= 0, window_list))

    return target_window_list
Пример #4
0
def get_active_window_info():
    curr_pid = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationProcessIdentifier']
    options = CG.kCGWindowListOptionOnScreenOnly
    window_list = CG.CGWindowListCopyWindowInfo(options, CG.kCGNullWindowID)

    for window in window_list:
        # dump_window_info(window)
        # print(window)
        if curr_pid == window['kCGWindowOwnerPID'] and window.get('kCGWindowName') is not None:
            return window
Пример #5
0
    def get_window(self, obj_handle=None):
        filters = CG.kCGWindowListOptionOnScreenOnly | \
            CG.kCGWindowListExcludeDesktopElements * bool(obj_handle)

        obj_name = obj_handle if obj_handle else u'DesktopWindow Server'
        obj_name = \
            obj_name if type(obj_name) == unicode else obj_name.decode('utf-8')

        regex = MacUtils.convert_wildcard_to_regex(obj_name)

        win_list = CG.CGWindowListCopyWindowInfo(filters, CG.kCGNullWindowID)

        window = filter(
            lambda x: re.match(
                MacUtils.replace_inappropriate_symbols(regex),
                MacUtils.replace_inappropriate_symbols(
                    x.get('kCGWindowName', '')) + x.get(
                        'kCGWindowOwnerName', ''), re.IGNORECASE)
            if x.get('kCGWindowName', '') else False, win_list)

        window = window[0] if window else window
        if not window:
            obj_name = obj_name.encode(self._default_sys_encoding,
                                       errors='ignore')
            raise TooSaltyUISoupException('Can\'t find window "%s".' %
                                          obj_name)

        process_name = window['kCGWindowOwnerName']
        window_name = window['kCGWindowName']
        process_id = int(window['kCGWindowOwnerPID'])
        # Escape double quotes in window name.
        window_name = window_name.replace('"', '\\"')

        try:
            selector = \
                MacUtils.ApplescriptExecutor.get_apple_event_descriptor(
                    'window "%s"' % window_name,
                    process_name).applescript_specifier
        except TooSaltyUISoupException:
            selector = \
                MacUtils.ApplescriptExecutor.get_apple_event_descriptor(
                    'window 0',
                    process_name).applescript_specifier

        selector = \
            selector if type(selector) == unicode else selector.decode('utf-8')

        return MacElement(selector, 0, process_name, process_id)
Пример #6
0
    def get_visible_window_list(self):
        win_list = CG.CGWindowListCopyWindowInfo(
            CG.kCGWindowListOptionOnScreenOnly
            | CG.kCGWindowListExcludeDesktopElements, CG.kCGNullWindowID)

        win_names = \
            [w.get('kCGWindowName', '') + w.get('kCGWindowOwnerName', '') for
             w in win_list if w.get('kCGWindowName', '') and 0 not in
             [int(w.get('kCGWindowBounds', 0)['Height']),
              int(w.get('kCGWindowBounds', 0)['Width'])]]

        windows = list()
        for win_name in win_names:
            try:
                windows.append(self.get_window(win_name))
            except TooSaltyUISoupException:
                continue

        return windows
Пример #7
0
def get_window_id(name, owner=None, on_screen=True):
    """
    Gets the id of the window with the given name. Only valid in macOS.
    :param str name: the name of the window whose id we want to retrieve.
    :param str owner: the name of the window owner whose id we want to retrieve.
    :param bool on_screen: require the window to have the "on screen" flag with a `True` value.
    :rtype: list[int]
    :return: the id of the window or -1 if no window with the given name was found.
    """
    # check for mac OS
    if platform.system() != 'Darwin':
        raise ValueError('Not supported in non-macOS platforms!')

    # get list of windows, search for name or owner and state and return id
    wl = CG.CGWindowListCopyWindowInfo(CG.kCGWindowListOptionAll, CG.kCGNullWindowID)
    for window in wl:
        if CG_WINDOW_NAME in window and name == window[CG_WINDOW_NAME] and \
                (not on_screen or (CG_WINDOW_ON_SCREEN in window and window[CG_WINDOW_ON_SCREEN])):
            yield int(window[CG_WINDOW_NUMBER])
        elif owner is not None and CG_WINDOW_OWNER_NAME in window and owner == window[CG_WINDOW_OWNER_NAME] and \
                (not on_screen or (CG_WINDOW_ON_SCREEN in window and window[CG_WINDOW_ON_SCREEN])):
            yield int(window[CG_WINDOW_NUMBER])