Quantcast
Channel: DEVONtechnologies Community - Latest posts
Viewing all articles
Browse latest Browse all 16117

Extracting text with specific linked Bates citation

$
0
0

So I went ahead and did this, but have hit an obstacle.

The code below will generally work, but it is not as precise as copy with source link, because it lacks the start parameter which is supposed to represent the precise position of the first character of the quoted text on the page that it begins on. How can I get that in AppleSript?

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 theContent to plain text of theRecord
		set theUUID to uuid of theRecord
		set theTitle to name of theRecord
		set quotedText to (selected text of think window 1) as string
		set sourceLink to reference URL of theRecord
	
		
		-- Separate the numeric part and prefix from the document's title
		set numericPart to my extractNumericPart(theTitle)
		set prefix to my extractPrefix(theTitle) -- Prefix before the numeric part (e.g., "NOAA")
		set totalLength to the length of theTitle
		set prefixLength to the length of prefix
		set desiredNumericLength to totalLength - prefixLength -- Desired length for the numeric part of Bates number
		
		-- Calculate the Bates number considering the page offset
		set batesNumber to (numericPart as number) + pageNumber
		set batesFormatted to my formatBatesNumber(batesNumber, prefix, desiredNumericLength) -- Format with leading zeros
		
		-- rely on perl for URL encoding
		set theText to quotedText
		set encodedText to do shell script "perl -MURI::Escape -e 'print uri_escape(q{" & theText & "});'"
		
		-- Calculate the precise length
		-- set theLength to count characters of quotedText
		
		-- set newSourceLink to "x-devonthink-item://" & theUUID & "?" & my joinQueryParameters(queryParameters)
		set markdownLink to "[" & batesFormatted & "](" & sourceLink & "?page=" & pageNumber & "&search=" & encodedText & ")"
		
		-- 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 with leading zeros and adding prefix
on formatBatesNumber(batesNumber, prefix, desiredNumericLength)
	set batesStr to batesNumber as text
	repeat until length of batesStr is equal to desiredNumericLength
		set batesStr to "0" & batesStr -- Pad with zeros to match the desired length
	end repeat
	return prefix & batesStr -- Return formatted Bates number with prefix
end formatBatesNumber

-- Extract numeric part from the document title
on extractNumericPart(title)
	set numericString to ""
	repeat with aChar in characters of title
		if aChar is in "0123456789" then set numericString to numericString & aChar
	end repeat
	return numericString -- Preserve leading zeros by returning as string
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

-- Combine query parameters back into a single string for the link
on joinQueryParameters(parameters)
	set AppleScript's text item delimiters to "&"
	return parameters as string
end joinQueryParameters

Viewing all articles
Browse latest Browse all 16117

Trending Articles