Skip to content

Instantly share code, notes, and snippets.

@Noleli
Created March 22, 2017 21:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noleli/1bc0d651547825e212ea1ec291ad17f3 to your computer and use it in GitHub Desktop.
Save Noleli/1bc0d651547825e212ea1ec291ad17f3 to your computer and use it in GitHub Desktop.
A Python script to add Notational Velocity/nvALT tags as hashtags for Bear
from subprocess import check_output
import re
import os
from shutil import copy2 as copy
# adjust paths to your liking
path = os.environ['HOME'] + '/Dropbox/Preferences/Notational Data/'
outpath = os.environ['HOME'] + '/Desktop'
os.mkdir(outpath + '/export')
for filename in os.listdir(path):
source = path+filename
dest = outpath + '/export'
print filename
tags = check_output('mdls -raw -name kMDItemUserTags "' + source.replace('"', '\\"') + '"', shell=True)
copy(source, dest)
if tags == '(null)':
continue
else:
tags = tags.split(',')
tags = map(lambda t: '#'+ re.search(r'\(?(\\n)?\s*"?([a-z0-9\-]+)\s*\)?', t).group(2), tags)
atime = os.path.getatime(source)
mtime = os.path.getmtime(source)
with open(dest + '/' + filename, 'a+') as newfile:
newfile.write('\n\n' + ' '.join(tags))
os.utime(dest + '/' + filename, (atime, mtime))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment