Recent

Author Topic: Copy to and from clipboard works on Windows and not on MacOS  (Read 1965 times)

daniel_sap

  • Full Member
  • ***
  • Posts: 115
Copy to and from clipboard works on Windows and not on MacOS
« on: March 13, 2025, 02:05:24 pm »
Hi
I have this code which copies stream data to and from clipboard.
It works fine on Windows, but not under MacOS.

Code: Pascal  [Select][+][-]
  1. procedure CopyStreamToClipboard(fmt: Cardinal; S: TStream);
  2. begin
  3.   Clipboard.Open;
  4.   try
  5.     Clipboard.AddFormat(fmt, S); // Директно използване на fmt за Windows
  6.   finally
  7.     Clipboard.Close;
  8.   end;
  9. end;
  10.  

Code: Pascal  [Select][+][-]
  1. function copyClipboardToStream(aFormat: Cardinal; aStream: TStream): Boolean;
  2. begin
  3.   Result := false;
  4.  
  5.   if not Assigned(aStream) then
  6.     exit;
  7.  
  8.   Clipboard.Open();
  9.   try
  10.     if not Clipboard.HasFormat(aFormat) then
  11.       exit;
  12.  
  13.     Result := Clipboard.GetFormat(aFormat, aStream);
  14.   finally
  15.     Clipboard.Close();
  16.   end;
  17.  
  18. end;
  19.  

Can you, advice, what will be the appropriate approach to handle the stream read write to clipboard on MacOS

Thanks

Lazarus 4.0RC1 (rev t-fixes-4-58-g43d904ed50) FPC 3.2.2 x86_64-win64-win32/win64
Lazarus 4.99 (rev main_4_99-590-g9b726c777f) FPC 3.2.2 aarch64-darwin-coca

daniel_sap

  • Full Member
  • ***
  • Posts: 115
Re: Copy to and from clipboard works on Windows and not on MacOS
« Reply #1 on: March 14, 2025, 05:20:19 pm »
The issue was that I pass Cardinal and it needs to be TClipboardFormat.
This code was ported from Delphi and not adjusted after that.

So, the copy and paste works perfect on Windows and MacOs

Here is the changed code, if someone came to such situation

Code: Pascal  [Select][+][-]
  1. function CopyStreamToClipboard(aFormat: TClipboardFormat; S: TStream): Boolean;
  2. begin
  3.   Clipboard.Open;
  4.   try
  5.     Result := Clipboard.AddFormat(aFormat, S);
  6.   finally
  7.     Clipboard.Close;
  8.   end;
  9. end;
  10.  
  11. function CopyClipboardToStream(aFormat: TClipboardFormat; aStream: TStream): Boolean;
  12. begin
  13.   Result := false;
  14.  
  15.   if not Assigned(aStream) then
  16.     exit;
  17.  
  18.   Clipboard.Open();
  19.   try
  20.     if not Clipboard.HasFormat(aFormat) then
  21.       exit;
  22.  
  23.     Result := Clipboard.GetFormat(aFormat, aStream);
  24.   finally
  25.     Clipboard.Close();
  26.   end;
  27.  
  28. end;
  29.  

jwdietrich

  • Hero Member
  • *****
  • Posts: 1272
    • formatio reticularis
Re: Copy to and from clipboard works on Windows and not on MacOS
« Reply #2 on: May 25, 2025, 10:14:43 pm »
Here is the changed code, if someone came to such situation

Thanks!
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 4.2.0 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

d2010

  • Sr. Member
  • ****
  • Posts: 253
Re: Copy to and from clipboard works on Windows and not on MacOS
« Reply #3 on: June 15, 2025, 11:34:52 am »
Code: [Select]
[quote author=daniel_sap link=topic=70509.msg549737#msg549737 date=1741871124]
Hi
I have this code which copies stream data to and from clipboard.
It works fine on Windows, but not under MacOS.

Inside MacoOs , for complex objects, You can execute pbcopy and pbpaste.
Are these still availble?

 

TinyPortal © 2005-2018