Skip to content

Instantly share code, notes, and snippets.

@lyndondrake
Last active February 7, 2022 07:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyndondrake/8983fa2c493f0f0911c6810c3b2ff64d to your computer and use it in GitHub Desktop.
Save lyndondrake/8983fa2c493f0f0911c6810c3b2ff64d to your computer and use it in GitHub Desktop.
to convertDate(textDate)
if length of textDate is 4 then
set textDate to "1/1/" & textDate
end if
set resultDate to my date textDate
return resultDate
end convertDate
to surname(textName)
set r to textName
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set s to text items of textName
if s is not missing value then
set r to (item 1 of s) as string
end if
set AppleScript's text item delimiters to otid
return r
end surname
to authorlist(authors)
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "
"
set r to authors as text
set AppleScript's text item delimiters to otid
return r
end authorlist
set theTemplateFile to "/Users/lyndon/Library/Application Support/DEVONthink 3/Templates.noindex/Education/Reference LD.md"
tell application id "DNtp"
set theDatabase to open database "/Users/lyndon/DevonThink/Research.dtBase2"
set theLocation to create location "/Library"
end tell
tell application "Bookends"
tell front library window
set theRefs to selected publication items
set itemCount to count of theRefs
if itemCount > 0 then
set toProcess to display dialog "You have " & itemCount & " items selected. Would you like to process the selected items or the entire library?" buttons {"Cancel", "Selection", "Library"} default button 3
end if
if the button returned of toProcess is "Library" then
set theRefs to publication items of group item "DT"
else if the button returned of toProcess is "Cancel" then
return
end if
repeat with theRef in theRefs
set {theID, theKey, thePaths, dtUUID, origURLs, modDate, theAuthor, theEditor, theDateText, theTitle, theShortTitle, theAbstract, theDOI, theKeywords, theURL} to {id, citekey, path of attachment items, user15, user20, date modified, authors, editors, publication date string, title, short title, abstract, doi, keyword names, url} of theRef
if theAuthor = "" then set theAuthor to theEditor
if theShortTitle = "" then set theShortTitle to theTitle
set theDate to my convertDate(theDateText)
set theYear to the year of theDate as string
set theBookendsURL to ("bookends://sonnysoftware.com/" & theID) as text
repeat 1 times -- dummy loop to allow skipping to next item if not recently modified in Bookends
tell application id "DNtp"
set theRecord to get record with uuid dtUUID in theDatabase
if theRecord is missing value then
set theFilename to theYear & " " & theShortTitle
set theAuthor to paragraphs of theAuthor
if (count of theAuthor) is 1 then
set s1 to my surname(item 1 of theAuthor)
set theFilename to s1 & " " & theFilename
else if (count of theAuthor) is 2 then
set s1 to my surname(item 1 of theAuthor)
set s2 to my surname(item 2 of theAuthor)
set theFilename to s1 & " and " & s2 & " " & theFilename
else if (count of theAuthor) > 2 then
set s1 to my surname(item 1 of theAuthor)
set theFilename to s1 & " et al " & theFilename
end if
set theGroup to create location "/Library/" & theFilename
set origURLs to theURL
else
if (modification date of theRecord) > (modDate + (1 * minutes)) then
set theTags to the tags of theRecord
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "
"
set theTags to theTags as text
set AppleScript's text item delimiters to otid
tell application "Bookends" to set the keywords of theRef to theTags
end if
if modDate ≤ modification date of theRecord then
exit repeat -- jump out of dummy loop to next item
end if
set theGroup to theRecord
end if
-- set group metadata and grab reference details
tell theGroup
set URL to theBookendsURL
set aliases to theKey
set tags to theKeywords
set custom meta data to {|date|:theDate, doi:theDOI, abstract:theAbstract, citekey:theKey, bookendsid:theID}
set dtNewUUID to uuid
set dtLink to reference URL & "?reveal=1"
end tell
-- update Bookends record
tell application "Bookends"
tell theRef
set user15 to dtNewUUID
set user20 to origURLs
set url to dtLink
end tell
end tell
-- create summary Markdown file
set theSummaryName to ("___" & theKey & ".md") as text
set theAuthorList to my authorlist(theAuthor)
set theSummaryPlaceholders to {|%reference%|:theTitle, |%authors%|:theAuthorList, |%date%|:theYear, |%citation%|:theKey, |%doi%|:theDOI, |%abstract%|:theAbstract}
set theTempRecord to import theTemplateFile placeholders theSummaryPlaceholders to theGroup
set theSummaryRecord to get record at (the location of theTempRecord) & theSummaryName
if theSummaryRecord is missing value then
set the name of theTempRecord to theSummaryName
set theSummaryRecord to theTempRecord
else
set theTempContent to the plain text of theTempRecord
set the plain text of theSummaryRecord to theTempContent
delete record theTempRecord
end if
set URL of theSummaryRecord to theBookendsURL
set custom meta data of theSummaryRecord to {|date|:theDate, doi:theDOI, abstract:theAbstract, citekey:theKey, bookendsid:theID}
set tags of theSummaryRecord to theKeywords
-- create bookmarks to URLs
set origURLs to paragraphs of origURLs
repeat with theOrigURL in origURLs
set theBookmarkRecord to lookup records with URL theOrigURL
if theBookmarkRecord is missing value or (count of theBookmarkRecord) is less than 1 then
create record with {name:theOrigURL, type:bookmark, URL:theOrigURL} in theGroup
end if
end repeat
-- index the attachments
repeat with thePath in thePaths
set theAttachmentRecord to lookup records with path thePath
if theAttachmentRecord is missing value or (count of theAttachmentRecord) is less than 1 then
set theAttachmentRecord to indicate thePath to theGroup
else
set theAttachmentRecord to item 1 of theAttachmentRecord
end if
set URL of theAttachmentRecord to theBookendsURL
set custom meta data of theAttachmentRecord to {|date|:theDate, doi:theDOI, abstract:theAbstract, citekey:theKey, bookendsid:theID}
set tags of theAttachmentRecord to theKeywords
end repeat
end tell
end repeat
end repeat
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment