I'm a Python newbie trying to work out how to have Python copy whatever text I have highlighted when the program is called.
I've looked at the solution posted in this thread: Copy highlighted text to clipboard, then use the clipboard to append it to a list
Everything there makes sense in theory but the problem is that when I run the program it seems that the 'pya.doubleClick(pya.position())' command gets rid of my highlight! If I leave my cursor hovering over the text then the program does successfully highlight a given word - but I need to be able to copy entire phrases!
What I want to achieve is to be able highlight any text on the screen (including whole phrases and not just particular words) and then run the program with the result that the highlighted text is fed through the program.
I had anticipated this would involve some kind of automation of the 'ctrl' + 'c' function while the text was highlighted... but I can't figure out to actually get this to work.
I'm using Python 3.7.4 on Windows 10.
This is the code which was presented as the solution in the thread that I linked above:
import pyautogui as pya
import pyperclip # handy cross-platform clipboard text handler
import time
def copy_clipboard():
pyperclip.copy("") # <- This prevents last copy replacing current copy of null.
pya.hotkey('ctrl', 'c')
time.sleep(.01) # ctrl-c is usually very fast but your program may execute faster
return pyperclip.paste()
# double clicks on a position of the cursor
pya.doubleClick(pya.position())
list = []
var = copy_clipboard()
list.append(var)
print(list)
Maybe my real issue is that I don't know how to get the program to run without getting rid of the highlight on whatever text is highlighted at the time. At the moment, in order to call the program I'm using the very clunky method of making a shortcut to my program and then specifying a hotkey to that shortcut in the 'properties' tab to that shortcut.