-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor-clipboard.py
More file actions
executable file
·40 lines (33 loc) · 1.53 KB
/
monitor-clipboard.py
File metadata and controls
executable file
·40 lines (33 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Monitor the X clipboard. When it changes, spit out whatever the new contents of
the clipboard are to stdout, from which it can, if necessary, be redirected.
This script relies on the external program XCLIP (try sudo apt-get install xclip
from within Ubuntu or other Debian-based distributions). It is really just a
quick hack, but if you want to, you can use it; it's licensed under the GNU GPL
(either version 3 or, at your option, any later version); see the file
LICENSE.md for details.
This script is copyright 2017-20 by Patrick Mooney. It is licensed under the GNU
GPL, either version 3 or (at your option) any later version. See the file
LICENSE.md for details.
"""
import time
import subprocess
import sys
if __name__ == "__main__":
print("\n\nMonitoring clipboard, press Ctrl-C to quit ...", file=sys.stderr)
try:
last_clipboard = ''
while True:
try:
the_contents = subprocess.run('xclip -sel clip -o', shell=True, capture_output=True).stdout.decode()
if the_contents != last_clipboard:
last_clipboard = the_contents
print(the_contents)
time.sleep(0.2)
except (subprocess.CalledProcessError, UnicodeDecodeError):
if last_clipboard:
print("WARNING! Unable to read and covert clipboard")
last_clipboard = None
except KeyboardInterrupt:
print("\nCaught Ctrl-C, quitting ...\n", file=sys.stderr)