Difference between revisions of "macOS Find Default Application"
From Lazarus wiki
Jump to navigationJump to search (New macOS article) |
m (→Example code: Code tidy) |
||
Line 61: | Line 61: | ||
// Bundle Display Name (optional) | // Bundle Display Name (optional) | ||
defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleDisplayName').description)); | defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleDisplayName').description)); | ||
+ | |||
// Bundle Name (conventionally required) | // Bundle Name (conventionally required) | ||
if (defAppName = '') then | if (defAppName = '') then | ||
defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleName').description)); | defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleName').description)); | ||
+ | |||
// Cannot fail :-) | // Cannot fail :-) | ||
if (defAppName = '') then | if (defAppName = '') then | ||
Line 86: | Line 88: | ||
// Bundle Display Name (optional) | // Bundle Display Name (optional) | ||
defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleDisplayName').description)); | defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleDisplayName').description)); | ||
+ | |||
// Bundle Name (conventionally required) | // Bundle Name (conventionally required) | ||
if (defAppName = '') then | if (defAppName = '') then | ||
defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleName').description)); | defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleName').description)); | ||
+ | |||
// Cannot fail :-) | // Cannot fail :-) | ||
if (defAppName = '') then | if (defAppName = '') then |
Revision as of 07:10, 31 December 2020
This article applies to macOS only.
See also: Multiplatform Programming Guide
Overview
From time to time your application may need to determine the user's default application for opening certain content - for example, a web URL or a file system file. Example code which demonstrates how to do this is set out below. Note that these are obviously not the only two content for which you may wish to determine the user's default application, but the same general approach will apply for those other content types.
Example code
unit Unit1;
{$mode objfpc}{$H+}
{$modeswitch objectivec2}
interface
uses
Classes,
Forms,
Dialogs, // needed for ShowMessage()
StdCtrls, // needed for buttons
CocoaAll, // needed for Cocoa framework (origin the NeXTSTEP libraries)
CocoaUtils; // needed for NSStringToString()
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
myUrl: NSUrl;
defAppUrl: NSUrl;
defAppName: String;
begin
myURL := NSUrl.URLWithString(NSSTR('https://sentinel.sentry.org/'));
defAppUrl := NSWorkspace.sharedWorkspace.URLForApplicationToOpenURL(myURL);
// Bundle ID (required)
ShowMessage(NSStringToString(NSBundle.bundleWithURL(defAppUrl).bundleIdentifier));
// Bundle Display Name (optional)
defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleDisplayName').description));
// Bundle Name (conventionally required)
if (defAppName = '') then
defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleName').description));
// Cannot fail :-)
if (defAppName = '') then
defAppName := NSStringToString(defAppUrl.URLByDeletingPathExtension.lastPathComponent);
// Show result
ShowMessage(defAppName);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
myUrl: NSUrl;
defAppUrl: NSUrl;
defAppName: String;
begin
myURL := NSUrl.URLWithString(NSSTR('file:///Users/trev/my_defaults.txt'));
defAppUrl := NSWorkspace.sharedWorkspace.URLForApplicationToOpenURL(myURL);
// Bundle ID (required)
ShowMessage(NSStringToString(NSBundle.bundleWithURL(defAppUrl).bundleIdentifier));
// Bundle Display Name (optional)
defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleDisplayName').description));
// Bundle Name (conventionally required)
if (defAppName = '') then
defAppName := NSStringToString(NSBundle.bundleWithURL(defAppUrl).objectForInfoDictionaryKey(NSStr('CFBundleName').description));
// Cannot fail :-)
if (defAppName = '') then
defAppName := NSStringToString(defAppUrl.URLByDeletingPathExtension.lastPathComponent);
// Show result
ShowMessage(defAppName);
end;
end.