diff --git a/Bates Source Link.scpt b/Bates Source Link.scpt index 20dbb5e..dbdf18e 100644 --- a/Bates Source Link.scpt +++ b/Bates Source Link.scpt @@ -4,32 +4,37 @@ tell application id "DNtp" set theRecord to content record of think window 1 set pageNumber to current page of think window 1 set theTitle to name of theRecord - set quotedText to (selected text of think window 1) as string + + -- 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) - -- Determine the Bates number or fallback - if numericPart is "" then - set batesFormatted to theTitle & " at " & pageNumber + 1 - else + + 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 batesFormatted to my formatBatesNumber(batesNumber, prefix) + set formattedCite to my formatBatesNumber(batesNumber, prefix) end if - -- URL encoding using Perl - set encodedText to do shell script "perl -MURI::Escape -e 'print uri_escape(q{" & quotedText & "});'" - -- Create the markdown link - set markdownLink to "[" & batesFormatted & "](" & sourceLink & ")" + 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 + on error errMsg -- Error handling display dialog "Error: " & errMsg -- Show any errors that occur end try end tell @@ -42,7 +47,7 @@ end formatBatesNumber -- Extract numeric part from the document title on extractNumericPart(title) try - -- Adjusted Perl script to capture only the numeric sequence following one or more uppercase letters and a space or underscore + -- 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 @@ -50,15 +55,14 @@ on extractNumericPart(title) 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 +