Hi, I’m trying to modify the script by zverhope to index items from Bookends with additional metadata and reflexive Bookends/DT links. I’ve got something which more or less works. What I need to do now is avoid creating duplicates - i.e., if the item already exists, update its metadata instead of creating a new item.
How can I test for that? Script below
tell application "Bookends"
tell front library window
set theTemplateFile to "/Users/lyndon/Library/Application Support/DEVONthink 3/Templates.noindex/Education/Reference LD.md"
set theIDs to get id of publication items of group item "Red"
repeat with theID in theIDs
try
set myRefs to (publication items whose id is theID)
set myItem to first item of myRefs
set {theKey, thePath, theAuthor, theEditor, theTitle, theShortTitle, theAbstract, theDOI, theKeywords, theURL} to {citekey, path of attachment items, authors, editors, title, short title, abstract, doi, keyword names, url} of myItem
if theAuthor = "" then set theAuthor to theEditor
if theShortTitle = "" then set theShortTitle to theTitle
set theRIS to format myItem using "RIS.fmt"
set theDate to format myItem using "LD date only.fmt"
set theFilename to format myItem using "LD file name.fmt"
set theReference to format myItem using "Oxford Author-Date.fmt"
set theBookendsURL to ("bookends://sonnysoftware.com/" & theID) as text
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set thePath to text items of thePath
set pathCount to count thePath
if pathCount = 0 then
set AppleScript's text item delimiters to otid
tell application id "DNtp"
set theDatabase to open database "/Users/lyndon/DevonThink/Research.dtBase2"
set theLocation to create location "/Library"
set theName to (theFilename & ".md") as text
set thePlaceholders to {|%reference%|:theReference, |%citation%|:theKey, |%doi%|:theDOI, |%url%|:theURL, |%bookends%|:theBookendsURL, |%abstract%|:theAbstract}
set theRecord to import theTemplateFile placeholders thePlaceholders to theLocation
set the name of theRecord to theFilename
set URL of theRecord to theBookendsURL
set aliases of theRecord to theKey
tell theRecord
add custom meta data theRIS for "ris" to it
add custom meta data theDOI for "doi" to it
add custom meta data theAbstract for "abstract" to it
end tell
set tags of theRecord to theKeywords
set theUUID to uuid of theRecord
set theLink to reference URL of theRecord
end tell
end if
if pathCount > 0 then
repeat with i in thePath
set thisPath to i as string
tell application "Finder" to set theName to name of (POSIX file thisPath as alias)
set AppleScript's text item delimiters to otid
tell application id "DNtp"
set theDatabase to open database "/Users/lyndon/DevonThink/Research.dtBase2"
set theLocation to create location "/Library"
set theRecord to indicate thisPath to theLocation
set URL of theRecord to ("bookends://sonnysoftware.com/" & theID) as text
set aliases of theRecord to theKey
tell theRecord
add custom meta data theRIS for "ris" to it
add custom meta data theDOI for "doi" to it
add custom meta data theAbstract for "abstract" to it
end tell
set tags of theRecord to theKeywords
set theUUID to uuid of theRecord
set theLink to reference URL of theRecord
end tell
end repeat
end if
set user15 of myItem to theUUID
set user20 of myItem to theLink
on error errorMessage
end try
end repeat
end tell
end tell