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

OkyDoky, misunderstood. In that case the modified script could work, but I’m no expert in that. Good luck

The following script seems to do what you want: It replaces all x-devonthink-item links with the URLs of the record the DT item is referring to. If that URL is defined. Otherwise you’ll get a message in DTs protocol window. This will, for example, happen with all PDFs that you scanned or images from your camera or … basically with everything that did not come from the web (see my question below for that).

function onperformsmartrule(records) {
  
  /* The function performing the replacement.
    UUID is the content of the capturing group in linkRE 
  */
  function replacementFunction(match, UUID, offset, string) {
    const record = app.getRecordWithUuid(UUID);
    const URL = record.url();
    if (!URL || URL === '') {
      app.logMessage(`URL missing from record "${UUID}"`);
    } else {
      return `(${URL})`;
    }
  }

  var app = Application("DEVONthink 3");
  app.includeStandardAdditions = true;

  /* Define regular expression for DT links: 
     Search for the string "x-devonthink-item://" following an opening parenthesis
     Save everything following it up to the next closing parenthesis in a capturing group
     This group should then contain the records UUID.
  */
  
  const linkRE = /\(x-devonthink-item:\/\/([^)]*)\)/g;
  /* Loop over all records */
  records.forEach(r => {
    const plainText = r.plainText(); /* The raw text of the MD file */
    r.plainText = plainText.replace(linkRE, replacementFunction);
  })
}

(() => {
  const app = Application("DEVONthink 3");
  onperformsmartrule(app.selectedRecords());
})()

The script can added to a smart rule that selects the relevant MD records (action “Run script”, select “JavaScript” and copy the script in the editor window). Alternatively, you can select the records directly in DT and run the script.

BEWARE: The MD records will be changed without any further ado. So better test the script’s behaviour thoroughly on some copies before using it in production. Also, it does not check if a record is really of type MD. The check can be added if needed.

Finally, I’m not quite sure why you even need this. If I drag a DT record in an MD file (in DT, that is), the resulting link automatically points to the URL in question.

@pete31: The really cool thing here is the replacement function. Not sure if the NSRegEx functions offer that. It is of course not strictly necessary, but comfy.

1 Like

Thanks for trying! I’m getting an error… And deleting the offending ) doesn’t seem to fix it.
Screen Shot 2021-11-11 at 12.18.09 AM

??? I’m confused. When I drag a record into a markdown file, I get an x-devonthink-item:// link, not a regular URL.

As to why I need this:
I’m writing a text that I want to share with someone who isn’t a DT user and doesn’t have access to the same downloaded files that I have in my database. So instead of sharing a document thats riddled with x-devonthink-item:// links that won’t do any good for my client, I want to replace the DT links with regular web links.

Sorry for that. I made a minor correction in the post and apparently screwed up. Pls remove the second parenthesis after replacementFunction)), the end of this line should be replacementFunction);. I’ll update the code in the post, too.

Well, if the DT document has an URL (like in the case of a bookmark, something downloaded from the web…), this URL is placed in the link here. If there’s no link in the original document, all kinds of things happen: In one case, I get a link to the path within DT, in another case the whole document gets inserted.

I understood that. But: If the DT records do have an URL, you could use that already in your markup document (instead of the x-devonthink-item link). If they do not have an URL, converting the x-devonthink-item link to an URL will not work, there’s nothing to share with anybody if it is something without a generally accessible URL.

Argh—these wretched parentheses! :wink: (Apologies—intended very light-heartedly.)

Stephen

1 Like

Luckily, we don’t have to write Lisp code here. That’s actually mind-boggling (and they wrote special editors to keep the parenthesis in check)

1 Like