Recent

Author Topic: How to implement "Open With" support on OSX?  (Read 9805 times)

havezet

  • New Member
  • *
  • Posts: 26
How to implement "Open With" support on OSX?
« on: October 31, 2010, 12:57:33 pm »
I'm new to Mac programming and come from Delphi on Windows.

In Windows, when you open a document using the "open with" interface from Windows Explorer then the selected files are passed via command line parameters and can be accessed in the application by querying the ParamStr.

In Mac this seems to work differently and I can't find out how I can access the files that were selected when my application was selected as the "open with" application in Finder.

Any pointers that could help me in the right direction?

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: How to implement "Open With" support on OSX?
« Reply #1 on: October 31, 2010, 02:51:32 pm »
If I understand you correctly, I think you can do it by adding some conditional (Mac-only) code to your main form. For example:

In your declaration of event handlers:

{$IFDEF DARWIN}
    procedure DropFiles(      Sender   : TObject;
                        const FileNames: array of string);
{$ENDIF}                       

Then in FormCreate or some other startup routine:

{$IFDEF DARWIN}
  OnDropFiles := DropFiles;
{$ENDIF}

Then the implementation. (In this example, LoadFile and SetFormData are additional methods specific to my app.)

{$IFDEF DARWIN}
procedure TMainForm.DropFiles(      Sender    : TObject;
                              const FileNames : array of string);
 {When start app by double-clicking a file,
   file name passed to this event handler.}
begin
  if High(FileNames) >= 0 then  {At least one file passed?}
    begin
    if LoadFile(FileNames[0]) then  {Opened first file okay?}
      SetFormData;
    end;
end;
{$ENDIF}                       


One other thing you can do to activate the ability of Finder to start your app and "drop" a double-clicked file on it (thereby opening the file), is to set the CFBundleDocumentTypes key in your app bundle's (.app folder) Info.plist file, thereby associating a document type (extension) with your app. Let me know if you have questions about this or see Apple's docs:

http://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009247

Thanks.

-Phil

havezet

  • New Member
  • *
  • Posts: 26
Re: How to implement "Open With" support on OSX?
« Reply #2 on: November 01, 2010, 07:33:46 am »
Hi Phil,

That was exactly what I needed. Works great.

Thank you

Hert
« Last Edit: November 01, 2010, 08:27:21 am by havezet »

mdkass

  • New member
  • *
  • Posts: 8
Re: How to implement "Open With" support on OSX?
« Reply #3 on: January 14, 2011, 09:26:23 pm »
Hello,

Double clicking and opening the file with this method is working only if the application is already opened, what if I want the application to run when double clicking the file then open the double clicked file?

Double clicking the associated file is opening the application without loading it.
but if the application is already opened Double clicking the associated file is working fine!!!

Regards.
 

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: How to implement "Open With" support on OSX?
« Reply #4 on: January 14, 2011, 09:38:56 pm »
Sounds like you don't have your app bundle's Info.plist settings right. That's how OS X knows about default "associations".

Thanks.

-Phil

mdkass

  • New member
  • *
  • Posts: 8
Re: How to implement "Open With" support on OSX?
« Reply #5 on: January 15, 2011, 12:10:04 pm »
Thank you.
In my project there are 2 auto created forms, the main form and a splash screen and no one is taking the event "ondropfile" upon Application start caused by the double click.

I think that this is my problem!

« Last Edit: January 15, 2011, 12:12:06 pm by mdkass »

mdkass

  • New member
  • *
  • Posts: 8
Re: How to implement "Open With" support on OSX?
« Reply #6 on: January 15, 2011, 12:38:31 pm »
I succeeded to make it work
the old code was :

Code: [Select]
 Application.Initialize;
  frmSplash := TfrmSplash. Create(Application);  

  Application.CreateForm(TfrmMain, frmMain);
  frmMain.Show;
  frmSplash.Show;
  frmSplash.BringToFront;
  Sleep(2000);
  frmSplash.Close;
  frmSplash.Free;
  Application.CreateForm(TformReg, formReg);
  formReg.BringToFront;
  formReg.ShowModal;
  Application.Run;
 

the working code is :
Code: [Select]
 Application.Initialize;
  Application.CreateForm(TfrmMain, frmMain);
  Application.CreateForm(TfrmSplash, frmSplash);

  frmSplash.Show;
  frmSplash.BringToFront;
  Sleep(2000);
  frmSplash.Close;
  frmSplash.Free;
  Application.Run;

and I moved the creation of the 3rd form to frmMain
« Last Edit: January 15, 2011, 01:06:51 pm by mdkass »

SassyPenguin

  • New Member
  • *
  • Posts: 49
Re: How to implement "Open With" support on OSX?
« Reply #7 on: January 24, 2022, 05:06:06 am »
If I understand you correctly, I think you can do it by adding some conditional (Mac-only) code to your main form. For example:

In your declaration of event handlers:

{$IFDEF DARWIN}
    procedure DropFiles(      Sender   : TObject;
                        const FileNames: array of string);
{$ENDIF}                       

Then in FormCreate or some other startup routine:

{$IFDEF DARWIN}
  OnDropFiles := DropFiles;
{$ENDIF}

Then the implementation. (In this example, LoadFile and SetFormData are additional methods specific to my app.)

{$IFDEF DARWIN}
procedure TMainForm.DropFiles(      Sender    : TObject;
                              const FileNames : array of string);
 {When start app by double-clicking a file,
   file name passed to this event handler.}
begin
  if High(FileNames) >= 0 then  {At least one file passed?}
    begin
    if LoadFile(FileNames[0]) then  {Opened first file okay?}
      SetFormData;
    end;
end;
{$ENDIF}                       


One other thing you can do to activate the ability of Finder to start your app and "drop" a double-clicked file on it (thereby opening the file), is to set the CFBundleDocumentTypes key in your app bundle's (.app folder) Info.plist file, thereby associating a document type (extension) with your app. Let me know if you have questions about this or see Apple's docs:

http://developer.apple.com/library/mac/documentation/General/Reference/InfoPlistKeyReference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009247

Thanks.

-Phil

Hi Phil, this Dropfile method worked, but very slow to show my App. When I double clicked the associate file in Finder, my program appeared on Dock instantly but the Main form took like 4 sec to show up with dropped (Double clicked) file.
Any idea why it is like this? Running Lazarus 2.2.0RC2 on Big Surr.

Lazarus 2.2.4 (Win11, Manjaro KDE, CachyOS KDE, Linux Mint)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: How to implement "Open With" support on macOS?
« Reply #8 on: January 24, 2022, 06:55:00 am »
It's very unlikely you will get a response from Phil as he has not visited since October 2018.

The demo application I wrote for my macOS File and Custom URL Scheme Associations Wiki article also takes ~5 seconds on a 2018 Mac mini running macOS 12.1 to load the dropped file content and then display the form (a simple editor with a TMemo).

Check the Applications > Utilities > Console system log for what is happening. Mine shows the log entries for the missing seconds from launch at ~40 seconds on dropping the file to appearance at ~45 seconds. I think those are the only clues you're going to get as to what's happening in those seconds.


trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: How to implement "Open With" support on macOS?
« Reply #9 on: January 24, 2022, 07:43:17 am »
I just re-compiled my simple editor application on my base model M1 Mac mini running macOS 12.1 and... there is no delay between drag and drop or clicking on the test.xyz file in Finder and the application opening with the file content.

Intel Macs' days are numbered :)

SassyPenguin

  • New Member
  • *
  • Posts: 49
Re: How to implement "Open With" support on macOS?
« Reply #10 on: January 24, 2022, 10:13:20 am »
I just re-compiled my simple editor application on my base model M1 Mac mini running macOS 12.1 and... there is no delay between drag and drop or clicking on the test.xyz file in Finder and the application opening with the file content.

Intel Macs' days are numbered :)

hahaha thank you for the update, so does my Ryzentosh's days.
Lazarus 2.2.4 (Win11, Manjaro KDE, CachyOS KDE, Linux Mint)

 

TinyPortal © 2005-2018