Select All and Copy

I think I’m too ingrained in how I do things to use this particular macro, but the idea of having a macro do something additional when holding down a hotkey longer is fascinating.

I’ll think about that and see if it can be harnessed somewhere in my arsenal. Thanks!

Wow, this is amazing. I really love these simple and very useful macros that simply extend the functionality of what is already there. Thank you for sharing it.

1 Like

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
  • if clipboard is not First
    • set clipboard to First
  • else
    • set clipboard to Second
2 Likes

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.

1 Like

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".

1 Like

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:

Assertion Failed: gCurrentExecuteActionListEntry,  file: /Users/peter/Keyboard Maestro/Project/Source/Actions/HExecuteActionListManager.mm:1215, value: 0

I get this line once when the If Then condition is false (short key press), and I get the line twice when the If Then is true (long key press). A path “/Users/peter” does not exist on my machine.

--
Model Identifier: Macmini6,2
System Version: OS X 10.11.5 (15F34)
KM: 7.2

BTW, each time the macro runs (without failure) I get this in the engine.log:

Assertion Failed: gCurrentExecuteActionListEntry,  file: /Users/peter/Keyboard Maestro/Project/Source/Actions/HExecuteActionListManager.mm:1215, value: 0

Peter says this about that, in another thread:

1 Like

Ah, ok, thanks for the link. I suspected it was unrelated. (I had a similar “file: /Users/peter/”-style error with another macro last year.)

1 Like

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.

1 Like

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.

Thanks for letting me know!

Update (2016-09-02)

Removed the Disable and Enable actions. The macro seems to work perfectly fine without them. (Thanks to @skillet for the hint.)

If you encounter problems with the new version please try the previous version which is still downloadable from the first post.

Select All and Copy.kmmacros (3.9 KB)

3 Likes

I have an idea for how to solve the problem of the “Enable” occasionally not getting executed. I know you took it out, but I still think this is worthwhile to discuss.

My thought was to create a second macro, that is normally disabled. This macro, if it were enabled, would trigger once every second (or whatever time seems appropriate). All this macro would do is:

  1. Disable itself.
  2. Enable your original macro.

So, your original macro would work like this:

  1. Enable the timer macro.
  2. Disable itself.
  3. Do whatever…

Your original macro would never need to re-enable itself, because the “timer” macro would do that when it fired.

Does that make sense?

Not sure if that matters, since the Enable/Disable actions apply to the original macro anyway (not to the running copy).

But possible that some quirks can be avoided with your method.

Yeah, I was just thinking about future situations.

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).

But the process is nothing like Popclip.

@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. :smile:

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.

Copy Topic in MarkDown.kmmacros (13 KB)

@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.