Last active
September 14, 2018 06:07
-
-
Save goerz/5157942 to your computer and use it in GitHub Desktop.
Script that is an adaption of the code found at
http://n2.nabble.com/Loss-of-local-file-links-td2209604.html It adds a
Local-File field to the BibTeX file with the relative path of the referenced
file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" Add/update Local-File field to BibDesk-organized BibTeX file """ | |
import sys | |
import base64 | |
import re | |
import shutil | |
from Foundation import * | |
key_pattern = re.compile(r'^(?P<indent>\s*)Bdsk-File-1 = \{(?P<key>.*)\}[,}]$') | |
def decode_bdisk(key): | |
"""Return the relative path of file referenced by key """ | |
decodedBytes = base64.b64decode(key) | |
nsData = NSData.dataWithBytes_length_(decodedBytes, len(decodedBytes)) | |
plist, fmt, error \ | |
= NSPropertyListSerialization\ | |
.propertyListFromData_mutabilityOption_format_errorDescription_(\ | |
nsData, 0, None, None) | |
if plist is None: | |
print "key: %s" % key | |
print "failed to decode archive due to error: %s" % (error) | |
exit(1) | |
# currently an implementation detail; may not be at position 6 in future | |
if (len(plist["$objects"]) > 6): | |
return plist["$objects"][6] | |
else: | |
print "unknown format for property list %s" % (str(plist)) | |
file = sys.argv[1] | |
backup = "%s~" % file | |
shutil.copy(file, backup) | |
out = open(file, 'w') | |
for line in open(backup): | |
key_match = key_pattern.search(line) | |
if key_match: | |
out.write("%sLocal-File = {%s},\n" | |
% (key_match.group('indent'), | |
decode_bdisk(key_match.group('key')))) | |
if not re.search("^\s*Local-File = ", line): | |
out.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Foundation comes from
pyobjc
.