def _screen_index(screen_name): """ Convert a screen name like consumer1 into its window index for AppleScript. We have a mapping of name->id but AppleScript seems to index only by window index. """ global ids_to_screen_names if not ids_to_screen_names: _refresh_ids() num_windows = int(applescripts.run_script(applescripts.GET_NUM_WINDOWS)) for i in xrange(1, num_windows + 1): id = applescripts.run_script(applescripts.GET_WINDOW_ID_FROM_INDEX % {'window': i}).strip() if ids_to_screen_names.get(id) == screen_name: return i raise Exception("Screen name %s not found" % screen_name)
def get_active_tab(screen): info = applescripts.run_script(applescripts.GET_ACTIVE_TAB % { 'window': _screen_index(screen), }) index, url = info.strip().split(' ', 1) url, title = url.split(' ', 1) if ' ' in url else (url, url) return {'index': int(index), 'url': url, 'title': title}
def _refresh_ids(): "Track the ids of all the open browser windows." global ids_to_screen_names ids_to_screen_names = {} ids_and_bounds = applescripts.run_script(applescripts.GET_WINDOW_IDS_AND_BOUNDS) ids_and_bounds = ids_and_bounds.strip().split('\n') ids_and_bounds = [line.strip().split(' ') for line in ids_and_bounds] # sort by distance from top left, with more weight on y comp = lambda line: int(line[1]) ** 2 + int(line[2]) ** 3 for i, line in enumerate(sorted(ids_and_bounds, key=comp)): screen_name = config.screen_name_prefix + str(i + 1) ids_to_screen_names[line[0]] = screen_name
def get_tab_info(screen): "Return a dict mapping tab index to tab url. Indices start at 1 due to Chrome's AppleScript interface." tab_lines = applescripts.run_script(applescripts.GET_TAB_INFO % { 'window': _screen_index(screen), }) tab_lines = tab_lines.strip().split('\n') # Convert space-delimited urls and titles result = {} for index, line in enumerate(tab_lines): url, title = line.split(' ', 1) if ' ' in line else (line, line) result[index + 1] = {'url': url, 'title': title} return result
def zoom_in(screen): applescripts.run_script(applescripts.ZOOM_IN_CURRENT_WINDOW % { 'window': _screen_index(screen), })
def bring_window_to_front(screen): applescripts.run_script(applescripts.BRING_WINDOW_TO_FRONT % { 'window': _screen_index(screen), })
def close_tab(screen): applescripts.run_script(applescripts.CLOSE_TAB % { 'window': _screen_index(screen), })
def reload_tab(screen): applescripts.run_script(applescripts.RELOAD_TAB % { 'window': _screen_index(screen), })
def activate_tab(screen, index): applescripts.run_script(applescripts.ACTIVATE_TAB % { 'window': _screen_index(screen), 'tab': index, })
def new_tab(screen, url): applescripts.run_script(applescripts.NEW_TAB % { 'window': _screen_index(screen), 'url': url, })
def presentation_mode(screen, toggle): applescripts.run_script(applescripts.PRESENTATION_MODE % { 'window': _screen_index(screen), 'enter_or_exit': 'enter' if toggle else 'exit', })
def restart_chrome(screen): applescripts.run_script(applescripts.RESTART_CHROME)
def inject_nyanwin(screen, lighter_host): applescripts.run_script(applescripts.INJECT_NYANWIN % { 'window': _screen_index(screen), 'nyanwinDoneUrl': 'http://' + lighter_host + ':5000/nyanwin_done?screen=' + screen, })
def execute_script(screen, script, by_url=False): applescripts.run_script(applescripts.EXECUTE_SCRIPT % { 'window': _screen_index(screen), 'script': script.replace('"', '\\"').replace('\n',''), })