Reputation: 95
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.
Upvotes: 4
Views: 3346
Reputation: 482
Maybe you can use keyboard module to monitor hotkeys, then you can open CMD console ,enter command python yourcode.py
, or change the file name to yourcode.pyw
which can run in the background process without windows, double click the code file it will be useful.
keyboard project:https://github.com/boppreh/keyboard
Be careful not to use list as a variable name, because it is a keyword in Python language.
The following code is valid on my computer(win10, python3.7):
import pyautogui as pya
import pyperclip # handy cross-platform clipboard text handler
import time
import keyboard
lst = []
def copy_clipboard():
pyperclip.copy("") # <- This prevents last copy replacing current copy of null.
pya.hotkey('ctrl', 'c')
time.sleep(.1) # ctrl-c is usually very fast but your program may execute faster
return pyperclip.paste()
def double_click_copy():
# double clicks on a position of the cursor
pya.doubleClick(pya.position())
var = copy_clipboard()
lst.append(var)
print(lst)
keyboard.add_hotkey('ctrl+f9', double_click_copy)
keyboard.wait()
Upvotes: 2
Reputation: 7313
You need some wait to switch to your actual window. You have already imported time
. Use .sleep
before the clicking process:
time.sleep(3)
So, your code will be like this:
...
def copy_clipboard():
...
time.sleep(3)
pya.doubleClick(pya.position())
...
Upvotes: 0