Howto add menu item to Mac OS Finder in Delphi XE2

后端 未结 2 1873
青春惊慌失措
青春惊慌失措 2021-01-07 16:51

I\'m working on Delphi XE2 application targetting Mac OS and Windows. And I want to have integration into context menu. For windows this is simple task. But for Mac OS I don

相关标签:
2条回答
  • 2021-01-07 17:03

    Finally, I returned to this project and successfully registered service provider and handled service request.

    First of all I tried to use NSRegisterServicesProvider method, but there is no such method in Macapi sources, so I searched for applicationDidFinishLaunching delegate. Using it I registered my service provider:

    procedure TApplicationDelegate.applicationDidFinishLaunching(Notification: Pointer);
    var
      autoReleasePool: NSAutoreleasePool;
      app: NSApplication;
      provider: TMessageProvider;
    begin
      autoReleasePool := TNSAutoreleasePool.Create;
      try
        autoReleasePool.init();
    
        app := TNSApplication.Wrap(TNSApplication.OCClass.sharedApplication);
    
        provider := TMessageProvider.Create();
        app.setServicesProvider(provider.ObjId);
      finally
        autoReleasePool.release();
      end;
    end;
    

    Also I have created interface for service provider (I think it is required for ObjectiveC-Delphi bridge work):

    IMessageProvider = interface(IObjectiveC)['{1EA9319A-8F99-4445-B435-48D5E73876FA}']
        procedure simpleMessage(pBoard: Pointer; userData: Pointer; error: PPointer); cdecl;
    end;
    

    and inherited TMessageProvider from this interface and TOCLocal class.

    After this my app can react to service request from context menu.

    I've shared sources of my project. Here they are.

    0 讨论(0)
  • 2021-01-07 17:18

    I see two potential problems

    1. You are allocating your own NSApplication object. I doubt that this is correct - doesn't Delphi create one internally also? And even if it doesn't, you'd probably need to enter the NSApplication's run method at some point to make it actually capable of handling messages.

    2. Service providers must be registeres in the applicationDidFinishLaunching: delegate method. You attempt to register it immediatly after creating your NSApplication instance.

    I think you can avoid both problems if you use NSRegisterServicesProvider(id provider, NSString *portName) to register your service provide, instead of using NSApplication's setServicesProvider:.

    0 讨论(0)
提交回复
热议问题