Replace Named Clipboard Sub-String with Hyperlink

Hi Folks, I'm struggling with something that I thought would be quite simple and would appreciate any help.

After copying some rich text (containing some hyperlinks) into a named clipboard, I want to search to find specific a hyperlink and replace it with a new hyperlink I have stored in another named clipboard.

However, regardless of whether I use the simple or Regex Search and Replace Action, the replacement operation only ever changes that hyperlink title, leaving the link pointing to the original destination. I just can't seem to figure out how to overwrite the entire hyperlink with the new hyperlink.

The key action I am using in this process is as follows:

%Variable%Instance__Old Link Title% is the old hyperlink title I am searching on
%NamedClipboard%002 EN New Link% is the clipboard that contains the new hyperlink (I have confirmed via separate testing that the hyperlink is being stored here correctly).

I suspect that the Search and Replace operation is the problem in that it is probably only confined to operating on text fields, but given that it takes named clipboards I was hoping that it would enable me to essentially add in rich text as part of the replacement operation.

Any guidance greatly appreciated.

Any thoughts, anyone? Perhaps this is not possible or there is a better KM action I should be using?

Not much information to go on here but on the face of it, it doesn't make sense to search for a title and replace the title with the new link. I would think you would be searching for the old link and replacing it with the new one. But perhaps you don't know what the old link actually is.

The actually clipboard data and the failing macro or a simplified version of it would be helpful in better diagnosing and resolving the issue.

1 Like

Thanks for the reply @mrpasini.

Here is some more information that might help understand the objective and the issue I am facing. I didn't want to clog up the first post with too much extraneous info so apologies if it was too vague.

Firstly, to answer your question, the only reason I search of the old link title (text) is that I don't know of any other way to search for a hyperlink using the Search and Replace action, since it only appears to search for text (no option to search for a hyperlink). I am only using the search to locate the old link after which I want to replace it with the new hyperlink that I have stored in a named clipboard ("002 EN New Link").

By way of further context, my objective is to ultimately create a "hyperlink search and replace" macro to work in Evernote for Mac, but I won't be able to do that unless I can first prove that it possible to search and replace a hyperlink in an individual note. I don't think that the Evernote note type (.enml) is the issue as I expect I would be facing the same challenge if I was just trying to replace hyperlinks in a plain old Rich Text Format note.

I created the attached Proof of Concept KM macro to try to get a successful search and replacement of a hyperlink but so far no luck. Everything seems to work correctly except for the step where the search and replace action is performed - it just doesn't find and/or replace the old link with the new link.

... and that brings me to the forum here scratching my head and wishing my brain was a little larger. :slight_smile:

002 [EN] EN Link Find and Replace (POC Test) v0.1 WIP.kmmacros (20.8 KB)

Hi @Ramst3r,

The variable you're setting in this prompt:


is not the same one you're using in the search and replace action:


There are two underscores in the variable you're using for the search term (Instance__Old Link Title) but only one in the variable you're setting in the prompt (Instance_Old Link Title). So while at a glance it may have seemed like you were using the same variable to search, you were actually using an empty variable, so it's no surprise that nothing was being replaced. Easy mistake to make, and fortunately also an easy one to fix once you see it.

1 Like

Good pick up @gglick. I developed that macro specifically for this forum post to assist in troubleshooting and mistyped the variable. Unfortunately, correcting the variable name doesn't solve the problem in that while the link title is now updated correctly, the underlying URL is still targeting the original note. I appreciate the help nevertheless and apologies for the stuff up. :+1:t3:

I've attached the corrected test macro (v0.2) and screenshot in case you or anyone else wishes to test and suggest any other course of action.

002 [EN] EN Link Find and Replace (POC Test) v0.2 WIP.kmmacros (20.9 KB)

Unfortunately, I don't use Evernote so I can't tell what it puts on the clipboard, which is the key to this puzzle. Can you use the Append Text to File action to write that out for analysis (or submission here)?

The trick would be to search that text for the HTML anchor surrounding your linked title to be able to replace the link (rather than the title).

The named clipboards don't seem essential to that, BTW.

1 Like

I think I see where you are coming from @mrpasini.

I added a "Write to file" action to the test macro as follows:

I ran the test macro against the following test Evernote, which shows the link that I am copying at the start of the macro:


The output file "002_New_EN_Link" only shows the new link title as text, and not as HTML or XML as I hoped. Refer following screenshot:


The System Clipboard appears to be copying the link intact, as the following screenshot of the System Clipboard shows:


If I could find a way to get that System Clipboard information converted to HTML or XML then I think I could use the Regex Search and Replace, as I think you are suggesting.

Any ideas how that might be done?

Try to use RTF instead of plain text in the Write clipboard action.

1 Like

Thanks for the suggestion @JimmyHartington.

Using RTF format puts a hyperlink into the file (refer following screenshot) when what I (think) I need is to be able to get the clipboard converted in text format as:

<a href="evernote:///view/650xxxx/s57/yada yada>New Test Note 3</a>

I should then be able to use regex to search and replace the link text to replace the link.

I suspect there is some way to convert RTF to achieve this, but that's beyond my capabilities. I appreciate your help.


I think you can use the Javascript for Automation from this post by @ComplexPoint :

So this process the clipboard and can return the html to a new variable, which you then can manipulate.

I have tried it with the macro below. And if I copy something from a webpage and run it, then I get HTML-code.

Keyboard Maestro 8.2.4 “Clipboard to html code” Macro

Clipboard to html.kmmacros (2.1 KB)

1 Like

Thanks a bunch @JimmyHartington! :+1:t3: . That looks promising. It'll stretch my brain but I will give it try and get back to this post with progress afterwards.

1 Like

I implemented that JSA script 'as-is' and it worked a treat to convert the RTF clipboards to HTML. For example the HTML file following was produced for the new Evernote link which clearly shows the tag I could then manipulate for my regex search later:

However, I soon realised that the process of converting the clipboards from RTF to HTML and then back to RTF, stripped out all the other elements of an Evernote note such as images, attachments, checkboxes etc. This effectively killed my grand plans to use KM to create a hyperlink search and replace macro.

Unfortunately, I'll have to put this particular project on hold until either I gain more brain cells to figure out how such a solution might be implemented that doesn't destroy the rest of the Evernote note. I've over invested in this particular project so will have to move onto something else.

I appreciate your's and everyone's assistance. :slight_smile:

Cheers. /R

I don’t think you have to convert the RTF content to anything. RTF is already a markup language and you can do any Search & Replace in it.

The trick is to open the RTF document as plain text.

I don’t have Evernote, so I’ll demonstrate it with a little RTF file created with TextEdit:

This is the interpreted RTF text, as seen by an RTF editor:


This is the (uninterpreted) source, the RTF markup, as seen by a plain text editor:


You gan get the RTF markup simply by opening any RTF file with a plain text editor (for example BBEdit, TextMate, etc.). Alternatively you can open it with TextEdit via the “Open…” menu item, then in the file selection dialog, under Options, select “Ignore rich text commands”.

You see, the RTF markup language has borrowed some elements from TeX, for example the backslash is a meta character and marks a command. Curly braces are used to form groups.

The first section of the source text contain general formatting commands (lines 1 through 6).

The second section (starting at line 8) contains the actual content with inline formatting commands.

This section also contains three hyperlinks:

I think you can easily spot the hyperlinks and their associated anchor texts (aka link labels) in the source text.

  1. Now to get the source text into KM we can simply open the file (ExampleRTF.rtf in my example) and read it to a variable (Local__Input).
  2. Then we apply our Search & Replace action (see explanation below) to that variable.
  3. Finally we write the result back to a new file ExampleRTF-Output.rtf


[demo] Search and Replace in RTF Markup.kmmacros (3.3 KB)

If you look at the source text, you see that all hyperlinks are constructed the same way:

{\field{\*\fldinst{HYPERLINK "<target link>"}}{\fldrslt <anchor text>}}

The Search & Replace action contains a regex to find all the target links in the source text, and replaces each one with a new value ( in the example macro).

If you want to modify the anchor texts (instead of the target links), you have to use a different/additional regex. (From your posts it wasn’t entirely clear to me if you wanted to change the links themselves or their anchor texts.)

The macro is a minimal demonstration of how you could do it. It is lacking anything related to EverNote as well as any other complications, but I think you get the idea and I’m sure you can build your macro around it.

Some things to pay attention to:

  • Try the macro first with my demo RTF file. Obviously you have to adapt the paths in the macro to the actual paths on your disk. (1.1 KB)

  • As said the example RTF was created with TextEdit. It may happen that EverNote uses a slightly different RTF version, and hence the structure of a hyperlink might be —slightly— different (though I don’t think so). So, verify the actual hyperlink syntax by inspecting the RTF markup (as plain text) of an EverNote-created RTF file.

1 Like

I'm not sure I understand your ultimate objective, but if you want to edit the Evernote enml source file to change something/anything, then I have an AppleScript that might help.

It currently opens the enml file for the selected Note in BBEdit, where you can make changes, use the BBEdit RegEx Find, etc. You could probably mod it to use the KM RegEx Search/Replace if you wish.

Let me know if this sounds helpful, and I'll post the AppleScript.

1 Like

Thanks a bunch @Tom for the comprehensive explanation of how I might be able to search and replace directly in the RTF document. I was able to follow your instructions and do a manual simulation of how I might be able to build a KM macro to achieve my objective. It all looks possible when the Evernote note only contains hyperlinks.

However, I hit two issues when the Evernote note contains an image or attachment:

  1. If an image is present, TextEdit requires the file to be saved as a RTFD format which does not display the source information when the opened with the option to "Ignore rich text comments". I suspect this issue might be overcome by using a different text editor.

  2. Attachments in an Evernote note do not get copied to the KM clipboard so they will get lost in any macro I create that relies on clipboard manipulation. That's a showstopper for me as I need my search and replace macro to work across all notes I have in Evernote, not just the ones that only have hyperlinks. I suspect the only way around this is to get directly into the note source as @JMichaelTX indicated he did in his reply after yours. His approach of accessing Evernote's source .ENML files looks promising.

@JMichaelTX, wrt my objective, I am trying to create a search and replace macro to make up for absence of this feature in the current Evernote for Mac app. I would like to go one step further and also be able to search for specific hyperlinks and replace with another chosen hyperlink. Your post prompted me to dig a little deeper into ENML and I see that a lot of users have chosen to edit the source ENML file as a way to make up for the deficiencies in the Evernote editor. I suspect your AppleScript might be very useful for me to do this so would be very grateful if you could post it. I am sure it will help me one way or another on my automation journey.

Thanks again.

The RTFD format is an Apple invention, as far as I know, and it’s only used by TextEdit (and probably other RTF components of the macOS).

An RTFD is a package (like a folder). If you right-click an RTFD you’ll see the option to show the package contents:


Then, inside the package, you’ll see something like this:


So, if you want to manipulate the RTF markup, you have to open that RTF file inside the RTFD package with your plain text editor.

But, as said, this is a TextEdit-special format, and it shouldn’t bother you. (I only took TextEdit for my example, because it’s installed on every Mac.)

With any “normal” RTF implementation (as used by MS Word, Nisus Writer, Keyboard Maestro, etc., and probably also Evernote) the image data is stored directly in the RTF file, either in binary format, but most of the times as hex-encoded text string.

This just to eliminate your confusion with the RTFD format :wink:

Back to Evernote:

Initially I had the impression that an Evernote note is already RTF. But I’m not sure now. Is the internal format of a note already RTF or is RTF an export option?

This would indeed be a showstopper. Unless an EN note is already RTF or the attachment links are preserved in an RTF export.

1 Like

To answer my own question: I have installed Evernote now, and noticed this:

Neither the internal format of an EN note is RTF, nor is there an RTF export option. So the RTF way is pretty pointless IMO. (You could do it via Clipboard, but this is clumsy and, above all, you’d indeed loose all non-image attachments.)

The EN notes are saved as XML with mostly HTML content. They are located as content.enml in subfolders in ~/Library/…/…/…

So, I think @JMichaelTX’s proposal to programmatically edit the source files is a good way.

I’ve manually edited a couple of them, and EN seems to have no problem with the external edits.

Alternatively you can export a note as .enex file (the content seems to be pretty identical to the source file), manipulate the content, and import it back. According to what I’ve seen in EN’s AppleScript dictionary, this should be automizable too.

1 Like

@Ramst3r, OK, give me a day or so to cleanup my script for public consumption, and I'll post. If you don't hear from me within 2 days, feel free to ping me here, or in a PM.

1 Like

Thanks very much @JMichaelTX. No rush so take your time.