Skip to content

Fix macOS multi-file copy to clipboard (#234) - #251

Open
happyTonakai wants to merge 2 commits into
Slackadays:mainfrom
happyTonakai:fix/macos-multi-file-copy
Open

Fix macOS multi-file copy to clipboard (#234)#251
happyTonakai wants to merge 2 commits into
Slackadays:mainfrom
happyTonakai:fix/macos-multi-file-copy

Conversation

@happyTonakai

Copy link
Copy Markdown

Summary

  • Fix inability to copy multiple files to clipboard on macOS by reading back from NSPasteboard after writing (Cannot copy multiple files to clipboard 0 on macOS #234)
  • Fix cb paste outputting text instead of files when external apps overwrite the system clipboard between copy and paste

Problem 1: Multiple files not preserved on macOS

macOS has a bug where NSPasteboard.writeObjects: only retains the last item when writing multiple file URLs. This caused cb copy file1 file2 file3 to only preserve one file on the system clipboard.

Fix: Read back from the pasteboard immediately after writing with readObjectsForClasses:, which forces macOS to internalize all written items.

The fix is based on a workaround shared by timhansinger in #234, who encountered the same macOS pasteboard bug in their own tool pbcp and solved it with the same read-back approach (implemented in Swift). The technique translates directly to our Objective-C++ code since both use the same NSPasteboard API.

Problem 2: cb paste outputs text instead of files

While testing Problem 1 over SSH, we discovered that cb paste would output shell command text (from the terminal's OSC 52 response) instead of the copied files. The root cause: syncWithExternalClipboards() was called at the start of every cb invocation, including paste/show. It would read whatever was on the system clipboard (which other apps like the terminal may have overwritten) and write it as a new clipboard entry via convertFromGUIClipboard(), effectively replacing the file entry with a text entry.

Fix: Skip calling convertFromGUIClipboard() for non-write actions (paste, show, etc.) since these operations should only read from internal storage, not ingest whatever is currently on the system clipboard.

…write on paste

Two fixes for Slackadays#234:

1. Read back from NSPasteboard after writing multiple file URLs to work
   around a macOS bug where only the last item persists otherwise.

2. Skip writing GUI clipboard content into internal storage for non-write
   actions (paste, show, etc.) to prevent external apps from corrupting
   the clipboard entry between copy and paste operations.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mohammadazeemwani

mohammadazeemwani commented Apr 21, 2026

Copy link
Copy Markdown

I did build it from source and still only one file is getting copied to my system clipboard.

image
  • can you assist my why Ctrl+y is not working for me on mac ?
    Here is my keymap.toml:
[[manager.prepend_keymap]]
on  = [ "<C-y>" ]
run = [ "plugin system-clipboard" ]
desc = "Copy selected files to system clipboard"

^^ this is the only thing in my keymap.toml
Edit, I got the keymap working

@happyTonakai

happyTonakai commented Apr 21, 2026

Copy link
Copy Markdown
Author

@mohammadazeemwani hi, this did work for me on both of my two macbooks:
image
are you sure that which cb shows the binary which is just you built? or you can run path/to/cb copy

@mohammadazeemwani

Copy link
Copy Markdown

@happyTonakai
image
I built from source using your fork..

OS: macOS Tahoe 26.3.1 arm64
Host: MacBook Air (M2, 2022)
Kernel: Darwin 25.3.0

@happyTonakai

Copy link
Copy Markdown
Author

I haven’t tested on macOS 26 and don't have a device for it, so this is probably an OS compatibility issue. Maybe some behavior is not the same on macOS 26.

Replace the readObjectsForClasses workaround with NSPasteboardItem,
which explicitly sets NSPasteboardTypeFileURL for each file. This
avoids the immediate read-back hack that may trigger macOS 26's
new pasteboard privacy protections.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@happyTonakai

Copy link
Copy Markdown
Author

@mohammadazeemwani I think I found the cause.

The previous fix used readObjectsForClasses right after writing to force the system to persist multiple files — but macOS 26's new pasteboard privacy protections likely block this.

I've replaced it with NSPasteboardItem, which explicitly sets NSPasteboardTypeFileURL for each file. No more read-back hack needed.

Could you pull the latest and test again?

@mohammadazeemwani

mohammadazeemwani commented Apr 21, 2026

Copy link
Copy Markdown

I build the project again after pulling latest changes and results still the same:

image

I am on this commit: 3a0ecd1

@mohammadazeemwani

mohammadazeemwani commented Apr 21, 2026

Copy link
Copy Markdown

My bad man I guess I am on wrong branch.

It works now

image

Kudos to you.

But for some strange reasons, it only pastes both using cb paste,
If for example, I go in finder and use ⌘ + v it only pastes the top one in the cb show list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants