Recent

Author Topic: Copy a file to the clipboard under Linux  (Read 1097 times)

Tommi

  • Full Member
  • ***
  • Posts: 235
Copy a file to the clipboard under Linux
« on: March 10, 2025, 08:48:34 am »
I would like copy one or more files into clipboard like any file manager does.

I've found here in the forum : https://forum.lazarus.freepascal.org/index.php?topic=51601.0 an example but it isn't platform independent and overall it doesn't work under Linux.

Any example ?

Thank you

Thaddy

  • Hero Member
  • *****
  • Posts: 16802
  • Ceterum censeo Trump esse delendam
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Tommi

  • Full Member
  • ***
  • Posts: 235
Re: Copy a file to the clipboard under Linux
« Reply #2 on: March 10, 2025, 12:23:36 pm »
There isn't what I am looking for. In that link explain how to copy text or images, binary data. But I want copy a generic file path, so that I go into file manager and I do "paste" and I may paste it in a folder.

I tried copying a file path as text but it doesn't work. When I go into file manager "Paste" option remain disabled.

silvercoder70

  • Full Member
  • ***
  • Posts: 164
    • Tim Coates
Re: Copy a file to the clipboard under Linux
« Reply #3 on: March 10, 2025, 01:09:08 pm »
Are you wanting to copy the contents of the file? or filename alone?

Are you wanting do something with drag and drop?

Or?
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

Tommi

  • Full Member
  • ***
  • Posts: 235
Re: Copy a file to the clipboard under Linux
« Reply #4 on: March 10, 2025, 01:29:22 pm »
I'd like to do the same thing that does any file manager (Nautilus, Nemo, Caja etc.) when you select a file and you do Right Click --> Copy. Or when you select a file and you press Ctrl-C.

Thank you

Aruna

  • Hero Member
  • *****
  • Posts: 627
Re: Copy a file to the clipboard under Linux
« Reply #5 on: March 10, 2025, 01:45:54 pm »
I would like copy one or more files into clipboard like any file manager does.

I've found here in the forum : https://forum.lazarus.freepascal.org/index.php?topic=51601.0 an example but it isn't platform independent and overall it doesn't work under Linux.

Any example ?

Thank you
I have attached a fully working and tested demo in the zip file. Screenshot shows you the interface.

1. Click the Copy File To Clipboard button and select the file you want copied to the clipboard.
2 .Then click the Copy From Clipboard button and it will paste the clipboard contents into the Tmemo. This was tested on Linux Debian.

The use of Clipbrd should ensure that this code is platform-independent and it should work across different systems Lazarus supports (Windows, Linux, macOS). But I have not tested under Windows and macOS.

Tommi

  • Full Member
  • ***
  • Posts: 235
Re: Copy a file to the clipboard under Linux
« Reply #6 on: March 10, 2025, 03:13:24 pm »
Thank you Aruna, but it isn't what 'm looking for.

Quote
I'd like to do the same thing that does any file manager (Nautilus, Nemo, Caja etc.) when you select a file and you do Right Click --> Copy. Or when you select a file and you press Ctrl-C.

When you click copy on your file manager, it doesn't copy file content into clipboard. It copies something so that when you click "Paste" it copies the file. And this Copy/Paste mechanism is compatible between all applications. For example if I copy from file manager I can paste to Desktop.

If, after running your example I run this code:
Code: [Select]
    a:='';
    for b := 0 to Clipboard.FormatCount-1 do
    begin
      a:=a+IntToStr(Clipboard.Formats[b])+' ';
    end;
    edit1.text:=a;

I obtain this result:
145 142 144 130 129 31 128 71 16045690984833335023 16045690984833335023 17361641481138401520 599490849 130031689270592 130031693263680 130031720674088 0

If I go into Nemo file manager and I copy a file, i obtain this:
145 142 144 146 161 133 162 163 71 129 128 31 131 130 16045690984833335023 132612417 0 0 130031720674088 0 112 2863311530 130031736603386 130031736797452 130031736735121 130031736736408 130031738907378 130031738446880


So the two programs do something heavily different.

Alexx2000

  • New Member
  • *
  • Posts: 15
Re: Copy a file to the clipboard under Linux
« Reply #7 on: March 10, 2025, 04:09:38 pm »
See this unit.

Aruna

  • Hero Member
  • *****
  • Posts: 627
Re: Copy a file to the clipboard under Linux
« Reply #8 on: March 10, 2025, 04:54:24 pm »
Thank you Aruna, but it isn't what 'm looking for.
My apologies I misunderstood the requirements :)

When you click copy on your file manager, it doesn't copy file content into clipboard. It copies something so that when you click "Paste" it copies the file. And this Copy/Paste mechanism is compatible between all applications. For example if I copy from file manager I can paste to Desktop.

The attached screenshot is self-explanatory, and the attached zip file does exactly what you need. Right-clicking will bring up a menu, and you can also use the keyboard shortcuts Ctrl+C and Ctrl+V. Basic error checking is included, but I wouldn't rely on it in a real production environment. This should help you get started.

Tommi

  • Full Member
  • ***
  • Posts: 235
Re: Copy a file to the clipboard under Linux
« Reply #9 on: March 10, 2025, 10:14:57 pm »
@Aruna

Thank you Aruna, but your example doesn't accocomplish Linux standard because if I try it from a file manager, "paste" option is disabled. It works just inside itself.

@Alexx2000

Thank you, it works!
At least with Linux, I didn't tested under Windows but it should.
I attach my example just for reference for other forum members.

The code:
Code: [Select]
uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, uClipboard;
[...]
procedure TForm1.Button1Click(Sender: TObject);
var
  aa:TStringList;
begin
  if opendialog1.Execute then
  begin
    aa:=TStringList.Create;
    aa.Add(opendialog1.FileName);
    CopyToClipboard(aa);
    aa.Free;
  end;
end;

In attachment the unit uClipboard also.
« Last Edit: March 10, 2025, 10:18:41 pm by Tommi »

Aruna

  • Hero Member
  • *****
  • Posts: 627
Re: Copy a file to the clipboard under Linux
« Reply #10 on: March 11, 2025, 12:56:55 am »
@Aruna
Thank you Aruna, but your example doesn't accocomplish Linux standard because if I try it from a file manager, "paste" option is disabled. It works just inside itself.
Hi Tommi, it works just fine in my file manager as you will see here. I am puzzled about your statement : your example doesn't accocomplish Linux standard. Please do let me know how? Anyway I Am happy you have found your own solution.

dseligo

  • Hero Member
  • *****
  • Posts: 1500
Re: Copy a file to the clipboard under Linux
« Reply #11 on: March 11, 2025, 01:33:19 am »
Hi Tommi, it works just fine in my file manager as you will see here.

Try copying it in your program and paste it to Debian's file manager (and vice versa). That is what OP wants to accomplish.

Aruna

  • Hero Member
  • *****
  • Posts: 627
Re: Copy a file to the clipboard under Linux
« Reply #12 on: March 11, 2025, 01:40:10 am »
Hi Tommi, it works just fine in my file manager as you will see here.

Try copying it in your program and paste it to Debian's file manager (and vice versa). That is what OP wants to accomplish.
Ah.., Thank you dseligo now I understand. Let me check and get back to you.

dbannon

  • Hero Member
  • *****
  • Posts: 3308
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Copy a file to the clipboard under Linux
« Reply #13 on: March 11, 2025, 03:59:22 am »
Interesting.

When I copy a file (from, ie, your file manager) I see this in the clipboard -

Code: [Select]
...
------x-special/mate-copied-files------
copy
file:///home/dbannon/Pascal/CopyNPaste/unit1.pas

------text/uri-list------
file:///home/dbannon/Pascal/CopyNPaste/unit1.pas

------application/vnd.portal.filetransfer------
10641596561086006366

------application/vnd.portal.files------
3819048243777226885

------text/plain------
/home/dbannon/Pascal/CopyNPaste/unit1.pas

The content show for text/plain is, obviously, also shown for UTF8_STRING, COMPOUND_TEXT, TEXT, STRING, text/plain;charset=utf-8 but removed for clarity.

So, it looks to me, if you want to copy a file (by reference), you push its filename into a text/uri-list format slot (and maybe a list is needed, even for only one file). The other formats are probably ignored.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Tommi

  • Full Member
  • ***
  • Posts: 235
Re: Copy a file to the clipboard under Linux
« Reply #14 on: March 11, 2025, 10:30:11 pm »
If you look at uClipboard unit, the issue is more complex.
It distinguish between Gnome and KDE based desktop enviroments.

Code: [Select]
  gnomeClipboardMime = 'x-special/gnome-copied-files';
  kdeClipboardMime = 'application/x-kde-cutselection';

[...] //if Gnome (s is 'cut' or 'copy')
      s := s + LineEnding + uriList;
      Clipboard.AddFormat(CFU_GNOME_COPIED_FILES, s[1], Length(s));

[...] //if KDE  (s is '0' to copy or '1' to cut)
Clipboard.AddFormat(CFU_KDE_CUT_SELECTION, s[1], Length(s));

[...]
  // Common to all, plain text.
  Clipboard.AddFormat(PredefinedClipboardFormat(pcfText),
                      plainList[1], Length(plainList));
  // Send also as URI-list.
  if CFU_URI_LIST <> 0 then
    Clipboard.AddFormat(CFU_URI_LIST, uriList[1], Length(uriList));

Where uriList is a list of files where encoded following RFC-3986.
« Last Edit: March 11, 2025, 10:35:49 pm by Tommi »

 

TinyPortal © 2005-2018