-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyClip6.sh
72 lines (53 loc) · 1.89 KB
/
keyClip6.sh
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
#!/usr/bin/env bash
### Copyright (c) 2019 Mark A. Maskeri (see LICENSE.txt)
### setup
PYFILE=`uuidgen`".py"
cd ~/Desktop
### prepare python script, put below script in file PYFILE
echo "import zlib
import subprocess
import sys
from AppKit import NSPasteboard
# initialize pasteboard
pboard = NSPasteboard.generalPasteboard()
pbItems = NSPasteboard.generalPasteboard().pasteboardItems()
types = pbItems[0].types()
count = types.count()
# obtain NSData-form metadata for the PDF...for some reason hidden
# in TSPData.## (not always the last)
# and convert to string form for further processing
# initialize variables for scoping and error checking
fVal = -1
retrieve = None
for i in range(0,count):
type = unicode(types[i])
# find only instances of TSPData for checking
fVal = type.find(\"TSPData\")
if fVal == -1:
continue
# search through for XML blobs
retrieve = str(pboard.dataForType_(type))
fVal = retrieve.find(\"/XML\")
cVal = retrieve.find(\"chemdraw\")
# matched TSPData and found XML blob therein
if (fVal != -1) and (cVal != -1):
rawSTR = retrieve
break
# error trap
if fVal == -1:
print \"keyClip: ChemDraw XML object not found\"
quit()
# isolate zlib-encoded CDXML binary blob from NSPasteboard metadata
# [1:] clips off remaining newline, retains zlib header b'x(escape)x9c'
# CDXML blob should be only XML data blob on pasteboard
# if Adobe changes PDF file format, can adjust using cambridgesoft tag
# note "ChemDraw" tag inconsistently served; adjusted for cval above to "chemdraw" as part of the "cambridgesoft" tag
CDXML = zlib.decompress(rawSTR.split(\"/XML\")[1].split(\"endstream\")[0].split(\"stream\")[1][1:])
# spawn pbcopy process and paste CDXML to clipboard
copyProcess = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
copyProcess.stdin.write(CDXML)
copyProcess.stdin.close()" > $PYFILE
### execute python script
python $PYFILE
### cleanup
rm $PYFILE