The Robservatory

Robservations on everything…


Count characters in Keyboard Maestro inputs via filters

Yesterday, I added an SSL certificate to my site, and posted a brief note about the change. In that post, I included an image…which turned out to be, well, the comments say it all:

Yes, I posted a non-https image in the ‘site is secure’ post. Sigh.

So I took Jonathan’s comment to heart, and created a Keyboard Maestro macro that ensures I post only relative URLs from now on.

Generally, I don’t think such a thing would be worth sharing, as it’s just a basic text replacement macro, right?. Mostly right, but in this case, I learned about a Keyboard Maestro feature that may be useful to others. So share I will…

The easy solution to this would be to create two new Keyboard Maestro text replacement macros with typed string triggers. When I start typing <a href="http://robs or <a href="https://robs, each macro would delete the http[s]: bit, and all would be done.

This would require two macros, though, and I’d also have to delete more characters than necessary (the <a href=" bit) and then add those characters back in (Keyboard Maestro can automatically delete all of the typed string, but not a portion of it). Also, deleting via keystrokes is slow, so the fewer characters to delete, the quicker the macro will run.

So down the rabbit hole I went. The end result is a single macro for both use cases, and it only deletes the necessary number of characters from either typed string. Here’s the final result; as it’s very specific to my use case, I don’t think it’s worth uploading.

The bit that’s of interest, and what initially had me stumped in trying to write this as one macro, was my need to delete a different number of characters for each text string: 11 for the http variant, 12 for https.

The key is Keyboard Maestro’s Filter Variable action. I’ve used this action before, most notably in my text transformation macro. I didn’t realize, however, that you could use this filter to actually transform a variable from text into a number.

Specifically, by setting the Filter Variable to filter with Character Count, I’m able to convert the captured input (%TriggerValue%, stored in theInput variable) into the number of characters in the captured string. This will either be 20 or 21, depending on whether I typed http or https.

Once the variable held the length of the string, the rest was pretty easy—though it requires knowing that the Repeat Actions action can use variables and math in its “how many times” box. Just start typing text, and the box will expand. In my case, I set it to repeat theInput-9 times, so it will delete everything except the <a href=" bit.

Once that’s done, I paste in the full relative URL (//; hooray, saved some typing!), then do some housekeeping (delete the pasted text from the clipboard and delete the variable.

Before you ask, yes, I do have other shortcuts for pasting things like movies and other bits of text; I’ve also updated those to incorporate the relative URLs.

1 Comment

Add a Comment

Leave a Reply

The Robservatory © 2017 Built from the Frontier theme