I want to create a simple Python app which would interact with currently selected text and do some things with it when user presses a keyboard shortcut. The obvious way to do this would be to copy the text to clipboard using pyautogui.hotkey("ctrl", "c")
and then interact with it. Problem is, this method overwrites whatever the user currently has in their clipboard.
The only workaround I've been able to think of would be something like:
#make a backup of current clipboard contents
tmpClipboard = pyperclip.paste()
#copy selected text and store it
pyautogui.hotkey("ctrl", "c")
selectedText = pyperclip.paste()
#copy old clipboard contents back inside
pyperclip.copy(tmpClipboard)
#process selected text
doStuff(selectedText)
What would be a better way of handling this (as I'm not sure how well this would perform when user has, say, an image in their clipboard instead of plain text)?