Paste P657

x11_clipboard_watch_operator_id_py_to_c.py
ActivePublic

Authored by Campbell Barton (campbellbarton) on Apr 16 2018, 2:18 PM.
Tags
None
Subscribers
None
1#!/usr/bin/env python3
2
3"""
4Continually check for operator strings in the clipboard.
5"""
6
7
8VERBOSE = False
9
10
11def run(cmd, return_result=True):
12if VERBOSE:
13print(">>> ", cmd)
14
15import subprocess
16
17if return_result:
18proc = subprocess.Popen(
19cmd,
20shell=False,
21stderr=subprocess.PIPE,
22stdout=subprocess.PIPE)
23
24result = proc.stdout.read()
25return result
26else:
27proc = subprocess.Popen(cmd, shell=False)
28proc.wait()
29
30
31def translate(w):
32pre = "bpy.ops."
33if w.startswith(pre):
34w = w[len(pre):].split('(')[0]
35head, tail = w.partition('.')[0::2]
36text = head.upper() + "_OT_" + tail
37if text != translate.text:
38print('- ' + text)
39translate.text = text
40translate.text = None
41
42
43
44def main():
45import time
46
47clip_prev = None
48while 1:
49clip_curr = run(["xclip", "-selection", "clipboard", "-out"])
50
51# first time only, don't do anything
52if clip_prev is None:
53clip_prev = clip_curr
54
55if clip_curr != clip_prev:
56clip_text = clip_curr.decode('utf-8')
57if clip_text:
58translate(clip_text)
59
60clip_prev = clip_curr
61time.sleep(0.05)
62
63
64if __name__ == "__main__":
65try:
66main()
67except KeyboardInterrupt:
68print("finishing up!")
69