How to search replace within a markdown link, and how this led me to new way of working in KM

How to search replace within a markdown link, and how this led me to new way of working in KM.

I was looking for new applications of the excellent macro Copy as Markdown Link - Macro Library - Keyboard Maestro Discourse (thank you @ComplexPoint ) and this led me to a new way of working in KM.

The problem this solves is the fact that I am usually working on more than 10 macros at a time and it's difficult to keep track of ongoing research pertinent to each macro. I put a keyword in the macro title and create a smart group, but I am limited to storing comments and pertinent website links in comment boxes in those macros which is tedious.

First I found that if I select a macro in KM → Copy as Markdown Link → If I paste that link in a Markdown (Bear) or RTF (Textedit, etc) note, clicking on the link opens the macro in KM editor (does not trigger the macro).

A macro markdown link looks like

Anybox

~[Anybox](keyboardmaestro://m=D629986E-D53B-4DA5-899A-BADAB11149C3)~

I then found that if I select multiple KM macros → run copy markdown link → the links to all selected macros are batch copied to the clipboard and can easily be batch pasted in Markdown or RTF notes which makes life much easier.

I created a "KM Journal" in Bear (Markdown but could be any RTF editor) in which I list all macros I am currently working on together with a link to each macro, to which I can add notes, links to websites, etc

I than noticed that macro URL triggers have the following structure which is similar to that of the Markdown link above

kmtrigger://macro=AA492269-558C-4D06-933D-091B6A5C0095

What I would now like to do and here is where I have difficulty:

I would like to add to my KM Journal the ability to trigger macros, so for each macro, I will end up with 2 links: one to edit the macro and the other to trigger it.

If I select a macro → copy as Markdown link, I end up with


~[Anybox](keyboardmaestro://m=D629986E-D53B-4DA5-899A-BADAB11149C3)~

I would like to create a macro which would convert the following in the clipboard (the output of copy as markdown link)

~[Anybox](keyboardmaestro://m=D629986E-D53B-4DA5-899A-BADAB11149C3)~

to a clipboard containing 2 links, one to edit and one to trigger the macro

~[Anybox EDIT](keyboardmaestro://m=D629986E-D53B-4DA5-899A-BADAB11149C3)~

~[Anybox TRIGGER](kmtrigger://macro=D629986E-D53B-4DA5-899A-BADAB11149C3)~

The difference being

  • the prefix of the edit URL is keyboardmaestro://m= and the prefix of the trigger is kmtrigger://macro=)
  • because both links look the same in Markdown (ie Anybox) I want to add "EDIT" AND TRIGGER just before "]"

Thanks in advance for your time and help and sorry for the long post

There are probably more elegant ways of doing this, but this is quite simple: Just using Search and Replace on the text string, and storing each result in two separate variables, then writing both back to the clipboard.

Download Macro(s): _Convert markdown link.kmmacros (3.8 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 13.6.5
  • Keyboard Maestro v11.0.2

I can't test in Bear, but I think this gets what you want: One entry on the clipboard containing both forms of the link.

-rob.

1 Like

Fantastic. Superb ! Works perfectly with the Anybox model. thank you very much !

I have 2 things to modify because at the start of the macro I would execute the copy markdown link macro and I must make your macro generic for any link.

1- I would replace (1) with %SystemClipboard%

2- (2) and (3) I don't know how to (likely a regex) to replace 2 and 3 with add suffix EDIT / TRIGGER to whatever string is between [ ] and make the macro generic for whatever markdown link is in the clipboard. Would you have an idea.

3- If I run the macro with multiple markdown links in the clipboard before running the macro I first have all the edits and following the edits all the triggers. I imagine that it would be very complicated to sequence them edit + trigger edit + trigger etc

thanks again

Simulation of multiple links

Outcome
image

No regex required, as long as there's always only one "]" in the string. Just change the search to "]" and the replaces to " EDIT]" and " TRIGGER]"

You'd just need to put the search/replace stuff in a For Each loop for rows on the clipboard, and then build up a variable containing the combined rows. Something like this:

Download Macro(s): _Clipboard loop test.kmmacros (3.9 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 13.6.5
  • Keyboard Maestro v11.0.2

Note this isn't a full solution, just most of what's above wrapped in the loop.

-rob.

1 Like

works perfectly. You are a genius ! Thank you so much !