Similar question but doesn't work: How do I copy multiple files to macOS's clipboard programmatically?
I'm searching for a way to add files to the system clipboard for macOS.
Context: I'm developing a Python TUI for managing files, similar to yazi and superfile, and I'm wanting to create a feature that copies a given selected files to the system clipboard, and then can be pasted anywhere (Finder, Discord, etc)
Based on my searches, most have said that the following command:
osascript -e 'set the clipboard to {POSIX file "/path/to/file1", POSIX file "/path/to/file2", POSIX file "/path/to/file3"}'
This seemed to have worked for them.
Taking a deeper dive resulted in some mentioning that the above command means macOS will set the clipboard to the literal list, not a list of files.
An alternative is to use something like gh:neilberman/clippy to copy to the system clipboard. This requires the end user to have either of these binaries on the system, which I don't want.
Another alternative is mentioned at Copying files to the clipboard using applescript
set f to {(POSIX file "/path/to/a/folder/a.png"), (POSIX file "/path/to/another/folder/b.png")}
tell application "Finder"
try -- to delete any old temp folder
delete folder "AS_mailCopy" of (path to temporary items)
end try
set tmp to make new folder at (path to temporary items) with properties {name:"AS_mailCopy"}
duplicate f to tmp
select files of tmp
activate
tell application "System Events" to keystroke "c" using command down
delete tmp
end tell
This feels janky, because when the command runs, it prompts the user whether they want the terminal to access Finder, then asks again for sending keystrokes (which is a restricted permission, and asks you to enable it in the settings). This poses quite a fatal amount of vulnerabilities (because once enabled, anyone can do whatever they want, because system interaction is enabled).
A final solution is directly using pyobjc to interact with the NPasteBoard class. Problem is that it is 100 megabytes big, I don't think that is worth it, even if I can install the module conditionally based on the system type.
TLDR: Searching for a way to copy multiple files to macOS's clipboard via the CLI but existing solutions either don't work, or are janky.