【问题标题】:Associate my app with its custom file type in OSX and open file on double click将我的应用程序与其在 OSX 中的自定义文件类型相关联并双击打开文件
【发布时间】:2018-03-06 08:14:31
【问题描述】:

我的 FMX 应用程序(比如 MyApp)使用自己的自定义文件类型来存储数据文件。假设这些文件的扩展名是 *.myext。

我已设法设置 info.plist,以便 OSX 将 MyApp 注册为文件类型 *.myext 的所有者。

如果 MyApp 尚未打开,它会在双击具有该扩展名的文件时打开。当然,文件没有打开,因为我没有编写任何代码来处理事件,因为我不知道如何在 OSX 中检测到事件已经发生。

如果 MyApp 已经打开,我会收到一条消息“无法打开文档“xxxx.myext”。 [MyApp] 无法打开“[MyApp 文件]”格式的文件。'

所以我的问题是 MyApp 如何知道文件已被双击以便启动文件打开程序?

【问题讨论】:

  • 您是否检查过您的应用是否在该事件中实际打开?如果是这样,将哪些参数传递给您的应用程序?也许您对它们的评价是错误的?此外,你读过这个delphihaven.wordpress.com/2012/08/12/… 吗?
  • @Sherlock70 应用程序无法打开,因为操作系统干预了我引用的错误消息。但从错误消息中可以清楚地看出,操作系统知道该应用程序与该扩展程序相关联。所以,我相信我已经通过 info.plist 正确地完成了文件关联。是的,我知道有关此主题的 DelphiHaven 文章。我下载了那些文章中引用的源代码并尝试运行它,但收到了多条错误消息。代码是为 XE2 编写的,我无法让它在 10.2 中运行。

标签: macos firemonkey


【解决方案1】:

我最终回答了我自己的问题。三位作者各自给出了包含大部分必要代码的非常相似的单元。

克里斯·罗利斯顿:https://delphihaven.wordpress.com/2012/08/14/associating-a-file-type-on-osx-part3/

维克多·费多伦科夫:https://pastebin.com/r4y6KmWz

雷米勒博:https://forums.embarcadero.com/thread.jspa?messageID=934522

这三个单位都不能在 Delphi Tokyo 10.2 中未经修改就可以使用,这无疑是由于自写帖子以来 Firemonkey 中发生的变化。

我对这三个版本中的每一个都进行了部分编辑,并进行了各种编辑以得到下面的单元,该单元适用于 Delphi 10.2 Update 3:

unit NSApplicationOpenFileDelegateUnit.Mac;

interface

type

  TOpenFileEvent = reference to procedure(const AFileName: string);

procedure InstallApplicationDelegate2(const AOnOpenFile: TOpenFileEvent);

implementation

uses
  System.SysUtils, System.RTLConsts, System.Messaging, System.Classes,
  Macapi.ObjectiveC, Macapi.CoreFoundation, Macapi.CocoaTypes, Macapi.AppKit,
  Macapi.Foundation, FMX.Forms,
  Macapi.ObjCRuntime,
  FMX.Platform, FMX.Platform.Mac, FMX.Helpers.Mac;

type
  NSApplicationDelegate2 = interface(NSApplicationDelegate)
    ['{BE9AEDB7-80AC-49B1-8921-F226CC9310F4}']
    function application(theApplication: Pointer; openFile: CFStringRef)
      : Boolean; cdecl;
  end;

  TNSApplicationDelegate2 = class(TOCLocal, NSApplicationDelegate2)
  private
    FOnOpenFile: TOpenFileEvent;
  public
    constructor Create(const AOnOpenFile: TOpenFileEvent);
    procedure applicationDidFinishLaunching(Notification
      : NSNotification); cdecl;
    procedure applicationDidHide(Notification: NSNotification); cdecl;
    procedure applicationDidUnhide(Notification: NSNotification); cdecl;
    function applicationShouldTerminate(Notification: NSNotification)
      : NSInteger; cdecl;
    function applicationDockMenu(sender: NSApplication): NSMenu; cdecl;
    procedure applicationWillTerminate(Notification: NSNotification); cdecl;
    function application(theApplication: Pointer; openFile: CFStringRef)
      : Boolean; cdecl;
  end;

constructor TNSApplicationDelegate2.Create(const AOnOpenFile: TOpenFileEvent);
begin
  inherited Create;
  FOnOpenFile := AOnOpenFile;
end;

procedure TNSApplicationDelegate2.applicationDidFinishLaunching
  (Notification: NSNotification); cdecl;
begin
  // Seems we have to have this method even though it is empty.
end;

procedure TNSApplicationDelegate2.applicationDidHide(Notification
  : NSNotification); cdecl;
begin
  // Seems we have to have this method even though it is empty.
end;

procedure TNSApplicationDelegate2.applicationDidUnhide
  (Notification: NSNotification); cdecl;
begin
  // Seems we have to have this method even though it is empty.
end;

function TNSApplicationDelegate2.applicationShouldTerminate
  (Notification: NSNotification): NSInteger; cdecl;
begin
  Result := NSTerminateNow;
end;

function TNSApplicationDelegate2.applicationDockMenu(sender: NSApplication)
  : NSMenu; cdecl;
begin
  Result := nil;
end;

procedure TNSApplicationDelegate2.applicationWillTerminate
  (Notification: NSNotification); cdecl;
begin
  FreeAndNil(FMX.Forms.application);
end;

function TNSApplicationDelegate2.application(theApplication: Pointer;
  openFile: CFStringRef): Boolean; cdecl;
var
  Range: CFRange;
  S: String;
begin
  Result := Assigned(FOnOpenFile);
  if not Result then
    Exit;
  Range.location := 0;
  Range.length := CFStringGetLength(openFile);
  SetLength(S, Range.length);
  CFStringGetCharacters(openFile, Range, PChar(S));
  try
    FOnOpenFile(S);
  except
    on E: Exception do
    begin
      FMX.Forms.application.HandleException(E);
      Result := False;
    end;
  end;
end;

var
  Delegate: NSApplicationDelegate2;

procedure InstallApplicationDelegate2(const AOnOpenFile: TOpenFileEvent);
var
  NSApp: NSApplication;
begin
  NSApp := TNSApplication.Wrap(TNSApplication.OCClass.sharedApplication);
  Delegate := TNSApplicationDelegate2.Create(AOnOpenFile);
  NSApp.setDelegate(Delegate);
end;

end.

要使用该单元,请将此行包含在主窗体的 FormCreate 中:

InstallApplicationDelegate2(OpenFile);

还包括一个文件打开过程

procedure TMyMainForm.OpenFile(const AFileName: String);
begin
// Application-specific code to open the file
end;

现在在 Finder 中双击文件会在应用程序中打开该文件。

以上假设您的应用程序和您的专有文件扩展名已经通过 info.plist 文件关联。以下是将文件扩展名“.myext”与应用程序 MyApp 关联的 info.plist。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleName</key>
    <string>My App Full Name</string>
    <key>CFBundleDisplayName</key>
    <string>My App Full Name</string>
    <key>CFBundleIdentifier</key>
    <string>com.mycompany.MyAppName</string>
    <key>CFBundleVersion</key>
    <string>1.0.6662</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleAllowMixedLocalizations</key>
    <string>YES</string>
    <key>CFBundleExecutable</key>
    <string>MyAppName</string>
    <key>NSHighResolutionCapable</key>
    <string>true</string>
    <key>LSApplicationCategoryType</key>
    <string>public.app-category.productivity</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>The reason for accessing the location information of the 
    user</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>The reason for accessing the location information of the 
    user</string>
    <key>NSContactsUsageDescription</key>
    <string>The reason for accessing the contacts</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0.0</string>

    <key>CFBundleIconFile</key>
    <string>MyAppName.icns</string>
    <key>CFBundleSupportedPlatforms</key>
    <array>
        <string>MacOSX</string>
    </array>

<key>CFBundleDocumentTypes</key>
    <array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.mycompany.MyAppName.myext</string>
        </array>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>CFBundleTypeIconFile</key>
        <string>MyAppName.icns</string>
    </dict>
    </array>
    <key>UTExportedTypeDeclarations</key>
    <array>
    <dict>
        <key>UTTypeIdentifier</key>
        <string>com.mycompany.MyAppName.myext</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>myext</string>
            </array>
        </dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.data</string>
        </array>
        <key>UTTypeDescription</key>
        <string>My App Full Name document</string>
        <key>UTTypeIconFile</key>
        <string>MyAppName.icns</string>
    </dict>
    </array>
</dict>
</plist>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 2012-01-29
    相关资源
    最近更新 更多