Script to add a MD file item’s own link to the same file in [filename](x-call back url) format

:raising_hand_man: is there a script available that can iterate over a group of .md files in DEVONthink that prepends the text in the file with the item’s own link (in markdown format)?

For me this would be very useful to open markdown files from an indexed folder in DEVONthink, especially since the release v. 3.7 and the improvements to markdown features.

(Also useful for opening the same files on iOS in DTTG, from another app)

Something like this (tested with 2 MD files only, not to be used in smart rules)

(() => {
  const app = Application("DEVONthink 3");
  const recs = app.selectedRecords();
  recs.forEach(r => {
     let txt = r.plainText();
     let link = `x-devonthink-item://${r.uuid()}`;
     r.plainText = `[${r.name()}](${link})\n\n${txt}`;
  });
})()
1 Like

Thank you very much @chrillek

This works great, really appreciate it!

Are you using MultiMarkdown metadata headers?

Yes. My workflow is evolving and has probably too many steps but generally all my notes and zettels are given some form of front matter.

If you insert a line before the metadata, the metadata would appear in the document. :thinking:

Good idea. I’ve been trying out different options lately. Generally I prefer to hide the metadata though for readability. The best thing about markdown is the flexibility, and 3.7 has added to that a lot. (Off topic but Sorter keyboard shortcuts very are welcome too).

My comment was a warning about the approach of a script that adds a link to the beginning of a document with metadata headers.

Have you tested the script on such a document?

I see what you mean now. I’ve tested the script using a .md document with metadata.

I guess in this scenario the metadata text is no longer ‘recognised’ as meta data as it is pushed down a line by the insertion of the link. Ideally it would add the link after the meta data in this case.

If someone told me how to recognize the metadata, I could try to modify the script

Assuming that metadata is terminated with a line containing three dashes, the script might look like this:

(() => {
  const separator = /^(---).*$/;
  const app = Application("DEVONthink 3");
  const recs = app.selectedRecords();
  recs.forEach(r => {
     let txt = r.plainText();
     let link = `x-devonthink-item://${r.uuid()}`;
     let complete_link = `---\n\n[${r.name()}](${link})\n\n`
     r.plainText = txt.replace(separator, complete_link);
  });
})()

I didn’t test this, though