AppleScript: problems with clipboard

In HTML records I have lots of links (originally created in Zettelkasten.app), but they broke when I moved to DEVONthink. The broken links look like “#z_60” and the right record to link to in DEVONthink would be “60.html”.

With the script below I can open the window that lets me edit the link, copy the broken link, format it (to match the beginning of the corresponding record), search for that record, get its reference URL and paste it in the link edit window.

But why does it work one time and the next time the clipboard pastes the wrong content? (= the reference URL from the previous run)

Is there a known issue with the clipboard being to slow? I tried a delay of 10 and even then it does not work reliable. Or am I missing something?

PS is there an easier way to do the trick?

tell application "System Events"
	
	tell process "DEVONthink Pro"
		
		set frontmost to true
		
		# open window to copy the broken link (assigned a shortcut for that)
		
		keystroke "x" using {control down, option down, command down, shift down}
		
		set copyLink to (keystroke "c" using {command down})
		
		set oldLink to the clipboard
		
		delay 2
		
		
		# format broken link to match with corresponding record name
		
		set theSelection to (characters ((offset of "_" in oldLink) + 1) thru -1 in oldLink as string)
		
		display notification "Suche nach Zettel: " & theSelection
		
		
		tell application "DEVONthink Pro"
			
			set theSearch to search theSelection
			
			set result1 to item 1 in theSearch
			
			set resultName1 to name of item 1 in theSearch
			
			display notification "Reference URL für: " & resultName1
			
			
			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
						
						keystroke "v" using command down
						
					end tell
					
				end tell
				
			end if
			
		end tell
		
	end tell
	
end tell

First problem is the structure of the script is messy with Inappropriate nesting that can lead to confused variable assignment (including the clipboard). When I run the script in Script Debugger it fails with a different error each time.

tell System Events 
   tell the process DEVONthink Pro
      tell the application DEVONthink Pro 
         tell System Events
            tell the process DEVONthink Pro

Why use GUI scripting to copy something to the clipboard? Use the interface’s ⌘C then run the script.

FWIW, I know you didn’t like the script I posted for you the other day (and so I deleted it) but in four lines it did everything this script does.

Is scripting perhaps not the approach to use here? Have you considered Keyboard Maestro?

You’re right korm. Tried different ways in the script and didn’t clean up. And often I don’t really know what I’m doing in AppleScript…

Thought your script the other day was only a hint where to go, I didn’t run it. My fault.

I just tried your suggestion using ⌘C and it works. Thanks!

Haven’t considered Keyboard Maestro yet. I use Alfred in combination with a Logitech G13 and want to learn AppleScript. Thanks again