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

No worries, I’m aware of such possibilities. And the Devontkink DB is still in import and testing status, I import it and do something with it almost every day.

1 Like

I promise I will, but I don’t have time for it today.

I had to replace EVlink to EVURL to mitigate an obvious naming error. However the script doesn’t change a thing (tried on a single note). Can you debug it on example in post Trying to relink all URLs after import from Evernote's enex files with Apple Script - #6 by Idify ?

I was hoping that I’d get a reference to the source property inside the forEach. Obviously, I wasn’t. So the script is now updating the current record’s source directly, which requires two more lines and one change at the end.
New version:

'use strict';
(() => {
  const app = Application("DEVONthink 3");
  const records = app.selectedRecords.whose({_match: [ObjectSpecifier().type, "formatted note"]});
  const sources = records.source();
  const EVnote = `<a href="evernote:///view/`;
  const EVRE = new RegExp(`<a href="(evernote:///view.*?)"`);
  let index = 0;
  sources.forEach(s => {
    if (s.indexOf(EVnote) > 0) {
	  const EVfound = s.match(EVRE);
	  if (EVfound) { /* There's a link inside this note to another one */
	    const EVURL = EVfound[1]; /* get the URL only */
		const results = app.search(`kind:formattednote url==${EVURL}`);
		if (results && results.length > 0) {
		  const DTURL = results[0].referenceURL();
		  /* the next line replaces the complete evernote link
		     with the DT link, i.e. evernote:///view/... is replaced by x-devonthink:///
			 */
 		  records[index].source = s.replace(EVURL, DTURL);
		}
	  }
	}
	index++;
  })
})()

Memory consumption grows too fast: ~ 300 MB/sec. I got about 30 Gigs used in just two minutes. I received AppleEvent timed out in Script Editor but Devonthink seemed to continue the process, so I had to terminate it manually.

Impressive. Sorry that this didn’t help.

Just curious but how much memory does this simple script use? I don’t have any real Evernote data but a basic test performing the replacement using 1500 copies of the documents of the above .enex file didn’t increase the memory usage at all.

tell application id "DNtp"
	show progress indicator "Relink Evernote Notes" steps (count of selected records) with cancel button
	repeat with theRecord in selected records
		step progress indicator (name of theRecord) as string
		if cancelled progress then exit repeat
		if type of theRecord is formatted note then
			set theSource to source of theRecord
			if theSource contains "evernote:///view/" then
				set replaced to false
				set theScope to root of database of theRecord
				set theLinks to get links of theSource
				repeat with theLink in theLinks
					if theLink begins with "evernote:///view/" then
						set theResults to search "kind:formatted note url:\"" & theLink & "\"" in theScope
						-- set theResults to search "kind:formatted note url==" & theLink in theScope
						if (count of theResults) is greater than 0 then
							set theReferenceURL to reference URL of item 1 of theResults
							set theSource to my findAndReplaceInText(theSource, theLink, theReferenceURL)
							set replaced to true
						end if
					end if
				end repeat
				if replaced then
					-- log message record theRecord info "Updated"
					set source of theRecord to theSource
				end if
			end if
		end if
	end repeat
	hide progress indicator
end tell

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText

BTW:
In case of a large database containing a lot of stuff not imported from Evernote, a good way to improve the performance might be to move the Evernote notes to a new database, execute the script and then move the notes back.

1 Like

After some experiments and DB crashes (had to terminate app) I had only ~2300 notes (~1/2 of the original count) with evernote links left in inbox.
Run finished in 15 min 07 sec, consumed 28,7 Gigs. Comparing to Trying to relink all URLs after import from Evernote's enex files with Apple Script - #35 by Idify it has relatively same speed but a slightly bigger memory consumption.

My sample is appropriate for debug reasons only, not even close to real conditions. Real notes have from 0 to over 30 links each, some have embedded images that result in 3 to 10 megabytes size and that may be the best guess what causes all this memory leaking mess. I would suggest to add a 3 MB image and replicate such notes (I can make a sample if needed).

Yes it really matters. That’s why I’m converting all right in the inbox. Previous attempts with databases, especially while testing the encrypted ones were less successful. To be honest I haven’t even brought any of it to finish.

More comprehensive sample data would of course be appreciated. Which versions of DEVONthink & macOS do you currently use?

Versions are: DEVONthink 3.7.2 (Trial), MacOS BigSur 11.5.1 (20G80).
sample2.enex.zip (3.1 MB)

On a side note: the current version of macOS Big Sur is 11.6. I’m sure you’re aware, but I thought I’d give a gentle reminder :slight_smile:

I did see an update thanks to your post and began to download it. You can consider it 11.6 already :slight_smile:

Awesome! :smiley:

Thank you for the data! Unfortunately I couldn’t reproduce the memory usage using my script and both version 3.7.2 and the latest internal build on macOS 11.6 so far.

Hmm… it’s strange.
What else can produce such memory usage in my case if not that notes contain images, formatting, a lot of links etc?
But it’s not so important cause the script can be used group by group.
BTW what memory consumption did you have?

Almost no additional memory usage while executing the script and this additional usage was more or less steady.

Glad I came across this thread. I’m trying to do something similar and I think I could modify the scripts here to get the results I want (since I’m an amateur scripter, I have trouble making these from scratch.)

I want to export and share a Markdown text file that contains links to items within my database. But the person I’m sharing with doesn’t use DEVONthink, so these local DT item links will break on their machine. I want to replace the x-devonthink-item:// links with each item’s URL. And since most of the items in question were imported with the web clipper, the URL field points usually points to a web-accessible source!

https://hookproductivity.com might be able to help - AFAIK, it can replace DNT Links with “hard” links that even work on other Macs

1 Like

I’m a Hook user but I don’t know about that feature!

I know Hook links are shareable if the other user has the file downloaded on their machine. But I want to link to web URLs, not local files, because the recipient doesn’t have these files downloaded. Can Hook do that?

Alternatively I could send a zip with all the linked assets. But that seems like it’s going overboard - it’s a little clumsier. I’d prefer to send a single lightweight markdown or PDF file to my client rather than a whole folder.