Skip to content

Instantly share code, notes, and snippets.

@goerz
Last active September 14, 2018 06:07
Show Gist options
  • Select an option

  • Save goerz/5157942 to your computer and use it in GitHub Desktop.

Select an option

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.
#!/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

ketch commented Jan 5, 2015

Copy link
Copy Markdown

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

@mankoff

mankoff commented Sep 14, 2018

Copy link
Copy Markdown

Foundation comes from pyobjc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment