-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard_regex_replace.py
198 lines (158 loc) · 5.78 KB
/
clipboard_regex_replace.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import keyboard
import pyperclip
import re
import logging
import traceback
import json
from time import sleep
from pystray import Icon, Menu, MenuItem
from PIL import Image
import threading
import os
import sys
# Set up logging
logging.basicConfig(filename='clipboard_regex_replace.log', level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')
def load_config():
try:
with open('config.json', 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
logging.error(f"Failed to load config: {str(e)}")
return {}
def get_clipboard_text():
if sys.platform == 'win32':
import win32clipboard
win32clipboard.OpenClipboard()
try:
return win32clipboard.GetClipboardData()
finally:
win32clipboard.CloseClipboard()
elif sys.platform == 'linux':
import subprocess
return subprocess.check_output(['xclip', '-selection', 'clipboard', '-o']).decode('utf-8')
else:
raise NotImplementedError("Unsupported platform")
def set_clipboard_text(text):
if sys.platform == 'win32':
import win32clipboard
win32clipboard.OpenClipboard()
try:
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(text)
finally:
win32clipboard.CloseClipboard()
elif sys.platform == 'linux':
import subprocess
subprocess.run(['xclip', '-selection', 'clipboard'],
input=text.encode('utf-8'))
else:
raise NotImplementedError("Unsupported platform")
def simulate_paste():
if sys.platform == 'win32':
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('^v')
elif sys.platform == 'linux':
import subprocess
subprocess.run(['xdotool', 'key', 'ctrl+v'])
else:
raise NotImplementedError("Unsupported platform")
def show_notification(title, message):
config = load_config()
if config.get('use_notifications', False):
try:
from plyer import notification
notification.notify(
title=title,
message=message,
app_name='Clipboard Regex Replace',
timeout=2 # Display for 2 seconds
)
except ImportError:
logging.warning(
"Plyer library not installed. Notifications disabled.")
except Exception as e:
logging.error(f"Failed to show notification: {str(e)}")
def replace_clipboard_text():
try:
# Get the current clipboard content
text = pyperclip.paste()
logging.info(f"Original text: {text}")
# Load the config
config = load_config()
replacements = config.get('replacements', [])
# Apply all regex replacements
for replacement in replacements:
pattern = replacement['regex']
replace_with = replacement['replace_with']
text = re.sub(pattern, replace_with, text, flags=re.UNICODE)
logging.info(f"Modified text: {text}")
# Set the new text to clipboard
pyperclip.copy(text)
# Show notification
show_notification(
"Clipboard Updated", "Text has been replaced according to your regex rules.")
# Wait to ensure the clipboard is updated
sleep(0.5)
# Attempt to paste
try:
if sys.platform == 'win32':
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('^v')
elif sys.platform == 'linux':
import subprocess
subprocess.run(['xdotool', 'key', 'ctrl+v'])
else:
raise NotImplementedError("Unsupported platform")
logging.info("Automatic paste attempted")
except Exception as paste_error:
logging.error(f"Automatic paste failed: {str(paste_error)}")
logging.info(
"Automatic paste failed. Please press Ctrl+V to paste manually.")
show_notification(
"Paste Failed", "Please press Ctrl+V to paste manually.")
except Exception as e:
logging.error(f"An error occurred: {str(e)}")
logging.error(traceback.format_exc())
def exit_action(icon):
icon.stop()
def setup_keyboard(hotkey):
try:
keyboard.add_hotkey(hotkey, replace_clipboard_text)
logging.info(f"Hotkey {hotkey} registered successfully")
except Exception as e:
logging.error(f"Failed to register hotkey: {str(e)}")
logging.error(traceback.format_exc())
def run_script(hotkey):
setup_keyboard(hotkey)
logging.info("Script is running in the background.")
def load_icon(icon_path):
if icon_path and os.path.exists(icon_path):
try:
return Image.open(icon_path)
except Exception as e:
logging.error(f"Failed to load icon: {str(e)}")
# Default to red square if icon_path is not provided or invalid
return Image.new('RGB', (64, 64), color=(255, 0, 0))
def main():
config = load_config()
hotkey = config.get('hotkey', 'ctrl+alt+v')
icon_path = config.get('icon_path')
# Create a simple menu
menu = Menu(MenuItem('Exit', exit_action))
# Load icon
image = load_icon(icon_path)
# Create a system tray icon
icon = Icon("Clipboard Regex Replace", image,
"Clipboard Regex Replace", menu)
# Run the script in a separate thread
script_thread = threading.Thread(target=run_script, args=(hotkey,))
script_thread.start()
# Run the system tray icon
icon.run()
# When the icon is stopped, also stop the keyboard listener
keyboard.unhook_all()
if __name__ == "__main__":
main()