How to import very large set of text snippets (CSV or .tab) as simple text macros?

My apologies if this has been answered before / can be found in the documentation; I didn’t.

I am using a text expansion tool that I’d like to get rid of and import all my shortcuts into Keyboard Maestro, my preferred tool for, well, everything. :blush:

Unfortunately, it’s a lot of Macros – more than 1000, many of them multi-line. And with so many items, I’d obviously like to avoid manual post-processing or one-by-one creation.

(I realize people may chime in and say “Stick with your other text expansion tool”, but I’d really like to put everything in KM. Simple one-step expansion without variables, cursor insertion etc. is good enough for me.)

So – if I prepare these text snippets e.g. in a spreadsheet and export them to a clean text file (tab oder comma-separated), with columns for…

  1. Macro name
  2. Macro shortcut (for “This string is typed”) trigger
  3. Macro (plain text) content (for action “Insert text by PASTING”)

…what would be the best approach for such a large batch-import into KM?

I don’t mind if they all end up in the same folder (actually, that would be preferred), I can take it from there.

Unfortunately, I don’t know any AppleScript, which would probably be helpful here.

(I also just saw that I had started a thread with similar questions back in 2014, but this was focussed on TextExpander, and it didn’t really go anywhere, so I’m not reviving it.)

Thank you.

I looked at your 2014 post out of curiosity and noticed that Peter Lewis had created/posted a macro to take a list of text expansions and automatically create KM macros out of them. His macro can be found here:

Is that what you’re looking for?

Thanks; I have missed this. Unfortunately, it’s not what I’m looking for. It seems this will allow creation of single macros in KM. I was looking for a way to batch-import a large set (CSV or tab just seem like good formats for this) that obviously would need to be parsed / split in some manner during import.

The macro actually creates a new text expansion macro for every line you supply in the variable Expansions; so if you have 600 lines in there it would create 600 separate macros. It places each of those in a macro group called Text Expansion. You'd know this already if you'd tried running Peter's macro.

Here's my version that allows you to specify the name of each text expansion macro that is created. Be sure to read the comment and edit the two red-coloured variables according to your needs.

Create Text Expansions.kmmacros (8.4 KB)

Click to see macro

Many thanks to @peternlewis

3 Likes

Nice.

Perhaps worth pointing out the every macro created will have "Exact case", "Diacriticals matter", and "Only after word break" set as trigger options. If that's a problem, @thatguyinberlin, you might be able to add extra columns to your CSV and alter the macro so it can pick and choose -- or edit @tiffle's macro to produce your most-required options so you can reduce the manual snagging required later.

3 Likes

That’s a good catch @Nige_S - and relatively easy to implement the changes if required.

1 Like

Thank you @tiffle, @Nige_S and obviously @peternlewis. I had only seen the preview of the original script and obviously mixed this up with a TextExpander-specific solution.

This is a good approach and a very nice, no-nonsense input format. I will have to think of something else for multiline snippets, but I may just create these manually.

2 Likes

I’m glad you mentioned that… Sadly I don’t have a suggestion for that :frowning_face:

What's the problem? I'm sure someone will have a solution...

Update: when you export a multiline expansion to a CSV, what does it look like? Knowing that you should be able to easily enhance the regular expression that is used to process those entries.

1 Like

I wanted to add to this Feb. 2023 thread to help the next guy who comes along and needs to import a few hundred (or thousand :blush:) text macros from a legacy tool into Keyboard Maestro.

So Peter N Lewis was kind enough to write a text import Macro in 2016, modified by tiffle, and I finally got around to migrating my 1250 macros (from aText, via Google Sheets for some cleanup work) to here.

It works, but there are a few caveats / things to consider:

  1. If the macros you import contain certain (XML) characters or strings, they need to be escaped, or there will be errors. In my case: “<” as “&lt”; “>” as “>”, “\r” as “\r”. I’m sure this can be added to the Macro itself, but I prepared my import file accordingly outside of KM.

  2. I also added the expansion character that I want to trigger every macro externally, so “kr#” expands to “Kind Regards” right away. Again, this might be added to the import routine itself.

  3. Multiline text expansion: I replaced every carriage return in the source with a placeholder (“@@@”) and replaced this by %Return% in the import For loop.

  4. I also added categories to the macro names (“Phone numbers: landline”), so it’s easier to retrieve/find them when I have forgotten a key combo.

  5. I have some very long snippets, so I chose “Insert text by pasting”. YMMV.

My KM file now has grown from 300 to 600 kB (not really a big deal in a time of terabyte SSDs), everything works fine, and now my too arsenal has shrunk from three to two (Keyboard Maestro and ObDev’s LaunchBar). Couldn’t be happier.

Thanks again to everyone who contributed.
text_macros_import.kmmacros (10.7 KB)

2 Likes

That’s a great outcome, Martin. And that you’ve shared it with everyone is very generous :grinning: