![]() |
How to open URLs with default applications in macOS and Windows
I'm currently updating an old Delphi 5 Desktop VCL application to to Delphi 11.3 FMX. And one of the capabilities I want to provide is the ability to launch several webpages from within the application. I want to place a link in the main menu to my YouTube channel so customers can easily get to product videos. And there's also a link to my website in the Help > About box.
![]() ![]() It was fairly straightforward the last time I did this using VCL because all I had to worry about was the Windows side of things. However, because I want this application to run on both Windows and macOS it presented a challenge. The Delphi IDE won't recognize the Macapi namespace unless the target is set to MacOS 64-bit Harry Stahl covers the COCOA API on pages 98-99 of his book Cross-Platform Development with Delphi. He also gives an example of how to use the NSWorkspace object of Macapi.Appkit. However, he doesn't show how to setup the uses clause. I also found a fantastic reference on stackoverflow by David Heffernan that was written in 2015. However, there are two issues with Heffernan's if you are looking for a complete answer:
After a couple hours of going back and forth with code that worked for Windows but didn't work for macOS. And code that worked for macOS but didn't work for Windows, I finally got Heffernan's example to work. The next step was to extract the code out of the main form and place it into it's own unit. And that is the code I'm sharing with you today. I hope you find this helpful. Here is the source code: Unit1.fmx object Form1: TForm1 Left = 0 Top = 0 Caption = 'Goto Website' ClientHeight = 238 ClientWidth = 478 Position = ScreenCenter FormFactor.Width = 320 FormFactor.Height = 480 FormFactor.Devices = [Desktop] DesignerMasterStyle = 0 object Text1: TText AutoSize = True Cursor = crHandPoint Position.X = 116.000000000000000000 Position.Y = 83.000000000000000000 Size.Width = 246.078125000000000000 Size.Height = 31.921875000000000000 Size.PlatformDefault = False Text = 'https://zilchworks.com' TextSettings.Font.Size = 24.000000000000000000 TextSettings.FontColor = claMediumblue OnClick = Text1Click OnMouseEnter = Text1MouseEnter OnMouseLeave = Text1MouseLeave endend Unit1.pas unit Unit1;interfaceuses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects;type TForm1 = class(TForm) Text1: TText; procedure Text1MouseEnter(Sender: TObject); procedure Text1MouseLeave(Sender: TObject); procedure Text1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.fmx}uses u.OpenURL;{ TForm1 }procedure TForm1.Text1Click(Sender: TObject);begin GotoWebsite(Text1.Text);end;procedure TForm1.Text1MouseEnter(Sender: TObject);begin Text1.Font.Style := Text1.Font.Style + [TFontStyle.fsUnderline];end;procedure TForm1.Text1MouseLeave(Sender: TObject);begin Text1.Font.Style := Text1.Font.Style - [TFontStyle.fsUnderline];end;end. u.OpenUrl.pas {+-------------------------------------------------------------------------------¦ Filename: u.OpenURL Unit¦ Author: Michael J. Riley¦ Website: ![]() ![]() ![]() ![]() ![]() Enjoy! Gunny Mike ![]() ![]() |
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:03 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz