Script: Copy RTF and Markdown link

This script copies a RTF and a Markdown link of selected records’ names and Reference URLs.

  • Depending on the receiving app there’s either the RTF or the Markdown version pasted.
  • On pasting RTF links automatically adopt the current app’s font and font size.

Properties

  • revealRecord : reveal record in database (instead of opening a new document window)
  • includeSuffix : include the record’s suffix
-- Copy RTF and Markdown link

use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property revealRecord : true
property includeSuffix : true

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Nothing selected."
		
		set theReferenceURLs to {}
		set theNames to {}
		
		repeat with thisRecord in theRecords
			set end of theReferenceURLs to reference URL of thisRecord
			if includeSuffix then
				set end of theNames to filename of thisRecord
			else
				set end of theNames to name without extension of thisRecord
			end if
		end repeat
		
		my copyRTFandMarkdownLink(theReferenceURLs, theNames)
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warningh
		return
	end try
end tell

on copyRTFandMarkdownLink(theURLs, theNames)
	try
		set theURLsArray to current application's NSMutableArray's arrayWithArray:theURLs
		set theNamesArray to current application's NSArray's arrayWithArray:theNames
		
		if revealRecord = true then
			repeat with i from 0 to ((theURLsArray's |count|()) - 1)
				set thisURLString to (theURLsArray's objectAtIndex:i)
				if (thisURLString's hasPrefix:"x-devonthink") then (theURLsArray's replaceObjectAtIndex:i withObject:(thisURLString's stringByAppendingString:"&?reveal=1"))
			end repeat
		end if
		
		if theURLsArray's |count|() = 1 then
			set theTypesArray to current application's NSArray's arrayWithArray:{"public.utf8-plain-text", "dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu", ¬
				"public.url-name", "dyn.ah62d4rv4gu8zs3pcnzme2641rf4guzdmsv0gn64uqm10c6xenv61a3k", "public.url"}
		else
			set theTypesArray to current application's NSArray's arrayWithArray:{"public.utf8-plain-text", "dyn.ah62d4rv4gu8zs3pcnzme2641rf4guzdmsv0gn64uqm10c6xenv61a3k"}
		end if
		
		set thePasteboard to current application's NSPasteboard's generalPasteboard()
		thePasteboard's declareTypes:theTypesArray owner:(missing value)
		set thePasteboardItem to (thePasteboard's pasteboardItems())'s objectAtIndex:0
		
		repeat with i from 0 to ((theTypesArray's |count|()) - 1)
			set thisType to (theTypesArray's objectAtIndex:i)
			
			if (thisType's isEqualTo:"public.utf8-plain-text") then
				set theMarkdownLinksArray to (current application's NSMutableArray's arrayWithArray:{})
				set theURLsArrayCount to (theURLsArray's |count|())
				repeat with i from 0 to (theURLsArrayCount - 1)
					set thisMarkdownLink to (current application's NSString's stringWithFormat_("[%@](%@)", theNamesArray's objectAtIndex:i, theURLsArray's objectAtIndex:i))
					if i < (theURLsArrayCount - 1) then set thisMarkdownLink to (thisMarkdownLink's stringByAppendingString:(space & space))
					(theMarkdownLinksArray's addObject:thisMarkdownLink)
				end repeat
				(thePasteboardItem's setString:(theMarkdownLinksArray's componentsJoinedByString:linefeed) forType:thisType)
				
			else if (thisType's isEqualTo:"dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu") then
				(thePasteboardItem's setData:(current application's NSPropertyListSerialization's dataWithPropertyList:theURLsArray ¬
					format:(current application's NSPropertyListXMLFormat_v1_0) options:0 |error|:(missing value)) forType:thisType)
				
			else if (thisType's isEqualTo:"public.url-name") then
				(thePasteboardItem's setString:(theNamesArray's objectAtIndex:0) forType:thisType)
				
			else if (thisType's isEqualTo:"dyn.ah62d4rv4gu8zs3pcnzme2641rf4guzdmsv0gn64uqm10c6xenv61a3k") then
				set theNamesWithLinefeedArray to (current application's NSMutableArray's arrayWithArray:{})
				set theNamesArrayCount to theNamesArray's |count|()
				repeat with i from 0 to (theNamesArrayCount - 1)
					set thisName to (theNamesArray's objectAtIndex:i)
					if i < (theNamesArrayCount - 1) then set thisName to (thisName's stringByAppendingString:return) -- necessary for OmniFocus. must be a return (a linefeed adds a blank line in Nisus Writer)
					(theNamesWithLinefeedArray's addObject:thisName)
				end repeat
				(thePasteboardItem's setData:(current application's NSPropertyListSerialization's dataWithPropertyList:{theURLsArray, theNamesWithLinefeedArray} ¬
					format:(current application's NSPropertyListXMLFormat_v1_0) options:0 |error|:(missing value)) forType:thisType)
				
			else if (thisType's isEqualTo:"public.url") then
				(thePasteboardItem's setString:(theURLsArray's objectAtIndex:0) forType:thisType)
			end if
		end repeat
		display notification "Copied"
		return true
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"copyRTFandMarkdownLink\"" message error_message as warning
		error number -128
	end try
end copyRTFandMarkdownLink

2 Likes