Weird text replacement use case - anybody ever had to do something like this before?

Writing blog posts for a site, and I want to interlink a bunch of content. After I finish writing the post, what I’d like to be able to do, in my ideal world, is this:

  • Highlight some words manually
  • Trigger a macro
  • Those highlighted words get case-insensitive matched against a lookup table to return a URL
  • Open MarsEdit’s “link” dialog
  • Paste the URL into MarsEdit

Right now I’m doing this by hand, and it’s taking a ton of time. It would be awesome if I could do this automatically.

Can anybody think of a way to accomplish something like this?

Note - I don’t want to match EVERY instance of the words in the document - only ones I manually select. And some keywords may link to the same thing, so I wouldn’t want to link the same content multiple times. Which means that a more pure text-based “search/replace” program probably wouldn’t do quite what I was looking for.

Thoughts?

There are a number of gaps in what you have set out.

  1. What format do you write in? HTMlL, Markdown, Rich Text?
  2. Does you lookup table already exist? If so, where? If not, what volume and frequency of updates to the table are you looking at?
  3. What does you markup for replacement look like?

The answers to these may throw up more specific questions.

FWIW, if you are using a plain text format, this should be easy to do e.g. via a roundtrip to shortcuts and a set of regex find/replace steps. But the maintenance of the URLs could be a more challenging step. Volume and location are potentially bigger drivers than how to access, but that is a factor too.

If you are dealing with rich text, then there’s almost certainly more work to do, and potentially timing issues if you must work via a GUI … and if you want to do more automation, you may wish to consider using a plain text format instead.

  1. I write in MarsEdit using the default format, which I’m pretty sure is Rich Text.
  2. The lookup table doesn’t already exist. I’d be fine using just about any format. Updates would be infrequent, but maybe a few updates at a time, once a month?
  3. It looks like MarsEdit doesn’t accept cut/paste of Rich Text. Replacement would be through the GUI (Command-Option-A, window pops up with the link on the clipboard pre-filled by MarsEdit, I hit Enter)

I’d be happy to deal with GUI timing issues if I could just get the “find the URL based on the highlighted text” sorted out.

The lookup table could be a JSON file that your read at runtime of your shortcut into a dictionary, where each word is a JSON variable and the URL is the value associated with each. However, better yet, you could store it as a dictionary in Data Jar and use Data Jar to pull out the value (the URL) by the key (the word you are looking up). You could even create a separate shortcut just to add new entries to your dictionary in Data Jar if you don’t want to add them in Data Jar itself.

Then use a URL or Text action to construct the link. I’m not sure how to construct a rich text link but you could construct a Markdown link and then convert it to rich text.

JSON would be easy to do. Are you thinking I could use something like Keyboard Maestro to parse the JSON? Or are you thinking a different automation pipeline?

I think you should be able to do something like the below in Shortcuts. I would use a fairly “flat” JSON file with just a bunch of keys and values.

I tried this with a simple JSON file and it converted it into a dictionary:

{
	"google": "https://google.com",
	"yahoo": "https://yahoo.com"
}

If you are ‘stuck’ with Rich Text for your approach, then you could call an AppleScript to use In Shortcuts to do the GUI interaction, or switch to using Keyboard Maestro (which as you noted can parse JSON).

I would go with the latter (Keyboard Maestro is generally better at GUI interactions than AppleScript alone) and have it copy the text to a custom clipboard where you could scan for a marked up instance of you key phrase, and if found, trigger a find in Mars Edit of the marked up text to select it, then trigger a replace through the addition of the URL. You would also need to pass through some keystrokes to remove you custom Mark up for identifying the text to link.

For the scan of the clipboard, you might want to do a. Iterative match and replace of your markup for text to link. That will mini mise the number of replacements to make and also allow you to link a phrase multiple times.

Hope that helps.

At least part of what you’re describing sounds like something I accomplish with Alfred’s new-ish Universal Actions feature. That is, I can highlight text and trigger Universal Actions, choose the action I want (or trigger the specific one with a keyboard shortcut), and then perform really countless text replacement functions — there’s the simple ones like capitalizing or lowercasing text, to replacing the word/phrase with the same text but with quotation marks around it, to using JavaScript to replace “three” with “3” and etc.

That sounds close to what you’re describing; you could select a phrase, trigger Universal Actions, and then Alfred could do a lookup from an API database based on the phrase (from Airtable, from Coda, etc., maybe with Alfy as an intermediary — or even from a local fine). You could even have Alfred return multiple options for you to choose between, and then the ultimate result would still be text replacement.

Not totally sure how that would integrate with MarsEdit, but maybe it’s a start. If you need more info just let me know.