def select_string(include_quotes): mouse("left/3").execute() clipboard = Clipboard() saved_text = clipboard.get_system_text() clipboard.set_system_text('') left_counter = 0 while left_counter < 50: key("s-left, c-c/3").execute() left_counter += 1 if clipboard.get_system_text().startswith("\""): break key("left").execute() move_right = left_counter if not include_quotes: move_right -= 1 key("right").execute() key("s-right:%s" % move_right).execute() right_counter = 0 while right_counter < 50: key("s-right, c-c/3").execute() right_counter += 1 if clipboard.get_system_text().endswith("\""): break if not include_quotes: key("s-left").execute() clipboard.set_text(saved_text) clipboard.copy_to_system()
def test_copy_to_system(self): # Test with format_unicode. text1 = u"unicode text" c = Clipboard(contents={format_unicode: text1}) c.copy_to_system(clear=True) self.assertEqual(Clipboard.get_system_text(), text1) # Test with format_text. Text string used deliberately here; # get_system_text() should returns those. text2 = u"text" c = Clipboard(contents={format_text: text2}) c.copy_to_system(clear=True) self.assertEqual(Clipboard.get_system_text(), text2) # Test with text. text3 = u"testing" c = Clipboard(text=text3) c.copy_to_system(clear=True) self.assertEqual(Clipboard.get_system_text(), text3) # Test with an empty Clipboard instance. c = Clipboard() c.copy_to_system(clear=False) self.assertEqual(Clipboard.get_system_text(), text3) c.copy_to_system(clear=True) self.assertEqual(Clipboard.get_system_text(), u"")
def get_selected_text(): clipboard = Clipboard() previous = clipboard.get_system_text() clipboard.set_system_text("") Key("c-c/3").execute() selected = clipboard.get_system_text() clipboard.set_text(previous) clipboard.copy_to_system() return selected
def test_backwards_compatibility(self): # The multi-platform class should be backwards compatible with the # Windows-only Clipboard class, at least for the constructor. text = u"unicode text" c = Clipboard(contents={13: text}) c.copy_to_system() self.assertEqual(Clipboard.get_system_text(), text) # Test with the CF_TEXT format (1) text = "text" c = Clipboard(contents={1: text}) c.copy_to_system() self.assertEqual(Clipboard.get_system_text(), text)
def test_copy_from_system(self): text = "testing" Clipboard.set_system_text(text) c = Clipboard() # Test the method with clear=False (default). c.copy_from_system(clear=False) self.assertEqual(c.text, text) self.assertEqual(Clipboard.get_system_text(), text) # Test again with clear=True. c = Clipboard() c.copy_from_system(clear=True) self.assertEqual(c.text, text) self.assertEqual(Clipboard.get_system_text(), "") # Test formats=format_unicode. # Set the system clipboard before testing. text1 = u"unicode text" c1 = Clipboard(contents={format_unicode: text1}) c1.copy_to_system() c2 = Clipboard() c2.copy_from_system(formats=format_unicode) self.assertTrue(c2.has_format(format_unicode)) self.assertEqual(c2.get_format(format_unicode), text1) # Test formats=format_text. # Set the system clipboard before testing. text2 = b"text" c1 = Clipboard(contents={format_text: text2}) c1.copy_to_system() c2 = Clipboard() c2.copy_from_system(formats=format_text) self.assertTrue(c2.has_format(format_text)) self.assertEqual(c2.get_format(format_text), text2) # Test formats=(format_unicode, format_text). # Set the system clipboard before testing. Use the same string for # both formats so the test will work on all platforms. c1 = Clipboard(contents={ format_text: b"text", format_unicode: u"text" }) c1.copy_to_system() c2 = Clipboard() c2.copy_from_system(formats=(format_unicode, format_text)) self.assertTrue(c2.has_format(format_unicode)) self.assertEqual(c2.get_format(format_unicode), u"text") self.assertTrue(c2.has_format(format_text)) self.assertEqual(c2.get_format(format_text), b"text")
def test_copy_from_system(self): text = "testing" Clipboard.set_system_text(text) c = Clipboard() # Test the method with clear=False (default) c.copy_from_system(clear=False) self.assertEqual(c.text, text) self.assertEqual(Clipboard.get_system_text(), text) # Test again with clear=True c = Clipboard() c.copy_from_system(clear=True) self.assertEqual(c.text, text) self.assertEqual(Clipboard.get_system_text(), "")
def stoosh_keep_clipboard(nnavi500, nexus): if nnavi500 == 1: Key("c-c").execute() else: max_tries = 20 cb = Clipboard(from_system=True) Key("c-c").execute() key = str(nnavi500) for i in range(0, max_tries): failure = False try: # time for keypress to execute time.sleep( settings.SETTINGS["miscellaneous"]["keypress_wait"] / 1000.) nexus.clip[key] = Clipboard.get_system_text() utilities.save_json_file( nexus.clip, settings.SETTINGS["paths"]["SAVED_CLIPBOARD_PATH"]) except Exception: failure = True utilities.simple_log() if not failure: break cb.copy_to_system()
def test_non_ascii(self): text = u""" ૱ ꠸ ┯ ┰ ┱ ┲ ❗ ► ◄ Ă ă 0 1 2 3 4 5 6 7 8 9 Ǖ ǖ Ꞁ ¤ Ð ¢ ℥ Ω ℧ K ℶ ℷ ℸ ⅇ ⅊ ⚌ ⚍ ⚎ ⚏ ⚭ ⚮ ⌀ ⏑ ⏒ ⏓ ⏔ ⏕ ⏖ ⏗ ⏘ ⏙ ⏠ ⏡ ⏦ ᶀ ᶁ ᶂ ᶃ ᶄ ᶆ ᶇ ᶈ ᶉ ᶊ ᶋ ᶌ ᶍ ᶎ ᶏ ᶐ ᶑ ᶒ ᶓ ᶔ ᶕ ᶖ ᶗ ᶘ ᶙ ᶚ ᶸ ᵯ ᵰ ᵴ ᵶ ᵹ ᵼ ᵽ ᵾ ᵿ """ Clipboard.set_system_text(text) self.assertEqual(Clipboard.get_system_text(), text)
def read_selected(same_is_okay=False): time.sleep(SETTINGS["keypress_wait"]) cb = Clipboard(from_system=True) temporary = None prior_content = None try: prior_content = Clipboard.get_system_text() Clipboard.set_system_text("") Key("c-c").execute() time.sleep(SETTINGS["keypress_wait"]) temporary = Clipboard.get_system_text() cb.copy_to_system() except Exception: return None if prior_content == temporary and not same_is_okay: return None return temporary
def _select_and_cut_text(wordCount): """Selects wordCount number of words to the left of the cursor and cuts them out of the text. Returns the text from the system clip board. """ clipboard = Clipboard() clipboard.set_system_text('') Key('cs-left/3:%s/10, c-x/10' % wordCount).execute() return clipboard.get_system_text()
def test_clear_clipboard(self): # Put something on the system clipboard. Clipboard.set_system_text("something") # Clear it. Clipboard.clear_clipboard() # Then test that it has been cleared. self.assertEqual(Clipboard.get_system_text(), "")
def test_get_set_system_text(self): # Test with an empty system clipboard. Clipboard.clear_clipboard() self.assertEqual(Clipboard.get_system_text(), u"") # Test setting a Unicode string on the system clipboard. text1 = u"Unicode text" Clipboard.set_system_text(text1) self.assertEqual(Clipboard.get_system_text(), text1) # Test setting a binary string on the system clipboard. # get_system_text() should return the equivalent Unicode string, as # format_unicode is preferred. Clipboard.set_system_text(b"text") self.assertEqual(Clipboard.get_system_text(), u"text") # Test that setting the text using None clears the clipboard. Clipboard.set_system_text(None) self.assertEqual(Clipboard.get_system_text(), u"")
def get_system_text(cls): # Get the server's clipboard content if possible and update this # system's clipboard. try: server_text = aenea.communications.server.paste() DragonflyClipboard.set_system_text(server_text) return server_text except ProtocolError as e: print("ProtocolError caught when calling server.paste(): %s" % e) print("Only getting local clipboard content.") return DragonflyClipboard.get_system_text()
def selection_to_bib(ref_type, bib_path): Key("c-c/20").execute() cb = Clipboard.get_system_text() if ref_type == "book": ref = book_citation_generator.citation_from_name(cb) elif ref_type == "paper": ref = bibtexer.bib_from_title(cb) elif ref_type == "link": ref = bibtexer.bibtex_from_link(cb) with codecs.open(bib_path, encoding="utf-8", mode="a") as f: f.write(ref) print("Reference added:\n" + ref) Clipboard.set_system_text(bibtexer.get_tag(ref))
def read_selected(same_is_okay=False): '''Returns a tuple: (0, "text from system") - indicates success (1, None) - indicates no change (2, None) - indicates clipboard error ''' time.sleep(0.05) cb = Clipboard(from_system=True) temporary = None prior_content = None try: prior_content = Clipboard.get_system_text() Clipboard.set_system_text("") Key("c-c").execute() time.sleep(0.05) temporary = Clipboard.get_system_text() cb.copy_to_system() except Exception: return 2, None if prior_content == temporary and not same_is_okay: return 1, None return 0, temporary
def _select_and_cut_text(wordCount): """Selects wordCount number of words to the left of the cursor and cuts them out of the text. Returns the text from the system clip board. """ clipboard = Clipboard() clipboard.set_system_text('') try: # Try selecting n number of words. Key('ctrl:down, shift:down').execute() Key('left:%s' % wordCount).execute() Key('shift:up').execute() finally: # It is important to make sure that the buttons are released. # Otherwise you get stuck in an unpleasant situation. Key('shift:up, ctrl:up').execute() Pause("10").execute() Key('c-x').execute() # Cut out the selected words. Pause("20").execute() return clipboard.get_system_text()
def test_system_text_methods(self): text = "testing testing" Clipboard.set_system_text(text) self.assertEqual(Clipboard.get_system_text(), text)
def _get_clipboard_text(): """Returns the text contents of the system clip board.""" clipboard = Clipboard() return clipboard.get_system_text()
def test_copy_to_system(self): text = "testing" c = Clipboard(text=text) c.copy_to_system() self.assertEqual(Clipboard.get_system_text(), text)