Created
March 22, 2017 21:53
-
-
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
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
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