Converse of Convert WikiLinks to Item Links script?

Hi,

I use the Tools > Item Links > Covert WikiLinks to Item Links script on Markdown files a lot, and it’s very useful.

Is there an equivalent script that could do the converse? I.e. take a Markdown Link with [Name of file](x-devonthink-item://<UUID>) and turn it into either a WikiLink [[Name of file]], or even a standard Markdown link [Name of file](path to file).

I can do the first (with some rough edges) using a series of regexes in say BBEdit or Vim, but that’s not really feasible over thousands of files, I think.

If there isn’t such a script, is the applescript for the Convert WikiLinks to Item Links available somewhere, so I could at least try to understand the concepts used (e.g. how to get at the list of links shown in the Inspector Panel)?

Thanks!

That’s not a script; it’s a menu command.

Item links are references.
WikiLinks are Wiki references
They are both either incoming or outgoing.

I know it’s a menu command! I wondered if it was also an AppleScript behind the scenes – one that had been written before and then incorporated into the main program, perhaps.

If not, have you come across such a script? I can’t find one using search.

Development would have to comment on technical details but no, there is no such script. What’s the use case?

Future proofing… If I have to move away from Apple, then how do I ensure that I do not lose the thousands of inter-document links I have, the majority of which are item links?

The only way I can see is to convert the links to WikiLinks or [Name](file path). If there’s a better way, then I’d be happy to explore it…

1 Like

I wouldn’t assume WikiLinks would be future proof.

The have their problems, of course, but they are more widely available than x-DEVONthink-item links. In the circumstances, is there a better option?

1 Like

Using a document’s file path is not recommended as it’s not an absolutely static path. It’s also not recommended to link to a resource inside the database package as that’s no place for people to be rooting around.

There is no immediate solution to your inquiry… but… Are you just referring to Markdown documents and square bracket syntax?

…to…

No. Only commands that import from or export to other applications use scripts.

Thanks for the info!

I understand that I shouldn’t link to imported documents with a file path – I’ve never done that.

However, in the context of having to move to another OS, there is no DT database to worry about / grieve over: the files will all be exported to their own file system location mimicking the DT group structure. (And that location will be either Linux or FreeBSD, so there’s no Windows-Weirdness to mess around with the paths.).

In any case, this is no different to being able to link via file path to indexed files on a Mac, isn’t it? There’s more maintenance required with file-paths compared to x-DEVONthink-item links (or Hookmark links), but neither of those will be available on Linux / FreeBSD, unfortunately.

Of course, not every program supports WikiLinks anyway…

The vast majority of my notes are Markdown documents, so yes, that’s the main focus of my questions.

At the moment, I have a set of regexes that:

  1. split the links out to their own lines
  2. take the name of each link and surround it with double brackets
  3. move the old x-DEVONthink-item links to the end of the file so that they’re not lost

This would be very tedious at scale! So I wondered if there was an AppleScript solution which made use of DT’s already collected knowledge of which links are in a document.

Thanks!

1 Like

Assuming that every document in DT pertains only to one group, i.e. you have no replicants, a script would

  • retrieve each item link (/\[.*?\]\(x-devonthink-item:\/\/(.*?)\)/ in RegEx parlance, storing the UUID in capturing group 1
  • Get the document referred to by this link with getRecordWithUuid
  • Get the absolute “group path” to it with locationWithName
  • Replace the item link with this absolute group path

Something like (no obvious syntax errors but not tested).

function performsmartrule(records) {
   const app = Application("DEVONthink");
   const linkMap = {};
   records.forEach(r => {
      const txt = r.plainText();
      const itemLinks = [...txt.matchAll(/\[.*?\]\(x-devonthink-item:\/\/(.*?)\)/)].map(match => match[1]);
      itemLinks.forEach(link => {
         if (!link in linkMap) {
            const groupPath = app.getRecordWithUuid(link).locationWithName();
            linkMap[link] = groupPath;
            txt.replaceAll(link, groupPath);
         } else {
            txt.replaceAll(link, linkMap[link]);
         }
      })
   })
}

These absolute group links should work in DT as well as the original item links. But I have no idea if each and every MD processor in the wild will handle them as you intend with a DT database exported to the filesystem. Absolute “group paths” might not work as you wish, so some massaging might be needed after export.

2 Likes

Thanks very much for this! I’m not near my desktop for a while, so can’t follow up just yet, but will do later. I appreciate you help.

Funny coincidence; Just yesterday I had the same thought and was looking for a way to do so. I am excitedly following this thread and hope @chrillek’s proposed script is working out for you

1 Like