While working on Chatterbox I ran into number of problems, most of them caused by my limited knowledge of device-independent programming with Firemonkey. One particular challenge was copying data to the clipboard - something that is AFAIK available on all supported platforms. So I asked my friendly AI helpers:
"How can I copy text to clipboard in a Delphi Firemonkey application?"As always, all logs are available on GitHub.
[Claude]
Claude suggested two approaches. A simple one just uses the object Clipboard defined in the FMX.Platform unit.
uses FMX.Platform;procedure CopyToClipboard(const Text: string);
begin
Clipboard.AsText := Text;
end;
Unfortunately, this strange mix of VCL and Firemonkey doesn't work as there is no Clipboard object in the FMX.Platform unit.
The second suggestion was, however, more appropriate:
procedure CopyToClipboard(const Text: string);
var
ClipboardService: IFMXClipboardService;
begin
if TPlatformServices.Current.SupportsPlatformService(
IFMXClipboardService, ClipboardService) then
begin
ClipboardService.SetClipboard(Text);
end;
end;
This approach works very nicely indeed.
[Ollama]
Codellama is also convinced that Firemonkey framework works the same as VCL.
procedure TForm1.Button1Click(Sender: TObject);
var
Clipboard: TClipboard;
begin
Clipboard := TClipboard.Create;
try
// Copy some text to the clipboard
Clipboard.SetText('Hello, world!');
finally
Clipboard.Free;
end;
end;
I asked the AI where this TClipboard is defined and it answered:
The `TClipboard` class is defined in the `FMX.Platform` unit, which is part of the Firemonkey framework.
Not true, sorry.
[Gemini]
No false starts, it just gave the correct approach and added some suggestions on how to handle errors. The longer answer, however, mentioned a TSnackbar component so I asked it:
What is this TSnackbar component and where can I find it?
The answer was not very satisfying:
The `TSnackbar` component is located in the **FMX.SnackBars**
Sorry, not true. I complained and got back a longer piece of code that was supposed to emulate the "snackbar" functionality:
procedure TForm1.Button1Click(Sender: TObject);
begin
// ... your clipboard code ...
lblSnackbar.Text := 'Text copied to clipboard!';
lblSnackbar.Visible := True; // Make the label visible
// Animate the fade-in
TAnimator.AnimateFloat(lblSnackbar, 'Opacity', 1.0, 0.25);
// Schedule the fade-out after a delay
TThread.ForceQueue(
procedure
begin
Sleep(3000); // Wait for 3 seconds
TThread.Synchronize(nil,
procedure
begin
TAnimator.AnimateFloat(lblSnackbar, 'Opacity', 0.0, 0.25,
procedure
begin
lblSnackbar.Visible := False; // Hide the label after fade-out
end);
end
);
end
);
end;
I'll give the AI points for the general idea - animating a component's Opacity property - but the rest of the code was completely broken.
[OpenAI]
O1-mini started with a correct implementation and even provided a code that reads text from the clipboard (although no-one asked it to do that).
procedure TForm1.ButtonPasteClick(Sender: TObject);
var
ClipboardService: IFMXClipboardService;
ClipboardText: string;
begin
// Check if the clipboard service is available
if TPlatformServices.Current.SupportsPlatformService(
IFMXClipboardService, IInterface(ClipboardService)) then
begin
ClipboardService.GetClipboard(ClipboardText); // Get text from clipboard
Edit1.Text := ClipboardText; // Assuming you have an Edit control named Edit1
end
else
ShowMessage('Clipboard service not available.');
end;
After that, however, it has started hallucinating:
Sorry, but no. There is no TClipboard in FMX.Clipboard.If you're using a newer version of Delphi that includes the `FMX.Clipboard` unit, you can simplify clipboard operations using the `TClipboard` class.
[CoPilot]
To copy text to the clipboard in a Delphi Firemonkey application, you can use the IFMXClipboardService interface. Here's a simple example of how you can do this:procedure TFormMain.ButtonCopyClick(Sender: TObject);varClipboardService: IFMXClipboardService;beginif TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService) thenbeginClipboardService := IFMXClipboardService(
TPlatformServices.Current.GetPlatformService(IFMXClipboardService));ClipboardService.SetClipboard(Memo1.Text); // Replace Memo1.Text with the text you want to copyend;end;
In this example:
- Check if the platform supports the clipboard service using SupportsPlatformService.
- Get the clipboard service using GetPlatformService.
- Set the clipboard content using SetClipboard [1][2]
This code snippet assumes you have a TButton named ButtonCopy and a TMemo named Memo1 on your form. When the button is clicked, the text from Memo1 is copied to the clipboard.
Feel free to ask if you need more details or have any other questions![1] https://andydunkel.net/snippets/Delphi/Copy_Text_to_Clipboard/index.html[2] https://docwiki.embarcadero.com/CodeExamples/Sydney/en/FMX.CopyPaste_Sample
I tried to google, on what information answers may be based, and found this article:
ReplyDeletehttps://lecturepress.com/academy/object-pascal/using-clipboard-in-delphi/
What's interesting, both FMX.Clipboard and FMX.Platform are mentioned in this article, so that's where hallucinations may come from :-)
There's no TClipboard in that article, however. This part of hallucinations comes from the VCL world.
Delete