Skip to content

Instantly share code, notes, and snippets.

@rcj4747
Last active August 7, 2018 15:32
Show Gist options
  • Save rcj4747/2c4200117d219d87993d6fa08f661d3d to your computer and use it in GitHub Desktop.
Save rcj4747/2c4200117d219d87993d6fa08f661d3d to your computer and use it in GitHub Desktop.
Watch the clipboard for URLs and open them in the browser (on Linux)
#!/usr/bin/env python3
"""
Watch the clipboard for URLs and open them in the browser (on Linux)
"""
import subprocess
import time
import pyperclip # Requires xclip to be apt installed
PRIOR = ''
while True:
# xsel/xclip functions support the 'primary' argument
PASTE = pyperclip.determine_clipboard()[1]
# Toggle the commented lines below to use primary/secondary clipboards
CURRENT = PASTE(primary=True).strip().split()[0]
# CURRENT = PASTE(primary=False).strip()
if not PRIOR:
PRIOR = CURRENT
if PRIOR != CURRENT:
print(CURRENT)
PRIOR = CURRENT
if CURRENT.startswith('http'):
subprocess.call(["xdg-open", CURRENT],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment