-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipboard_guard.py
More file actions
50 lines (37 loc) · 1.36 KB
/
clipboard_guard.py
File metadata and controls
50 lines (37 loc) · 1.36 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
from dataclasses import dataclass
from typing import List, Tuple
from AppKit import NSPasteboard
@dataclass
class PasteboardSnapshot:
"""
Semantic snapshot of NSPasteboard.generalPasteboard().
Stores (type, raw_bytes) for all materialized types.
"""
items: List[Tuple[str, bytes]]
def snapshot_clipboard() -> PasteboardSnapshot:
"""Take a semantic snapshot of the current clipboard."""
pb = NSPasteboard.generalPasteboard()
types = pb.types() or []
items: List[Tuple[str, bytes]] = []
for t in types:
data = pb.dataForType_(t)
if data is None:
# Some lazy/promised types may not materialize; safe to skip
continue
items.append((str(t), bytes(data)))
return PasteboardSnapshot(items=items)
def restore_clipboard(snapshot: PasteboardSnapshot) -> None:
"""Restore clipboard from a previously taken snapshot."""
pb = NSPasteboard.generalPasteboard()
pb.clearContents()
for t, raw in snapshot.items:
try:
pb.setData_forType_(raw, t)
except Exception:
# Private / unsupported types may fail -- acceptable and expected
pass
def overwrite_clipboard_with_text(text: str) -> None:
"""Replace clipboard contents with plain text."""
pb = NSPasteboard.generalPasteboard()
pb.clearContents()
pb.writeObjects_([text])