Skip to content

Instantly share code, notes, and snippets.

@zverhope
Created May 14, 2019 21:51
Show Gist options
  • Save zverhope/1b39066b92d30b7e990339b99088a257 to your computer and use it in GitHub Desktop.
Save zverhope/1b39066b92d30b7e990339b99088a257 to your computer and use it in GitHub Desktop.
set myJSONFile to "/Users/zhope/Dropbox/Sundry/Library.json"
set jsonFile to POSIX file myJSONFile
tell application "Keyboard Maestro Engine" to set libraryLastUpdate to getvariable "libraryNextUpdate"
set lastUpdate to date libraryLastUpdate
set theDate to current date
set theSeconds to theDate - lastUpdate
tell application "System Events"
if exists file myJSONFile then
try
set currentBibs to do shell script "cat " & quoted form of myJSONFile & " | /usr/local/bin/jq -r ' .[] | \"\\(.id)\"'"
set currentBibs to paragraphs of currentBibs
set bibJSON to my readFile(jsonFile)
tell application "JSON Helper" to set bibJSON to read JSON from bibJSON
on error
set currentBibs to {}
end try
else
my write_to_file("[", jsonFile, false)
set bibJSON to {}
set currentBibs to {}
end if
end tell
set modKeys to {}
set bibsAdded to {}
set bibsSubtracted to {}
tell application "Bookends"
tell front library window
set theIDs to get citekey of publication items of group all
set matchingPubs to sql search "dateModified > datediff( now(), '01/01/1904 00:00:00', 'second' ) - " & theSeconds
repeat with thePub in matchingPubs
if citekey of thePub is not "" then copy citekey of thePub to end of modKeys
end repeat
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 and n is not "" then
set theItem to (publication items whose citekey is n)
set theRecord to (format theItem using "BibTeX.fmt") as string
set theJSON to do shell script "echo " & quoted form of theRecord & " | /usr/local/bin/pandoc-citeproc -j -f bibtex"
tell application "JSON Helper" to set theRecord to read JSON from theJSON
set the end of bibJSON to item 1 of theRecord
set the end of bibsAdded to n
end if
end repeat
repeat with x from 1 to count of items of currentBibs
set n to item x of currentBibs
if n is in modKeys and n is not in bibsAdded then
set theItem to (publication items whose citekey is n)
set theRecord to (format theItem using "BibTeX.fmt") as string
set theJSON to do shell script "echo " & quoted form of theRecord & " | /usr/local/bin/pandoc-citeproc -j -f bibtex"
tell application "JSON Helper" to set theRecord to read JSON from theJSON
set item x of bibJSON to item 1 of theRecord
else if n is not in theIDs then
set bibJSON to (items 1 thru (x - 1)) of bibJSON & (items (x + 1) thru -1) of bibJSON
end if
end repeat
tell application "JSON Helper" to set finalJSON to make JSON from bibJSON
end tell
end tell
my write_to_file(finalJSON, jsonFile, false)
tell application "Keyboard Maestro Engine" to setvariable "libraryNextUpdate" to (current date) as string
-- Sub-Routine: credit to https://www.macosxautomation.com/applescript/sbrt/sbrt-09.html but modified for utf-8 encoding
on readFile(theFile)
-- Convert the file to a string
set theFile to theFile as string
-- Read the file and return its contents
return read file theFile
end readFile
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
@iandol
Copy link

iandol commented Dec 15, 2020

Hi @zverhope, just to mention that pandoc-citeproc is now deprecated, and pandoc itself can convert BibTeX to CSL-JSON

pandoc -f biblatex -t csljson

Best, Ian

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