AppleScript to create a "quotes" note for a document - some questions

I’ve tried to refine the “Create new quotes from selection” macro that @Kourosh presents in his book, by doing away with the UI scripting and trying to gather all quotes in a single document.

I’ve used a number of ideas from this forum, of course, and this is still a work in progress. I stole the idea to use a markdown section as a text delimiter from one of the “Backlinks” script that appeared on the forum. I’m sorry if I haven’t written down were I took my sources from …

My current script is run from a KM macro.
It creates a note in a “quotes” group in the current database.

  • The name of the note can be personalised. I use “Citation -Document Alias” as a name, if the alias is defined. If not, “Citation - Document Name”.
  • The quote is the selected text inside the document. If nothing is selected, an error message is generated
  • The “## Reference” is a link back to the original document.
  • Additional quotes will be append to the main body of the note, just before the “## Reference” section

I still have a few questions I couldn’t find answers to:

  • how do I specify a database by name or uuid for the note location?
    create location "/quotes" in database whose name is "memex" throws a syntax error.
  • how do I get the first alias? Aliases are NOT a list, but a string delimited by comas OR semi-colons, so I can’t just say set theAlias to the first item of (aliases of theRecord) ; this just gives me the first letter of the alias.

If this is of interest to anyone, here’s the script:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application id "DNtp"
	-- get information
	set referenceSection to "## Reference"
	set theRecord to (content record of think window 1)
	set theName to name of theRecord
	set theAliases to (aliases of theRecord)
	-- aliases are empty, use name instead
	if theAliases = "" then
		set theAliases to theName
	end if
	set theRecordLink to "[" & theName & ", " & theAliases & "](" & (reference URL of theRecord) & ")"
		set theQuote to "> " & (selected text of think window 1) & return
	on error errMsg number errNum
		display alert (localized string "the Quote can't be empty") & return & errMsg
	end try
	set theQuoteName to "Citations -" & theAliases
	-- create or append quote
		set theQuoteGroup to create location "/quotes"
		if not (exists child theQuoteName of theQuoteGroup) then -- Create the document from scratch
			set theQuoteText to "# Citations - " & theAliases & return & return & theQuote & return & "## Reference" & return & theRecordLink
			set theQuoteRecord to create record with {name:theQuoteName, type:markdown, content:theQuoteText, tags:quote} in theQuoteGroup
		else -- Record already exists, just append the new quote
			set theQuoteRecord to child theQuoteName in theQuoteGroup
			set theText to plain text of theQuoteRecord
			-- split at Reference section
				set oldDelims to AppleScript's text item delimiters
				set AppleScript's text item delimiters to referenceSection
				set delimitedList to every text item of theText
				set AppleScript's text item delimiters to oldDelims
			on error
				set AppleScript's text item delimiters to oldDelims
			end try
				set theExistingQuote to item 1 of delimitedList
				set theExistingReference to item 2 of delimitedList
				set theQuoteText to theExistingQuote & theQuote & return & referenceSection & theExistingReference
			end try
			set plain text of theQuoteRecord to theQuoteText
		end if
		open tab for record theQuoteRecord
	on error errMsg number errNum
		display alert (localized string "An error occured when adding the document.") & space & errMsg
	end try
end tell

Like so

create location "/quotes" in database "xy"

Use Text Item Delimiters with theDelimiter set to {",",";"}

Then get item 1

if theAliases ≠ "" then
	set theAliases_list to my tid(theAliases, {",", ";"})
	set firstAlias to item 1 of theAliases_list
	-- do something else
end if

on tid(theString, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set theList to text items of theString
	set AppleScript's text item delimiters to d
	return theList
end tid