Named Clipboard Question

I use Philippe Martin’s Multiple Named Clipboards macro http://flipmartin.net/software/tips-and-tricks-for-keyboard-maestro#multiple-clipboards and it should only copy to the named clipboard.

For some reason, when I use it to copy and paste it also copies to the system clipboard. I’m not aware of having set up any macro for it to do this.

I’m assuming that something I’ve set in Keyboard Maestro is causing this, or maybe it’s something else.

Anyone got any idea how I can prevent this?

Hey Duncan,

You cannot prevent it. Keyboard Maestro has to use the System Clipboard as a gateway to get input into named clipboards.

You can save and restore the initial content of the System Clipboard in your macros though.

--
Best Regards,
Chris

Thanks Chris, presumably any clipboard app like ClipBuddy, ClipMenu etc has the same problem?

Not quite following what you mean here Chris. Could you give an example?

Hey Duncan,

Yes. All of those will have to use the System Clipboard to get data in/out of regular applications.

Although some utilities like Typinator will restore the current clipboard after expanding an abbreviation.

E.g. some utilities are better behaved than others.

Now then. Since you're writing this "utility" (macro) you're responsible for everything it does. If you want to preserve what's in the System Clipboard you have to manage it (and of course KM is flexible enough to do it).

Psudo-code:

Copy System Clipboard to tempClipboard
Copy selection to named clipboard clip1
Copy tempClipboard to System Clipboard
Set tempClipboard to "" (nothing - to save memory)

* There's a 'Copy Clipboard To Clipboard' Action.

Of course you could use a variable instead of a named clipboard for your temp-value.

You do the same basic steps when you paste back out of your named clipboards.

That's all pretty straightforward, but if you have any trouble let me know.

-ccs

Thanks Chris I’ll have a play with this.

Just to be clear, the reason Keyboard Maestro does not automatically preserve the clipboard automatically (it did, many versions ago when its facilities were far more limited) is because there is inevitably a race condition when you do this that results in it not being entirely robust in all cases (there is a wiki article that goes in to excruciating detail on this). Basically, the sequence:

  • Set Clipboard to A
  • Command-V
  • Set Clipboard to B

Because the Command-V is queued and processed by the application when it feels like it, there is no way to guarantee that B is not pasted. At a minimum you need some sort of delay after the Command-V, but there is no way to know how long to delay for. And if you delay long enough, then you may have the user copying something else before the Set Clipboard to B which has its own problems.

So because it cannot be done robustly in all cases and depends on the applications and their performance, Keyboard Maestro instead gives you the tools to do it as you need to, and you can adjust the timing and behaviour as appropriate to your Mac and your applications.

Thanks for the detailed explanation Peter.

Please clarify: are you saying that the solution is to use a delay or that there are ways in KM to paste without a ⌘V? Is the “Type the ⌘V Keystroke” action any different than “Type a Keystroke” and specifying ⌘V? Likewise for copying selected text.

There is no way in general to paste without using the System Clipboard.

There is no way to restore the clipboard without issues. Regardless of the length of the pause after simulating the ⌘V, either the pause is sometimes too short and the clipboard is restored before the paste happens (and so the wrong thing is pasted) or the pause is sometimes so long that the user has a chance to copy something else which is then overwritten by the restored clipboard.

The Paste action, and Type a Keystroke: ⌘V are pretty much functionally equivalent (the only difference is the delay after simulating the keystroke that Keyboard Maestro waits until it progresses - one uses the Copy/Paste delay, one uses the Simulate Command Keystroke delay).

The Copy action is different to just a Type a Keystroke: ⌘C in that it actually waits for the clipboard to change.

So, except in rare cases, do not restore the clipboard and use the Clipboard History as necessary, or restore the clipboard and either ensure you manually stop yourself long enough for things to happen, or accept the loss of robustness.