Last active
June 17, 2019 20:41
-
-
Save zverhope/1d4d7783e543ca5465eefee049623738 to your computer and use it in GitHub Desktop.
This file contains 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
set myBibFile to "/Users/zhope/Dropbox/Sundry/Library.bib" | |
if myBibFile exists then | |
set currentBibs to do shell script "/usr/local/bin/pandoc-citeproc --bib2json " & quoted form of myBibFile & " | /usr/local/bin/jq -r ' .[] | \"\\(.id)\"'" | |
set currentBibs to paragraphs of currentBibs | |
end if | |
set bibFile to POSIX file myBibFile | |
my write_to_file("", bibFile, true) | |
set bibs2add to {} | |
set bibs2subtract to {} | |
tell application "Bookends" | |
tell front library window | |
set theIDs to get citekey of publication items of group all | |
repeat with x from 1 to count of items of theIDs | |
set n to item x of theIDs | |
if n is in theIDs and n is not in currentBibs then set end of bibs2add to n | |
end repeat | |
repeat with x from 1 to count of items of currentBibs | |
set n to item x of currentBibs | |
if n is in currentBibs and n is not in theIDs then set end of bibs2subtract to n | |
end repeat | |
set bibCount to count of bibs2add | |
set steps to 25 | |
set nLoop to round (bibCount / steps) rounding up | |
set thisLoop to 1 | |
repeat while thisLoop is less than or equal to nLoop | |
-- set the batch index range | |
set startindex to (steps * thisLoop) - (steps - 1) | |
set endindex to (steps * thisLoop) | |
if endindex is greater than bibCount then | |
set endindex to bibCount | |
end if | |
set thisListItems to items startindex thru endindex of bibs2add | |
set myBibs to "" | |
repeat with theKey in thisListItems | |
set matchingPub to (publication items whose citekey is theKey) | |
-- set theID to id of first item of matchingPub | |
set theRecord to (format matchingPub using "BibTeX.fmt") as string | |
-- set theRecord to «event ToySGUID» theID given «class RRTF»:"false", string:"bibtex" | |
set myBibs to myBibs & theRecord & linefeed | |
end repeat | |
my write_to_file(myBibs, bibFile, true) | |
set thisLoop to thisLoop + 1 | |
end repeat | |
if (count of bibs2subtract) is greater than 0 then | |
set subCount to 1 | |
repeat with theKey in bibs2subtract | |
set ln to do shell script "grep -n " & quoted form of theKey & " " & myBibFile & " | cut -d: -f 1" | |
if subCount is 1 then | |
do shell script "sed -i.bak " & ln & "d " & myBibFile | |
else | |
do shell script "sed -i.delete " & ln & "d " & myBibFile | |
end if | |
set subCount to subCount + 1 | |
end repeat | |
if myBibFile & ".delete" exists then do shell script "rm " & myBibFile & ".delete" | |
end if | |
end tell | |
end tell | |
-- Sub-Routine: credit to https://www.macosxautomation.com/applescript/sbrt/sbrt-09.html but modified for utf-8 encoding | |
on write_to_file(this_data, target_file, append_data) | |
try | |
set the target_file to the target_file as string | |
set the open_target_file to open for access file target_file with write permission | |
if append_data is false then set eof of the open_target_file to 0 | |
write this_data to the open_target_file as «class utf8» starting at eof | |
close access the open_target_file | |
return true | |
on error | |
try | |
close access file target_file | |
end try | |
return false | |
end try | |
end write_to_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment