Skip to content

Instantly share code, notes, and snippets.

@goerz goerz/gist:5157942
Last active Sep 14, 2018

Embed
What would you like to do?
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.
#!/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)
@ketch

This comment has been minimized.

Copy link

commented Jan 5, 2015

Can you tell me -- what is "Foundation" and how can I install it?

@mankoff

This comment has been minimized.

Copy link

commented Sep 14, 2018

Foundation comes from pyobjc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.