-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyClip7.sh
71 lines (52 loc) · 1.71 KB
/
keyClip7.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
#!/usr/bin/env bash
### keyClip7
### Copyright (c) 2023 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 = pboard.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)
# initialize variables for scoping and error checking
fVal = -1
retrieve = None
for i in range(0,count):
type = types[i]
# find only instances of TSPData for checking
fVal = type.find(\"TSPData\")
if fVal == -1:
continue
# search through for XML blobs with chemdraw flag
retrieve = pboard.dataForType_(type)
fVal = retrieve.find(b'/XML')
cVal = retrieve.find(b'chemdraw')
# if chemdraw XML blob is found end iterative search
if ((fVal != -1) and (cVal != -1)):
rawXML = retrieve
break
# error trap, no CDXML object found in TSPData
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
rawCDXML = rawXML.split(b'/XML')[1].split(b'endstream')[0].split(b'stream')[1][1:]
CDXML = zlib.decompress(rawCDXML)
# spawn pbcopy process and serve CDXML to pasteboard
copyProcess = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
copyProcess.stdin.write(CDXML)
copyProcess.stdin.close()" > $PYFILE
### execute python script
python3 $PYFILE
### cleanup
rm $PYFILE