-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·37 lines (30 loc) · 870 Bytes
/
main.py
File metadata and controls
executable file
·37 lines (30 loc) · 870 Bytes
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
#!/usr/bin/python
from pypinyin import pinyin, Style
import subprocess
import sys
def show_error(message):
subprocess.run([
"notify-send",
"-u", "critical",
"Hanzi to Pinyin Error",
message
])
sys.exit(1)
try:
# Get highlighted text
highlighted_text = subprocess.check_output(
["xsel", "-p"], text=True, stderr=subprocess.DEVNULL
).strip()
if not highlighted_text:
sys.exit(0) # Silent exit for no selection
# Convert to Pinyin with tones
pinyin_text = ' '.join([item[0] for item in pinyin(highlighted_text, style=Style.TONE)])
# Copy to clipboard
subprocess.run(
["xsel", "-ib"],
input=pinyin_text,
text=True,
stderr=subprocess.DEVNULL
)
except Exception as e:
show_error(f"{str(e)}\n(Selected text: '{highlighted_text[:50]}')")