def selected_text() -> str: with clip.capture() as s: actions.edit.copy() try: return s.get() except clip.NoChange: return ""
def set_selection(m): with clip.capture() as sel: press("cmd-c") print("sel:", sel.get()) value = sel.get() if value not in CLIPBOARD: CLIPBOARD.append(value) clip.set(value)
def fix_format(m): press("cmd-right") press("shift-cmd-left") s = "" with clip.capture() as _s: press("cmd-c", wait=2000) s = str(_s.get()) utils.insert(re.sub(r' +', ' ', s))
def learn_jargon(m): with clip.capture() as s: press("cmd-c", wait=2000) meaning = s.get() # type: str if meaning: meaning = meaning.strip() key = " ".join(parse_words(m)) app.notify(f"learned {key}={meaning}") add_jargon(key, meaning)
def correctText(): with clip.capture() as s: press('cmd-c') inputText = s.get() # print('!!!!') # print(inputText) # print('!!!!') correctText = formatText(inputText) clip.set(correctText) press('cmd-v')
def _get_line(direction, back, more): press(direction, wait=2000) press(back, wait=2000) press("shift-" + more, wait=2000) with clip.capture() as s: press("cmd-c", wait=2000) press(back, wait=2000) text = s.get() return text
def add_alternative(m): try: with clip.capture() as s: press("cmd-c") existing = s.get() except clip.NoChange: return paste_text(f"({existing} | )") press("left") text(m)
def react(m): emoji = m.emoji[0] edit.select_all() with clip.capture() as s: edit.copy() insert(f"+{emoji}\n") try: insert(s.get()) except clip.NoChange: pass
def copy_selection(m): with clip.capture() as sel: press('cmd-c') if len(m._words) > 1: key = ' '.join(parse_words(m)) value = sel.get() keymap['paste %s' % key] = value ctx.keymap(keymap) ctx.reload() else: clip.set(sel.get())
def copy_selection(m): if len(m._words) > 1: with clip.capture() as sel: press('cmd-c') key = ' '.join(parse_words(m)).lower() value = sel.get() # print('saving as', key) # is it taking the first word as well??? keymap['paste %s' % key] = value keymap['free %s' % key] = seti(value) ctx.keymap(keymap) ctx.reload() tell_hammerspoon_osa(f"showAlert('copied to {key}')")
def selected_text() -> str: # try: # text = ui.focused_element().AXSelectedText # if text: # return text # except Exception: # pass try: with clip.capture() as s: actions.edit.copy() return s.get() except clip.NoChange: return clip.get()
def stop_selection(cursor_position): assert cursor_position in ("left", "right") with clip.capture() as s: actions.edit.extend_right() time.sleep(0.25) actions.edit.copy() current_highlight = s.get() actions.edit.extend_left() if len(current_highlight) > 1: if cursor_position == "left": actions.edit.left() elif cursor_position == "right": actions.edit.right()
def select_text_to_right_of_cursor(m, cursorKey, clipboardSelectKey="shift-end"): key = join_words(parse_words(m)).lower() with clip.capture() as clipboardText: press(clipboardSelectKey, wait=20000) press("cmd-c", wait=20000) press("left", wait=20000) searchText = clipboardText.get().lower() result = searchText.find(key) if result == -1: return False # cursor over to the found key text and select the matching text for i in range(0, result): press(cursorKey, wait=0) for i in range(0, len(key)): press("shift-right", wait=0) return True
def select_text_to_left_of_cursor(m, cursorKey, clipboardSelectKey='shift-home'): key = join_words(parse_words(m)).lower() with clip.capture() as clipboardText: press(clipboardSelectKey, wait=20000) press('cmd-c', wait=20000) press('right', wait=20000) searchText = clipboardText.get().lower() result = searchText.rfind(key) if result == -1: return False # cursor over to the found key text and select the matching text for i in range(result, len(searchText) - len(key)): press(cursorKey, wait=0) for i in range(0, len(key)): press('shift-left', wait=0) return True
def word_neck(word_index, valid_characters=alphanumeric): with clip.revert(): stop_selection("right") with clip.capture() as s: actions.edit.extend_line_end() time.sleep(0.25) actions.edit.copy() actions.edit.left() time.sleep(0.25) text_right = s.get().lower() is_word = [character in valid_characters for character in text_right] word_count = 1 i = 0 while i < (len(is_word) - 1) and not is_word[i]: i += 1 # print("a start", i) while i < (len(is_word) - 1) and word_count < word_index: # print(i, is_word[i], word_count, word_index) if not is_word[i] and is_word[i + 1]: word_count += 1 i += 1 # warning: this is a hack, sorry # print("i", i) if i == 1 and is_word[0]: i = 0 start_position = i # print(text_right[start_position:]) while i < len(is_word) and is_word[i]: i += 1 end_position = i # print(start_position, end_position) # cursor over to the found word for i in range(0, start_position): actions.edit.right() # now select the word for i in range(0, end_position - start_position): actions.edit.extend_right()
def extract_formatter_and_words(m): fmt = [] # noinspection PyProtectedMember for w in m._words: # noinspection PyUnresolvedReferences if isinstance(w, Word) and parse_word(w.word) != "over": # noinspection PyUnresolvedReferences fmt.append(w.word) words = [a for w in parse_words(m) for a in normalize(w).split()] # print(words) if not words: with clip.capture() as s: press("cmd-c", wait=2000) try: words = normalize(s.get()).split() except clip.NoChange: words = [] if not words: words = [""] return fmt, words
def stop_selection(cursor_position): """ determine if anything is currently selected. If it is selected, deselect it, if it is not selected, leave the cursor in the position we found it """ assert cursor_position in ("left", "right") try: with clip.capture() as s: actions.edit.extend_right() time.sleep(0.25) actions.edit.copy() current_highlight = s.get() actions.edit.extend_left() except clip.NoChange: current_highlight = "" if len(current_highlight) > 1: if cursor_position == "left": actions.edit.left() elif cursor_position == "right": actions.edit.right()
def word_prev(word_index, valid_characters=alphanumeric): with clip.revert(): stop_selection("left") with clip.capture() as s: actions.edit.extend_line_start() time.sleep(0.25) actions.edit.copy() actions.edit.right() time.sleep(0.25) text_right = s.get().lower() text_right = list(reversed(text_right)) is_word = [character in valid_characters for character in text_right] word_count = 1 i = 0 while i < (len(is_word) - 1) and not is_word[i]: i += 1 while i < (len(is_word) - 1) and word_count < word_index: # print(i, is_word[i], word_count, word_index) if not is_word[i] and is_word[i + 1]: word_count += 1 i += 1 start_position = i # print(text_right[start_position:]) while i < len(is_word) and is_word[i]: i += 1 end_position = i # print(start_position, end_position, text_right[start_position:end_position]) # cursor over to the found word for i in range(0, start_position): actions.edit.left() # now select the word for i in range(0, end_position - start_position): actions.edit.extend_left()
def learn_selection(_): with clip.capture() as s: press("cmd-c", wait=2000) words = s.get().split() add_vocab(words) tell_hammerspoon_osa(f"showQuickMessage('Learned: {words}')")
def learn_selection(_): with clip.capture() as s: press("cmd-c", wait=2000) words = s.get().split() add_vocab(words) print("Learned " + ",".join(words))
def get_selection(): with clip.capture() as s: press("cmd-c", wait=0) return s.get()
def cut_safe() -> None: """Like `edit.cut` but waits for the clipboard to change.""" with clip.capture() as c: edit.cut() return c.get()
def get_highlighted() -> None: """Get the currently highlighted text, without altering clipboard.""" with clip.capture() as c: edit.copy() return c.get()
def learn_selection(_): with clip.capture() as s: press("cmd-c", wait=2000) words = s.get().split() add_vocab(words)
def file_manager_terminal_here(): actions.key("ctrl-l") with clip.capture() as path: actions.edit.copy() ui.launch(path="gnome-terminal", args=[f"--working-directory={path.get()}"])
def copy_safe() -> None: """Like `edit.copy` but waits for the clipboard to change.""" # FIXME: This restores the clipboard with clip.capture() as c: edit.copy() return c.get()