Detect when a specific URL is copied to the clipboard

Viewed 97

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.

2 Answers

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!')
Related