I am trying to copy (using python or a CL command which I then can call using python) a file to the clipboard to later paste it using STRG+V. As far as I understand it, files are not "moved" into the clipboard, but rather the clipboard holds the path and an argument/flag that tells the OS "this is a file". I am happy with a linux-specific answer, but a universal answer would be the cherry on top.
pyperclip
Is not a solution, because it doesn't allow to copy files, just strings.
xclip
Is not a solution, because it only copies text
xclip-copyfile
Is not a solution, because it only copies to the X clipboard, not the clipboard. While xclip offers the option -selection clipboard (but only copies text), xclip-copyfile has no such option.
Using find
find ${PWD} -name "*.pdf"| xclip -i -selection clipboard -t text/uri-list
is a command described here: https://askubuntu.com/questions/210413/what-is-the-command-line-equivalent-of-copying-a-file-to-clipboard#answer-210428
But I can't replicate copying files with it and therefore assume that it is not working for all files.
Configurations
The clipboard is part of the Window Management and not of the Linux operating system itself. Different configurations with different distributions behave differently and therefore require different variants. Meanwhile, Wayland is increasingly on the way to successively replace X, which means there are three configurations to consider:
Sending clipboard content
When saving to the clipboard, the system first only informs the receiver that data is available for the clipboard. Only on request, the actual data is sent. The program that sends the content to the clipboard must therefore not be terminated before the data has been transferred. Depending on the environment/configuration, it is also possible that the content of the clipboard is deleted as soon as the program is terminated.
How then does the
xclipprogram already mentioned in the question work? It seems to terminate immediately after being called. But on closer inspection it doesn't, because it performs a fork, so that it is still present in the background (easily verifiable by looking at the source code or the commandps).Format
Furthermore, different environments require the content in different ways. For example GNOME requires the list of files to be copied with the special target
x-special/gnome-copied-filesand a special formatting of the content, e.g.copy\nfile:///etc/groupfor the GNOME file manager Nautilus to perform the copy operation correctly.Under KDE, on the other hand, there is then only one URI list with the target
text/uri-list.Determining the environment
The following example program works for Linuxmint 20.2 Cinnamon, Ubuntu 22.04 with Gnome and Kubuntu 22.04 with KDE. Other distributions / configurations may require some customization. Here it is advisable to simply copy a file in the appropriate file manager and then look at the clipboard contents with a program and then make appropriate adaptions to the script.
Based on the environment variables
XDG_CURRENT_DESKTOPandWAYLAND_DISPLAYthe following program tries to determine the environments.If it is Wayland,
wl-copyis used, otherwisexclipis used. The target and the content formatting is adapted accordingly. Withsubprocess.Popenthe tool is started and the content is sent tostdinof the tool.As soon as this is done, the program exits. Both
wl-copyandxclipthen create a fork, ensuring that the data is present in the clipboard.As mentioned above for other environments simply copy a file in the native file manager and then inspect the current clipboard contents and make appropriate adjustments to the script.
Depending on the environment,
xcliporwl-copy(install the packagewl-clipboardwith your package manager) must be there. Detailed information aboutwl-copycan be found here: https://github.com/bugaevc/wl-clipboard.Inspect Clipboard
Finally, to be able to dump the current contents of the clipboard, here is a small script that does just that. So it is possible to see what other programs like the native file manager put into the clipboard. Usually many programs put several different representations targets of the same data into the clipboard.