[solved] AppleScript for copying link not working after update to 3.6

Used to use this here heavily to create a markdown (or rather: org-mode markup link) from item-link.

Now I get an error message: Invalid argument searchString

-- Created: Sunday, 6. September 2020 14:37
-- https://discourse.devontechnologies.com/t/markdown-version-of-the-item-link/55041/2?u=ugur

-- Copy record link(s) as markdown link(s)

tell application id "DNtp"
	try
		set windowClass to class of window 1
		if {viewer window, search window} contains windowClass then
			set currentRecord_s to selection of window 1
		else if windowClass = document window then
			set currentRecord_s to content record of window 1 as list
		end if
		
		if currentRecord_s = {} then
			display notification "Nichts ausgewählt!" with title "DEVONthink"
			return
		end if
		
		-- if only ONE link selected
		if (count of currentRecord_s) = 1 then
			
			set theRecord to item 1 of currentRecord_s
			--set itemName to (name of theRecord)
			set itemName to (filename of theRecord)
			set itemURL to (reference URL of theRecord) & "?reveal=1"
			set theMarkdownLink to "[[" & itemURL & "][DEVONthink: " & itemName & "]]"
			
			set the clipboard to theMarkdownLink
			display notification theMarkdownLink with title "Markdown-Link kopiert"
			
			
		else -- several links selected
			set theMarkdownLinksString to ""
			set theCount to 0
			
			repeat with thisRecord in currentRecord_s
				
				-- set itemName to (name of thisRecord)
				set itemName to (filename of thisRecord)
				set itemURL to (reference URL of thisRecord) & "?reveal=1"
				set thisMarkdownLink to "[[" & itemURL & "][DEVONthink: " & itemName & "]]"
				
				set theCount to theCount + 1
				
				if theCount ≠ (count of currentRecord_s) then
					set theMarkdownLinksString to theMarkdownLinksString & thisMarkdownLink & return
				else
					set theMarkdownLinksString to theMarkdownLinksString & thisMarkdownLink
				end if
			end repeat
			
			set the clipboard to theMarkdownLinksString
			display notification theMarkdownLinksString with title "Markdown-Links kopiert"
			
		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
	end try
end tell

Update

Thanks @pete31 and @cgrunenberg for your help.

Here is the updated version:

-- Copy record link(s) as org-mode markup link(s)
--     for selected record(s)
--
-- Created: Sunday, 6. September 2020 14:37
-- Updated: Friday, 20. November 2020 10:26
--
-- https://discourse.devontechnologies.com/t/markdown-version-of-the-item-link/55041/2
-- https://discourse.devontechnologies.com/t/applescript-for-copying-link-not-working-after-update-to-3-6/59626/4

tell application id "DNtp"
	try
		set theRecords to selection
		
		if theRecords = {} then
			display notification "No records were selected. - Please select one or more records." with title "DEVONthink"
			return
		end if
		
		-- if only ONE record was selected
		if (count of theRecords) = 1 then
			
			set theRecord to item 1 of theRecords
			--set itemName to (name of theRecord)
			set itemName to (filename of theRecord)
			set itemURL to (reference URL of theRecord) & "?reveal=1" -- NB: Adding "reveal" to link!
			set theOrgModeMarkupLink to "[[" & itemURL & "][DEVONthink: " & itemName & "]]"
			
			set the clipboard to theOrgModeMarkupLink
			display notification theOrgModeMarkupLink with title "Copied org-mode markup link to clipboard."
			
		else -- several links were selected	
			set theOrgModeMarkupLinksString to ""
			set theCount to 0
			
			repeat with thisRecord in theRecords
				
				-- set itemName to (name of thisRecord)
				set itemName to (filename of thisRecord)
				set itemURL to (reference URL of thisRecord) & "?reveal=1"
				set thisMarkdownLink to "[[" & itemURL & "][DEVONthink: " & itemName & "]]"
				
				set theCount to theCount + 1
				
				if theCount ≠ (count of theRecords) then
					set theOrgModeMarkupLinksString to theOrgModeMarkupLinksString & thisMarkdownLink & return
				else
					set theOrgModeMarkupLinksString to theOrgModeMarkupLinksString & thisMarkdownLink
				end if
			end repeat
			
			set the clipboard to theOrgModeMarkupLinksString
			display notification theOrgModeMarkupLinksString with title "Markdown-Links kopiert"
			
		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
	end try
end tell

This is an outdated script, version 3 doesn’t have any search windows and since version 3.6 most AppleScript commands like search do not silently ignore invalid parameters anymore. Therefore just replace…

if {viewer window, search window} contains windowClass then

…with…

if windowClass = viewer window then
2 Likes

Wouldn’t be

set theRecords to selected records

sufficient now?

Even set theRecords to selection should be sufficient, the beginning of the script appears to be borrowed from an old script dating back to version 1.x.

This was intended :slight_smile: