Inconsistent behavior when copying system clipboard to named clipboard

I am seeing strange inconsistencies when trying to copy the system clipboard containing an image to a named clipboard.

The sequence:

  • Copy an image (manually) with ⌃C
  • Wait a long time
  • Execute KM macro with a single action: Copy System Clipboard to Named Clipboard
  • Try to paste from the named clipboard.

The result:

  • The original copy to the system clipboard always succeeds. When I examine the contents of the system clipboard, the image is there.
  • When I copy to the named clipboard, sometimes the behavior is as expected; the copied image ends up in the named clipboard.
  • But sometimes the named clipboard is garbage. The value inspector describes it as a "Non-Text Clipboard," and future operations using the clipboard fail.
  • The source and content of the image matter. I have not seen a failure with an image copied from Preview. I am very likely to see a failure with an image copied from LiquidText, although sometimes it succeeds.

Best to demonstrate with examples:

Value inspector showing both the System Clipboard and a clipboard named copylinkCopiedContent after using ⌘C to copy an image from a display in Preview.

The macro that I used to copy the System Clipboard to copylinkCopiedContent.

Value Inspector showing the contents of both clipboard after executing the macro.

Value Inspector showing the contents of both the System Clipboard and the named clipboard after using ⌘C to copy an image from LiquidText.
The System Clipboard looks fine here. When I look at clipboard info in AppleScript it looks like a perfectly normal clipboard. And when I paste the clipboard using ⌘V the image pastes perfectly.

But when I execute the macro to copy the System Clipboard to the named clipboard, Keyboard Maestro sees the named clipboard as garbage.

I have tried a number of obvious things, like clearing out the previous image in the named clipboard before doing the copy. I have not found anything that helps with the problem. I am also giving the system plenty of time between actions; sometimes timing matters with the clipboard.

I have not tried this out with a bunch of different applications. But this one demonstrates a circumstance where a System Clipboard that looks right in the KM Value Inspector and behaves correctly in all other respects cannot be copied to a named clipboard by Keyboard Maestro.

@peternlewis , this one is probably for you. I understand that Mac clipboards (i.e., Cocoa NSPasteboard objects) are complicated beasts. Any thoughts?

Keyboard Maestro Version 10.0.2, reported as current.
Running macOS Monterey Version 12.3.1, also current.

Its hard to say what the cause might be. It might be that it is caused by one or other of the various flavors that are actively ignored by Keyboard Maestro because reading them causes problems.

There was an app called ClipboardViewer that shows the various flavors on the clipboard, and you could use something like that to see what is on the system clipboard, and then what is on the system clipboard after you copy the clipboard to the named clipboard and then from there to the system clipboard.

One option is in Apple's Additional tools for XCode
https://developer.apple.com/download/all/?q=clipboard%20viewer

(Free to download if you sign in with an Apple ID)

1 Like

I’ll poke around a bit more and see what I find. I am curious that the KM value inspector sees the named clipboard properly, but the copy clipboard action does not. Clearly there is some strangeness.

I wonder if you could share the Cocoa methods that you use to get the data to display and the copy operation. I am far from a Cocoa whiz, but might write a bit of code and see what I can pull out.

Hey Peter,

I looked at the pasteboard with a bit of AppleScriptObjC:

use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions

set thePasteboard to current application's NSPasteboard's generalPasteboard()
set theItems to thePasteboard's pasteboardItems()
set theTypes to (item 1 of theItems)'s |types|()

The following pasteboard is properly viewed and copied by Keyboard Maestro:

This pasteboard can be viewed by KM's Value Inspector, but the Copy Clipboard to Clipboard action fails:

There are some obvious differences. Is there any more information I can provide that would help?

1 Like

You may see some of the basics for reading NSPasteboard.generalPasteboard items, of various types, in this macro:

Clipboard Viewer - Macro Library - Keyboard Maestro Discourse

1 Like

Great minds! :wink:

1 Like

The clipboard reading code I use is incredibly complex, not the sort of thing you would want for just looking at the clipboard.

1 Like

Understood. I've been asked the equivalent of that question more than a few times in my life, and can hear the groan from here.

1 Like

I would hardly call this an elegant solution, but it works:

# Takes a complicated clipboard containing an image and turns it
# into a simple clipboard containing only an image.

# Make a temporary file
set filePath to do shell script "echo `mktemp /tmp/clipboard.XXXXX`"

# Get the image out of the clipboard and write it to the tmp file
set theImage to the clipboard as {«class PNGf»}
set openedFile to open for access filePath with write permission
write theImage to openedFile
close access openedFile

# Reopen the temporary file and read the image into the clipboard
set openedFile to open for access filePath
set the clipboard to (read openedFile as {«class PNGf»})
close access openedFile

# Delete the temporary file
do shell script "rm " & filePath
1 Like