-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-clipboard.py
More file actions
74 lines (63 loc) · 1.93 KB
/
simple-clipboard.py
File metadata and controls
74 lines (63 loc) · 1.93 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
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
"""Read from or write to the system clipboard."""
import sys
import platform
import argparse
import subprocess
def _find_cmd(candidates: list[list[str]]) -> list[str]:
import shutil
for cmd in candidates:
if shutil.which(cmd[0]):
return cmd
raise RuntimeError(f"No clipboard tool found. Tried: {[c[0] for c in candidates]}")
def paste() -> str:
system = platform.system()
if system == "Windows":
cmd = ["powershell", "-NoProfile", "-Command", "Get-Clipboard"]
elif system == "Darwin":
cmd = ["pbpaste"]
else:
cmd = _find_cmd(
[
["xclip", "-selection", "clipboard", "-out"],
["xsel", "--clipboard", "--output"],
["wl-paste"],
]
)
result = subprocess.run(cmd, capture_output=True, text=True)
return result.stdout
def copy(text: str) -> None:
system = platform.system()
if system == "Windows":
cmd = ["clip"]
elif system == "Darwin":
cmd = ["pbcopy"]
else:
cmd = _find_cmd(
[
["xclip", "-selection", "clipboard"],
["xsel", "--clipboard", "--input"],
["wl-copy"],
]
)
subprocess.run(cmd, input=text, text=True, check=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Read/write the system clipboard.",
epilog="Copy: echo hello | %(prog)s Paste: %(prog)s --paste",
)
parser.add_argument(
"--paste", "-p", action="store_true", help="Print clipboard to stdout"
)
parser.add_argument(
"text", nargs="?", help="Text to copy (alternative to stdin pipe)"
)
args = parser.parse_args()
if args.paste:
sys.stdout.write(paste())
elif args.text:
copy(args.text)
elif not sys.stdin.isatty():
copy(sys.stdin.read())
else:
parser.print_help()
sys.exit(1)