How can I detect when the text copied to the clipboard has certain text? I need to run a script whenever I copy a specific URL to the clipboard.
How can I detect when the text copied to the clipboard has certain text? I need to run a script whenever I copy a specific URL to the clipboard.
Using PyQT5 you can register an event handler for when items are copied to the clipboard. Here is a good starting point:
How to use QClipboard.dataChanged signal to change the text in clipboard?
You can do this via the win32Clipboard library in python. First, we need to import it:
import win32clipboard
Next we have to open the clipboard and empty it:
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
Now we'll put in your url:
url = win32clipboard.SetClipboardText('https://www.youtube.com/watch?
v=dQw4w9WgXcQ')
Now we'll actually get the data:
data = win32clipboard.GetClipboardData()
This is where the fun part comes in; we're going to check if the text placed is the one you want and then you run the script:
while True:
if data == url:
print('it happened!')
We can check it worked by copying the link.
This is the full code:
import win32clipboard
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
url = win32clipboard.SetClipboardText('https://www.youtube.com/watch?
v=dQw4w9WgXcQ')
data = win32clipboard.GetClipboardData()
while True:
if data == url:
print('it happened!')