Copying PDF Text, Citation, and Clickable Link to Clipboard

I’ve added a handler to your script that copies both, a markdown and a RTF link.

Depending on the destination document one link is inserted when you paste, in RTF documents you’ll get a RTF link, in markdown a markdown link.

-- Set clipboard to selected text and versions of a link to the current PDF page (RTF and markdown)

-- The handler sets the clipboard to both, a Markdown and a RTF version of the link.
-- Depending on the destination document one link is inserted when you paste, in RTF documents you'll get a RTF link, in markdown a markdown link.
-- It can be used to either copy only links or links plus text.

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} or (count theRecords) > 1 then error "Please select a PDF document"
		set thisRecord to item 1 of theRecords
		
		if (type of thisRecord) is PDF document then
			set currentPage to (current page of think window 1) -- This is zero-indexed, so page 20 is page=19. If you are going to use the page in text, you need to increment it plus one.
			set recURL to reference URL of thisRecord
			set xref to (recURL & "?page=" & currentPage)
			set customMD to custom meta data of thisRecord
			set mdcasename to (mdcasename of customMD)
			set mdcasecitation to (mdcasecitation of customMD)
			set mdcourt to (mdcourt of customMD)
			set mdfirstpage to (mdfirstpage of customMD)
			set pincite to ((mdfirstpage as number) + (currentPage))
			if mdcourt ≠ "U.S." then
				set mdcourt to mdcourt & " "
			end if
			if mdcourt = "U.S." then
				set mdcourt to ""
			end if
			set mdyear to (mdyear of customMD)
			
			-- get selected text
			try
				set selectedText to selected text of think window 1 & "" as string
			on error
				error "No text selected"
			end try
			
			-- copy links and selected text  
			set theName to ("_" & mdcasename & "_" & ", " & mdcasecitation & ", " & pincite & " (" & mdcourt & mdyear & ")") as string
			my copyMarkdownRTFLink(theName, xref, selectedText, linefeed & "--  " & linefeed, "<br>--  <br>")
			
			-- copy links without text
			#my copyMarkdownRTFLink(theName, xref, "", "", "")
			
		else
			error "Please select a PDF document"
		end if
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on copyMarkdownRTFLink(theName, theURL, theText, theDelimiter_Markdown, theDelimiter_HTML)
	try
		set theName to my replace_String(theName, "&", "&amp;")
		set theName to my replace_String(theName, "<", "&lt;")
		set theName to my replace_String(theName, ">", "&gt;")
		set theHTMLLink to ("<a href=\"" & theURL & "\">" & theName & "</a>") as string
		do shell script "export LANG=\"en_US.UTF-8\" && echo '<font face=\"helvetica\">' " & quoted form of theText & quoted form of theDelimiter_HTML ¬
			& quoted form of theHTMLLink & "'</font>' | textutil -format html -convert rtf -inputencoding UTF-8 -stdin -stdout | pbcopy -Prefer rtf"
		if theText ≠ "" then set theText to theText & space & space
		set theMarkdownText to (theText & theDelimiter_Markdown & "[" & theName & "](" & theURL & ")") as string
		set theClipboard_record to the clipboard as record -- https://forum.latenightsw.com/t/how-do-i-set-clipboard-pasteboard-to-both-rich-text-rtf-and-plain-text/1189/3
		set theClipboard_RTFdata to «class RTF » of theClipboard_record -- binary RTF data
		set the clipboard to {Unicode text:theMarkdownText, «class RTF »:theClipboard_RTFdata}
	on error error_message number error_number
		activate application id "DNtp"
		display alert "Error: Handler \"copyMarkdownRTFLink\"" message error_message as warning
		error number -128
	end try
end copyMarkdownRTFLink

on replace_String(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replace_String

1 Like