Trying to relink all URLs after import from Evernote's enex files with Apple Script

Processing: test.enex…
test.enex.zip (2.2 KB)

I’ve attached an example. There are two notes in enex file that can be imported via File > Import > Files and Folders. The first note Sed ut perspiciatis has a link to the second note Lorem ipsum with reference evernote:///view/2199673/s25/438c47ee-8c1e-418f-a2c7-54e84467ace9/438c47ee-8c1e-418f-a2c7-54e84467ace9/. I’m trying to get all such references to be replaced with Devonthink cross-note links. Both imported notes will have text starting with evernote:///view/... embedded in their url metadata field, however only the destination note has evernote:///view/2199673/s25/438c47ee-8c1e-418f-a2c7-54e84467ace9/438c47ee-8c1e-418f-a2c7-54e84467ace9/.
I wonder how developers of the app did this. Because notes imported the proposed way (File > Import > Notes from Evernote) have their links already replaced. But I can’t import one huge notebook with standard means, this is why I need all this AppleScript stuff.

BTW this is my current version of script ATM:

using terms from application "DEVONthink 3"
	tell application "DEVONthink 3"
		try
			# (i) for every formatted note in main db
			set db to database "main"
			set recs to search "kind:formattednote" in root of db
			repeat with rec1 in recs
				log message (name of rec1) as text
				# (ii) for every link in note pointing towards destination that starts with "evernote://"
				set chunks to attribute run of rich text of rec1
				repeat with chunk in chunks
					set ref1 to (URL of chunk)
					if ref1 is not null then # ERROR
						if ref1 starts with "evernote://" then
							set recs2 to search "kind:formattednote url:" & (ref1 as text) in root of db
							# (iii) for every record2 formatted note in db that has the given url in its url field
							repeat with rec2 in recs2 # there should be only one record, this line is for debug reasons
								# (iv) replace evernote link with the new one
								set url2 to URL of record2
								if url2 is equal to URL of chunk then
									log message (ref1 as text) & " got to be replaced with " & ((reference URL of rec2) as text)
									# set (URL of chunk) to (reference URL of rec2)
								end if
							end repeat
						end if
					end if
				end repeat
			end repeat
		on error error_message number error_number
			if the error_number is not -128 then
				display alert "DEVONthink 3" message error_message as warning
			end if
		end try
	end tell
end using terms from