Search code examples
pythonclipboard

Run a python code when copying text with specific keyword


I am new to Python, trying to write a code to parse text into a certain format and copy it to the clipboard, which I have achieved. Now I am looking for a way to automatically run the python code when I copy the text to clipboard and if the text has a certain keyword. so that as soon as my text content is copied, it will be parsed and readily available to be pasted on a text editor.


Solution

  • import clipboard
    import asyncio
    
    
    # Exemple function.
    async def your_function():
        print("Running...")
    
    
    async def wait4update(value):
        while True:
            if clipboard.paste() != value : # If the clipboard changed.
                return
    
    async def main():
        value = clipboard.paste() # Set the default value.
        while True :
            update = asyncio.create_task(wait4update(value))
            await update
            value = clipboard.paste() # Change the value.       
            asyncio.create_task(your_function()) #Start your function.
    
    asyncio.run(main())