AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Delphi-PRAXiS - Lounge Delphi-News aus aller Welt How to open URLs with default applications in macOS and Windows
Thema durchsuchen
Ansicht
Themen-Optionen

How to open URLs with default applications in macOS and Windows

Ein Thema von DP News-Robot · begonnen am 1. Jan 2024
Antwort Antwort
Benutzerbild von DP News-Robot
DP News-Robot

Registriert seit: 4. Jun 2010
15.392 Beiträge
 
#1

How to open URLs with default applications in macOS and Windows

  Alt 1. Jan 2024, 17:20
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:


  • There is a reference to a blog post by Malcolm Groves called Opening files and URLs in default applications in OS X which is no longer available or accessible.
  • The example doesn't tell you you need to target the MacOS 64-bit platform before the IDE will recognize Macapi namespace..
The Delphi IDE won't recognize the Macapi namespace unless the target is set to MacOS 64-bit. Shame on me for not reading up on the Embarcadero docs. Wrapping my head around how to use the {$IFDEF MSWindows} and the {$IFDEF MACOS} was a little tricky But I eventually caught on.


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: https://zilchworks.com¦ YouTube: https://www.youtube.com/@CapeCodGunny¦ Copyright: © 2023-2024 by Michael J. Riley. All Rights Reserved.+-------------------------------------------------------------------------------¦ Purpose: Opens a url using the default browser on the users system.¦¦ OS: Windows, macOS+-------------------------------------------------------------------------------¦ Developer References:¦¦ Book: Cross-Platform Development with Delphi 10.2 & FireMonkey¦ Author: Harry Stahl¦ ISBN: https://isbnsearch.org/isbn/9781549545764¦ Notes: See pages 98-99.¦¦ Websites: https://stackoverflow.com/q/28858392/195983¦ https://stackoverflow.com/a/28859000...-------------¦ DISCLAIMER:¦¦ This source code is provided "as is" and without any warranty. Use it at your¦ own risk. The author(s) make no guarantees or assurances regarding the¦ correctness or functionality of the code, and disclaim any liability for¦ damages resulting from its use.¦¦ It is advisable to thoroughly review and test the code before deploying it in¦ any production environment.¦¦ By using this code, you agree to these terms and acknowledge that any issues¦ arising from its use are solely your responsibility.+-------------------------------------------------------------------------------}unit u.OpenURL;interface{$IFDEF MSWindows}uses Winapi.ShellAPI, Winapi.Windows;{$ENDIF}{$IFDEF MACOS}uses Macapi.AppKit, Macapi.Foundation, Macapi.Helpers; procedure macOSGotoWebsite(URL: string);{$ENDIF} procedure GotoWebsite(URL: string);implementationprocedure GotoWebsite(URL: string);begin {$IFDEF MSWindows} ShellExecute(GetDesktopWindow, 'open', PChar(URL), '', '', SW_SHOWNORMAL) {$ENDIF} {$IFDEF MACOS} macOSGotoWebsite(URL); {$ENDIF}end;{$IFDEF MACOS}procedure macOSGotoWebsite(URL: string);var macURL: NSURL; macWorkspace: NSWorkspace;begin macURL := TNSURL.Wrap(TNSURL.OCClass.URLWithString(StrToNSSt r(URL))); macWorkspace := TNSWorkspace.Wrap(TNSWorkspace.OCClass.sharedWorks pace); macWorkspace.openURL(macURL);end;{$ENDIF}end.

Enjoy!
Gunny Mike
https://zilchworks.com











Weiterlesen...
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 09:28 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz