Skip to content

Instantly share code, notes, and snippets.

Last active Sep 14, 2018
What would you like to do?
Script that is an adaption of the code found at 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\
nsData, 0, None, None)
if plist is None:
print "key: %s" % key
print "failed to decode archive due to error: %s" % (error)
# currently an implementation detail; may not be at position 6 in future
if (len(plist["$objects"]) > 6):
return plist["$objects"][6]
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 =
if key_match:
out.write("%sLocal-File = {%s},\n"
% ('indent'),
if not"^\s*Local-File = ", line):
Copy link

ketch commented Jan 5, 2015

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

Copy link

mankoff 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