0

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)?

Straightfw
  • 2,143
  • 5
  • 26
  • 39

1 Answers1

1

You can try to let the user define hotkey for interactions. I do not know your end application, therefore it depends on the user experience you want to achieve. If the user is an engineer, for example, I think he would prefer to be prompted about the default hotkey, and change it if he wants.

Regarding grabbing the selected text - it depends on your GUI binding - are you using Tk, Qt, etc..? For instance, Qt has a QTextCursor that you can use to obtain information about the cursor and user selection, and then grab the text without using the whole clipboard trick. This way you can also be sure you get the data you desire.

As for images, etc: if you still choose to go with clipboard, you can assert the type of data. However, it has really vast amount of possible values and it seems to be very cumbersome and inefficient.

Good luck!

mr_mo
  • 1,401
  • 6
  • 10
  • Thanks for the reply but the problem here is I don't want to make this dependent on the GUI library cause I don't want to have the app be able to get text from within itself but rather system-wide. Say you select text in Word or Notepad or your browser of choice, you use the shortcut and the Python app catches the selected text - that's the behaviour I want. – Straightfw Apr 02 '19 at 14:36
  • 1
    In that case, consider the assertions solutions. Interacting with other programs highly depends on the target OS, program, etc. For instance, textboxes in web browsers are very much problematic (since not all elements are really text boxes, domain composition, etc). For Web I would recommend developing an extension to Chrome or something like that, which in turn uses the Python code to do the computation. For Word, you would want `pywin32` or something like that. Please see other thread https://stackoverflow.com/questions/14288177/interact-with-other-programs-using-python – mr_mo Apr 02 '19 at 16:30
  • Oh my, in that case it sounds like the hacky copy-paste solution may be the fastest way to do this after all :) Thanks! – Straightfw Apr 05 '19 at 08:29