-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClipboardMonitor.py
128 lines (105 loc) · 4 KB
/
ClipboardMonitor.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
Clipboard Watcher with Threading using Pyperclip
Purpose:
monitor the clipboard and perform changes or actions
Thanks to:
Pyperclip by Al Sweigart [email protected]
https://github.com/asweigart/pyperclip
Stackoverflow
http://stackoverflow.com/a/14687465/4222833
http://stackoverflow.com/users/1156006/thorsten-kranz
http://stackoverflow.com/users/1244729/kuanyui
"""
import time
import threading
import re
import pyperclip
import win32gui
import os
import webbrowser
### Todo make configurable
### Windows/Linux will be different
chrome = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
def clean_whitespaces(clipboard_content):
ReSearchFilter = r'\n';
ReSearch = r'^\s*(.*?)\s*$';
tmp_clipboard_content = clipboard_content
# Don't change content if it comes from these
if re.search(r'\sExcel$', getActiveWindowTitle()):
return False
if re.search(r'Sublime', getActiveWindowTitle()):
return False
### copied a Link
# if re.search(r'Special skype window', getActiveWindowTitle()) and re.search(r'^\s*http', clipboard_content):
# clipboard_content = re.sub(ReSearch, r'\1', clipboard_content)
# os.system('"' + chrome + '" %s ' % clipboard_content)
# return True
if 'TConversationForm' == getActiveWindowClass() and re.search(r'^\s*http', clipboard_content):
clipboard_content = re.sub(ReSearch, r'\1', clipboard_content)
os.system('"' + chrome + '" %s ' % clipboard_content)
return True
if not re.search(ReSearchFilter, clipboard_content) and re.search(ReSearch, clipboard_content):
clipboard_content = re.sub(ReSearch, r'\1', clipboard_content)
if tmp_clipboard_content == clipboard_content:
return False
setClipboard(clipboard_content)
return True
return False
def print_to_stdout(clipboard_content):
return
clipboard_content = str((clipboard_content).encode('utf-8').strip())
print "Window: '%s'" % (getActiveWindowTitle()).encode('utf-8').strip()
print "Print: '%s'" % clipboard_content
def getActiveWindowTitle():
try:
return win32gui.GetWindowText (win32gui.GetForegroundWindow())
except (RuntimeError, TypeError, NameError):
return ''
def getActiveWindowClass():
try:
return win32gui.GetClassName (win32gui.GetForegroundWindow())
except (RuntimeError, TypeError, NameError):
return ''
class ClipboardWatcher(threading.Thread):
def __init__(self, predicate, callback, pause=5.):
super(ClipboardWatcher, self).__init__()
self._predicate = predicate
self._callback = callback
self._pause = pause
self._stopping = False
def run(self):
recent_value = ""
while not self._stopping:
tmp_value = getClipboard()
if tmp_value != "" and tmp_value != recent_value and not re.search(r'\sExcel$', getActiveWindowTitle()):
recent_value = tmp_value
if self._predicate(recent_value):
self._callback(recent_value)
recent_value = getClipboard()
time.sleep(self._pause)
def stop(self):
self._stopping = True
def getClipboard():
try:
return pyperclip.paste()
except (RuntimeError, TypeError, NameError):
return ""
def setClipboard(clipboard_content):
try:
return pyperclip.copy(clipboard_content)
except (RuntimeError, TypeError, NameError):
return ""
def main():
watcher = ClipboardWatcher(clean_whitespaces,
print_to_stdout,
1.)
watcher.start()
while True:
try:
# print "Waiting for changed clipboard..."
time.sleep(10)
except KeyboardInterrupt:
watcher.stop()
break
if __name__ == "__main__":
main()