Improving a slow macro

Hi, I'm trying to make a macro that adds a markdown checkbox to the current line. I got something that works, but it’s pretty slow - about a second. I tried both shell script and AppleScript, but they’re about the same. I’m wondering if any of the actions I'm using are particularly slow and could be replaced.

Add checkbox.kmmacros (5.0 KB)

Is there any reason you are using the Named Clipboard?

It looks like you are selecting the line, then removing any (leading space, -, space), then pasting it back.

If so, this sequence should be all your need:

Shell scripts have some overhead, and AppleScripts/JavaScripts have a fair amount of overhead, so unless they are doing fairly complex things, they are likely to be slower than direct actions.

I used the named clipboard to not overwrite the main clipboard. I can’t type out the characters here because it’s being converted to a checkbox :laughing:, but it’s inserting a checkbox markdown at the beginning of the line. It’s stripping out the leading dash if present so I can convert a regular bullet to a checkbox.

This seems like a perfect place to use a Regular Expression using the "zero or one instance" operator, and KBM is very good (fast) with REs.

You can work around this by just deleting the newly-added clipboard entry when you're done with it:

https://wiki.keyboardmaestro.com/action/Delete_Past_Clipboard

You'd want to delete past clipboard #0, the most recent one.

-rob.

1 Like

Thanks for the suggestions! This is what I ended up with. I think now the initial keystroke to highlight the line is the slowest part, but it’s definitely better now :+1:

That will not work - there is only one way to copy or paste things on the Mac and that is via the system clipboard.

You can, but it is generally not recommended (at least by me) for the reasons described here (essentially because the system clipboard is synchronously changed, but the paste is done via a keyboard event that is queued, and thus deleting the clipboard you wish to paste can result in the previous clipboard being pasted).

1 Like

I should have been clearer, because that's an obvious concern: I delete the entry I added long after the paste event. I realize the clipboard could have been changed by myself or some other app in the interim, but that's not generally a concern with my macros, because when they're running, they're running and have full control of my Mac—so the only way the clipboard changes is if they change it.

I do have a small number of periodic and/or background macros, but none of them work with the clipboard.

-rob.

1 Like