DEVONthink/Bates Source Link.scpt
sij b24b302df8 Update Bates Source Link.scpt
- Removed unused Perl deeplink formatter script
- Added quotation marks before and after selected text in clipboard
2024-10-29 00:29:58 +01:00

68 lines
2.2 KiB
AppleScript

tell application id "DNtp"
try
-- Retrieve record details directly
set theRecord to content record of think window 1
set pageNumber to current page of think window 1
set theTitle to name of theRecord
-- Get selected text
set selectedText to (selected text of think window 1) as string
-- Add quote marks around selected text
set quotedText to "\"" & selectedText & "\""
-- Get source link of selected text
set sourceLink to reference URL of think window 1
-- Extract the numeric part and prefix from the document's title
set numericPart to my extractNumericPart(theTitle)
set prefix to my extractPrefix(theTitle)
if numericPart is "" then -- Handle non-Bates cites
set formattedCite to theTitle & " at " & pageNumber + 1
else -- Handle Bates cites
set batesNumber to numericPart + pageNumber
set formattedCite to my formatBatesNumber(batesNumber, prefix)
end if
-- Create the markdown link
set markdownLink to "[" & formattedCite & "](" & sourceLink & ")"
-- Compile the final clipboard content and update the clipboard
set finalClipboardContent to quotedText & " " & markdownLink & "."
set the clipboard to finalClipboardContent
on error errMsg -- Error handling
display dialog "Error: " & errMsg -- Show any errors that occur
end try
end tell
-- Formatting the Bates number without leading zeros
on formatBatesNumber(batesNumber, prefix)
return prefix & batesNumber -- Return formatted Bates number with prefix
end formatBatesNumber
-- Extract numeric part from the document title
on extractNumericPart(title)
try
-- Perl script to capture only the numeric sequence following one or more uppercase letters and a space or underscore
set perlScript to "perl -ne 'print $1 if /^[A-Z]+[ _]([0-9]+)(?:-[0-9]+)?/; exit;'"
return do shell script "echo " & quoted form of title & " | " & perlScript
on error errMsg
return ""
end try
end extractNumericPart
-- Extract prefix (non-numeric part) from the document title
on extractPrefix(title)
set prefix to ""
repeat with aChar in characters of title
-- stop when we reach a numeric character
if aChar is in "0123456789" then exit repeat
set prefix to prefix & aChar
end repeat
return prefix
end extractPrefix