I don’t know how that would happen either, but you might like to add a Semaphore Lock action to the start of the macro to ensure the macro always run synchronously - if you press Command-A a second time it will wait for the first macro instance to finish before starting the second.
By the way, a variant I did of this was for a Copy to Markdown macro, where pressing the trigger a second time copied it in a different style. It did this simply by:
set variable First to whatever the first style is
set variable Second to whatever the second style is
Thanks for the tip. I already played around with combinations of Semaphore Lock and Disable/Enable Macro. I threw the semaphore lock out because it seemed to change nothing. Maybe I’ll give it another try.
Anyway, this isn’t grave since in normal usage I won’t trigger the macro in rapid sequences. (And the macro trigger mode “is pressed” prevents a repetition while the keys are still down.)
If any of the actions fail and abort the macro, then the Enable will never happen.
The Copy action will fail if there is no contents (ie, even after Select All there is no selection). In that case the macro would be aborted and the Enable will not happen. You could probably safely put the Enable before the If action, after the delay which should be long enough to ensure the Command-A does not cause the macro to recur.
Every once in awhile I wish KM had a “try/finally” or even a full-blown “try/catch/finally”. Or at least a method to say “Do this when exiting the macro, even if something fails or a “Cancel” is invoked”.
Yes, I already had tried it on that position, too, but could achieve to end up with the macro disabled equally. However, today I can not reproduce it at all, no matter what position and with or w/o Semaphore Lock. The last disabled macro I got was yesterday (with a Semaphore Lock). But there was a KM engine restart in-between, so maybe it’s this…
In any case, you are right with the empty selection (I didn’t test it against this), so it’s certainly preferable to place the Enable Macro before the If Then. I’ll change the screenshot and the macro above. Thanks.
BTW, each time the macro runs (without failure) I get this in the engine.log:
I am not sure I completely follow why would it get tied up in anything else because if you short press command+a it never even makes it to the second half of the macro because “a” would not be down. I just ran it many times and it seemed to work just fine and would select all.
That’s an interesting observation, and I can confirm it.
It’s mysterious to me why it works now even without the Disable action. I’m pretty sure that at the time I wrote the macro the “Type the ⌘A” action did not select without a preceding Disable action, for the described reason.
Well, fine, removing the Disable and Enable actions enhances the reliability of the macro, since the Enable action could fail in some occasions.
Hi @peternlewis - I saw a reference to your Copy to Markdown macro. In fact, I am trying to see if there exists a Copy to Markdown macro similar to what this CopyAsMarkdown extension Popclip extension enables.
It seems like you may have created one. Can I request you to share that? Thank you!
I’m afraid mine is nothing like Popclip, it is simply a hot key triggered macro to convert the current page (title / url) to a markdown. Then if I press the link again, it sees the clipboard already has the desired answer, and so puts it in a different format. So I can hit the hot key once for markdown (for the forum) and a second time for plain text markup (for emailing).
@peternlewis Got it. Thanks for the quick reply. I will make a separate post about the copy to markdown possibility. Please help there if you can.
Re the macro that you are referring to - that sounds very interesting also. I already have the copy-link-as-markdown macro, but when I need to send a link in mail, I need to copy from markdown editor into a rich text format… So if you can share your macro, that would be great! Thanks.
Here is the macro. The exact details of what it does are not particularly important, because it creates four different styles of links, and several of them are fairly specific to me (related to working in the Keyboard Maestro wiki). But the implementation of the technique is useful, in particular:
It starts with a Semaphore Lock, so you can press the hot key as far as you want, the macro will execute to completion before starting the next instance.
It creates four different variants of the desired pasted text, and stores each in variables Out1, Out2, Out3, Out4.
Then it looks at the current clipboard - if it is already equal to the value of Out1, then it sets it to Out2. Otherwise, if it is equal to the value of Out2 already, then it sets it to Out3, and if it is equal to Out3, then it sets it to Out4, and otherwise it sets it to the first format, Out1. So the clipboard itself stores the state for how many times the hot key was pressed, and all four variants are created each time, but only one is placed on the clipboard.
@peternlewis Thanks for sharing your macro. I can say for myself that when I copy a link in a markdown file vs when I copy it in Mail.app - this can be used and the semaphore lock idea is powerful to understand for other hot key based macros as well.