1

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.

11
  • 1
    Are you taking about the contents of multiple files? There is only one AppleScript clipboard. Commented Jan 20 at 15:19
  • @red_menace Finder can copy multiple file references to the clipboard from a Finder window. Commented Jan 20 at 16:41
  • This question feels a bit like an xyproblem.info Without some context as to the overall workflow it's difficult to advise as you seem to have settled on the need to program a solution. At present I'm leaning towards leaving the question open (currently flagged as off-topic software development), but I could imagine this question being closed for lack of detail re a specific problem unless OP is willing to provide more. Also, some information on why the linked solution didn't work for OP would be helpful. Commented Jan 20 at 18:22
  • If what you want is possible at all, there will be no getting around the fact that your script has to control the Finder from a shell, and that will inevitably trigger a prompt for authorization if the user hasn't already authorized it. There are plenty of ways to move files around in the shell that don't involve the Finder. Maybe you should modify the question, or ask a new one, focused on what you want to do rather than on how you want to do it. Commented Jan 20 at 19:36
  • 1
    You should add the information you have added to your duplicate question to this question, and close the duplicate. Don't take the comment re a limited supply of comments literally (they can be tidied up if required), I think Linc means that currently you've gone far enough in comments on this question such that further clarification of the actual problem to be solved is needed, and belongs in this question with an edit. Dripping amendments via comments is not a good approach to asking a good question. Neither is firing off a new question if the first doesn't get the expected response. Commented Jan 21 at 9:40

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.