„Add Link“ via AppleScript?

I’m trying to migrate notes from Zettelkasten.app (zettelkasten.danielluedecke.de) to DEVONthink. I exported the notes in HTML and now I want to rebuild the cross-references in DEVONthink. So far I’ve thrown together the AppleScript below. I select the broken link (displayed as a number), run the script and it searches for this number, then displays a dialog which copys the reference URL of the selected file in the search window to the clipboard. This works, but I think I could save time if someone could answer…

My two questions:

  1. Is there a way to select the first result in a search window via AppleScript (instead of using the mouse)?

  2. Is there a way to add a link to a selected text in HTML via AppleScript (instead of using the context menu)?

tell application "DEVONthink Pro"
	
	set theSelection to selected text of think window 1
	
	delay 1
	
	tell application "System Events"
		
		tell process "DEVONthink Pro"
			
			set frontmost to true
			
			keystroke "f" using {control down, option down, command down}
			
			key code 51
			
			activate
			
			delay 1
			
			keystroke theSelection
			
			key code 36
			
			display dialog "Right?" buttons {"Cancel", "Copy Reference URL"} with title "Copy Reference URL" default button 2
			
			# Dialog, der fragt ob das momentan ausgewählte Dokument das gewünschte ist (u die Reference URL zu kopieren)
			
			if button returned of result is "Copy Reference URL" then
				
				tell application id "DNtp"
					
					set theRefURL to (the reference URL of (the first item of (selection as list)) as string)
					
					set the clipboard to theRefURL
					
					close think window 1
					
					
				end tell
				
			end if
			
		end tell
		
	end tell
	
end tell

(deleted a pointless comment)

Hi korm, I attached a screenshot. The numbers above “Literaturangaben” (at the bottom) are the broken Links. I tried your suggested “search” command, but can’t get it to work. No special need for UI scripting, just want to get the job done. I created a separate database to import the HTML files - if there’s a way in AppleScript to tell this search command to only search in this database the first search result will always be the right one. I think this could be done but have no clue how.

(deleted a useless suggestion)

Thanks a lot! Its incredibly faster this way.

  1. select text (the broken link)

  2. run script

  3. paste reference URL

  4. hit enter

Done! :smiley:

In case someone else in the future wants to migrate from Zettelkasten to DEVONthink, here’s what works for me to rebuild the cross-references.

PS I assigned a shortcut to “Add Link…” in order to call it from the script

tell application "DEVONthink Pro"
	
	set theSelection to selected text of think window 1
	
	set the clipboard to theSelection
	
	set theResults to search theSelection
	
	set result1 to item 1 in theResults
	
	set resultName1 to name of item 1 in theResults
	
	
	if resultName1 does not start with theSelection then
		
		display notification "Nicht gefunden"
		
		tell application "System Events"
			
			set theProcesses to processes
			
			if theProcesses does not contain "Zettelkasten" then
				
				tell application "Zettelkasten"
					
					activate
					
				end tell
				
			end if
			
			
			tell process "Zettelkasten"
				
				set frontmost to true
				
				delay 1
				
				keystroke "g" using {command down}
				
				keystroke theSelection
				
				key code 36
				
				keystroke "e" using {command down, shift down}
				
				delay 1.5
				
				try
					
					tell window "Zettel auswählen"
						
						tell radio button 2
							
							click
							
						end tell
						
					end tell
					
					keystroke "v" using {command down}
					
				end try
				
			end tell
			
		end tell
		
	else if resultName1 starts with theSelection then
		
		set theRefURL to (the reference URL of result1)
		
		set the clipboard to theRefURL
		
		tell application "System Events"
			
			tell process "DEVONthink Pro"
				
				set frontmost to true
				
				# I assigned a shortcut to "Link hinzufügen" (think it's "Add Link.." in english)
				
				keystroke "l" using {control down, option down, command down, shift down}
				
			end tell
		end tell
		
	end if
	
end tell