From 22ba8034352162bdb2b67cb4c36a937f4d7b4410 Mon Sep 17 00:00:00 2001 From: sij Date: Sat, 26 Oct 2024 22:56:25 +0200 Subject: [PATCH] Add Bates Source Link.scpt --- Bates Source Link.scpt | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Bates Source Link.scpt diff --git a/Bates Source Link.scpt b/Bates Source Link.scpt new file mode 100644 index 0000000..20dbb5e --- /dev/null +++ b/Bates Source Link.scpt @@ -0,0 +1,64 @@ +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 + set quotedText to (selected text of think window 1) as string + 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 + set batesNumber to numericPart + pageNumber + set batesFormatted 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 & ")" + + -- Compile the final clipboard content and update the clipboard + set finalClipboardContent to quotedText & " " & markdownLink & "." + set the clipboard to finalClipboardContent + + on error errMsg + 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 + -- Adjusted 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 + if aChar is in "0123456789" then exit repeat + set prefix to prefix & aChar + end repeat + return prefix +end extractPrefix