def _pasteText( self, textDict ): """ Puts the given textObject into the clipboard, then simulates a paste keystroke in the application. If all goes well, the text will be inserted into the application, and this function returns True. If the attempt fails, returns False. """ # Create the function which will be used for callback def getPendingData( formatCode ): try: if formatCode == ContextUtils.CF_CLIPBOARD_VIEWER_IGNORE: return "HumanizedEnsoTextSelectionContext\0" else: return self._renderClipboardFormat( textDict, formatCode ) except Exception: import traceback logging.error( "Traceback in getPendingData():\n%s" % traceback.format_exc() ) raise # Give the above function to clipboard backend, along with the # list of formats in which we can support pasting ClipboardBackend.prepareForPasting( getPendingData, SUPPORTED_FORMATS ) # then type the paste command key, which will cause the app to # draw the data out of getPendingData. self.simulatePasteKeystroke() ClipboardBackend.waitForPaste( STANDARD_WAIT_TIME ) success = ClipboardBackend.finalizePasting() return success
def replaceSelection(self, textDict): """ Replace the selected text with the given text by first cutting and discarding the selected text, then pasting in the new text. Returns a boolean telling whether replacement succeeded. """ text = textDict.get('text') if text: win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText(text) win32clipboard.CloseClipboard() # then type the paste command key, which will cause the app to # draw the data out of getPendingData. self.simulatePasteKeystroke() ClipboardBackend.waitForPaste(STANDARD_WAIT_TIME) ClipboardBackend.finalizePasting() return True else: return DefaultTextSelection.replaceSelection(self, textDict)