Lazarus

Announcements => Third party => Topic started by: lainz on February 15, 2013, 11:42:13 pm

Title: TFileAssociation component
Post by: lainz on February 15, 2013, 11:42:13 pm
TFileAssociation component

Wiki: http://wiki.lazarus.freepascal.org/FileAssociation

With this component you can easily register file associations for all Windows versions. This includes Windows Vista/7/8 Default Programs feature.

Test program 'Lazarus File Association'. This is for all the people using Lazarus SVN  ;)
http://sourceforge.net/projects/lainz007/files/FileAssoc/lazfileassoc_win32.7z/download

See attached screenshot.

Download package:
http://sourceforge.net/projects/lainz007/files/FileAssoc/fileassoc_package.7z/download

Installation:
- Install like anypackage. The component is under the "System" tab.

PD: you can install it or just use the FileAssociation.pas file.

Usage:
- See the 'test' folder.
Title: Re: TFileAssociation component
Post by: Vincent Snijders on February 16, 2013, 09:02:50 am
Please read this wiki (http://wiki.lazarus.freepascal.org/Code_Conversion_Guide#Packaging_and_Releasing_your_component) page and create a page for the component, if you didn't do it yet. That is all you have to do for adding to Lazarus-CCR. If you wish I can upload it the SF site of Lazarus-CCR too.
Title: Re: TFileAssociation component
Post by: BigChimp on February 16, 2013, 09:08:43 am
Great, Lainz, thanks a lot!
Title: Re: TFileAssociation component
Post by: lainz on February 16, 2013, 07:03:47 pm
Please read this wiki (http://wiki.lazarus.freepascal.org/Code_Conversion_Guide#Packaging_and_Releasing_your_component) page and create a page for the component, if you didn't do it yet. That is all you have to do for adding to Lazarus-CCR. If you wish I can upload it the SF site of Lazarus-CCR too.
Ok I'll create the page.
Edit: here it is http://wiki.lazarus.freepascal.org/FileAssociation

Great, Lainz, thanks a lot!

Hope you like it =)
Title: Re: TFileAssociation component
Post by: BigChimp on February 17, 2013, 09:45:33 am
@Lainz: I fixed some typos etc on the page.

I noticed this:
Quote
LGPL (The same as Lazarus).
Are you sure you don't want to change that to:
Quote
Modified LGPL (The same as Lazarus).
?
LCL=Modified LGPL
IDE=GPL

Thanks,
BigChimp
Title: Re: TFileAssociation component
Post by: lainz on February 17, 2013, 12:59:49 pm
Thanks. Ok i've changed it to Modified LGPL.
Title: Re: TFileAssociation component
Post by: lainz on April 04, 2014, 07:51:05 pm
Hi, I've updated TFileAssociation, now it consumes less memory and it has few options. It's all OOP now.

http://wiki.lazarus.freepascal.org/FileAssociation

Also I've tested it better and you need to compile both 32 and 64 bit executable depending on windows version.
Title: Re: TFileAssociation component
Post by: LovelyA72 on January 10, 2019, 08:36:29 am
The lazfileassoc_win32.7z file no longer exists. Can you please re-upload it or include it as an example in the package?

Thank you!
Title: Re: TFileAssociation component
Post by: balazsszekely on January 10, 2019, 08:39:06 am
@LovelyA72
You can find it in OPM.
Title: Re: TFileAssociation component
Post by: LovelyA72 on January 10, 2019, 08:44:29 am
@LovelyA72
You can find it in OPM.

I already installed it from OPM. But there's no example project included.

Maybe I'll just build a new program to test it... Thank you for your reply!
Title: Re: TFileAssociation component
Post by: balazsszekely on January 10, 2019, 08:52:42 am
Maybe the example at the following page helps: http://wiki.lazarus.freepascal.org/FileAssociation
Title: Re: TFileAssociation component
Post by: maurobio on December 15, 2019, 09:57:01 pm
@lainz,

Thanks a lot for this component, but could another example be provided, showing how an application can  effectively open an associated file when the user clicks upon the file icon? I presume some code for treating the  program execution parameter need to be write, but this is not clear from the available documentation for the component.

Thanks in advance for any assistance you can provide.

Best wishes,
Title: Re: TFileAssociation component
Post by: lainz on December 15, 2019, 11:03:11 pm
@lainz,

Thanks a lot for this component, but could another example be provided, showing how an application can  effectively open an associated file when the user clicks upon the file icon? I presume some code for treating the  program execution parameter need to be write, but this is not clear from the available documentation for the component.

Thanks in advance for any assistance you can provide.

Best wishes,

For example if the associated file is a text file you can do:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   s: String;
  4. begin
  5. // if there are parameters
  6.   if ParamCount > 0 then
  7.   begin
  8. // load the first parameter
  9.     s := ParamStr(1);
  10.  
  11. // if is a .txt file
  12.     if ExtractFileExt(s) = '.txt' then
  13.     begin
  14. // load the .txt file into a memo
  15.       Memo1.Lines.LoadFromFile(s);
  16.     end;
  17.   end;
  18. end;    
Title: Re: TFileAssociation component
Post by: maurobio on December 15, 2019, 11:30:17 pm
@lainz,

Thanks for the fast reply.

Unfortunately, it still does not work. When I click on an associated file, Windows Explorer asks for the application to be used to open it.

I have the Action property set to '"fde.exe" "%1"', where fde.exe is my application executable. I suppose this line, along with the code you provided, should do the trick, but it does not. In fact, the test of ParamCount > 0 returns False!

Any hints?

Best regards,
Title: Re: TFileAssociation component
Post by: lainz on December 15, 2019, 11:34:24 pm
Unfortunately, it still does not work. When I click on an associated file, Windows Explorer asks for the application to be used to open it.

I have the Action property set to '"fde.exe" "%1"', where fde.exe is my application executable. I suppose this line, along with the code you provided, should do the trick, but it does not. In fact, the test of ParamCount > 0 returns False!

Any hints?

Best regards,

Hi, "fde.exe" "%1" this is wrong.

It must be like:
'"C:\lazarus\lazarus.exe" "%1"'

It requires a full path.

You can use ParamStr(0) to get your full .exe path. I will add that to the wiki too.

Added like this:
"// full path required, you can use ParamStr(0) to get the path with the .exe name included. The path must be inside quotes if it has whitespace."

Edit, also take into account this I've added:

  // notice that using RegisterForAllUsers as True requires Administrator Privileges
  // if you want to run without privileges set it to false, but it will register for current user only
  assoc.RegisterForAllUsers:=False;
Title: Re: TFileAssociation component
Post by: maurobio on December 16, 2019, 12:31:26 am
@lainz,

Thanks again for your prompt reply.

I had already suspected that a full path was needed, but in fact that is not the only problem I am running into. It happens that no parameter is being passed to my application, therefore the expression below:

Code: Pascal  [Select][+][-]
  1. if ParamCount > 0 then
  2.   begin
  3.     s := ParamStr(1);
  4.     if ExtractFileExt(s) = '.dtz' then LoadFile(s);
  5.   end;
  6.  

always evaluates as False, even if I hardcode the full pathname in the Action property:

Code: Pascal  [Select][+][-]
  1. FileAssociation1.Action := "C:\Users\mauro\My Documents\Projetos\FreeDELTA\fde.exe" "%1"

Best regards,
Title: Re: TFileAssociation component
Post by: lainz on December 16, 2019, 12:54:39 am
Maybe this

  // notice that using RegisterForAllUsers as True requires Administrator Privileges
  // if you want to run without privileges set it to false

Try running it as admin or set registerforallusers to false.

If that doesn't work upload a small test project I can compile and test to see what's happening.
Title: Re: TFileAssociation component
Post by: maurobio on December 16, 2019, 01:21:13 am
@lainz,

Running as admin or setting RegisterForAllUsers to False do not work either.

I will create a test project soon.

Best regards,
Title: Re: TFileAssociation component
Post by: lainz on December 16, 2019, 01:28:07 am
 Ok. Yes is the way to go.
Remember  me what version of Windows you're using so when you upload your program I will try to test on same version

I recently tested the component and worked. But anything can fail as well
Title: Re: TFileAssociation component
Post by: lainz on December 22, 2019, 03:19:48 pm
If this component for some reason doesn't works for you, or you need to run it as administrator for all users without elevating your application, an Inno Setup Script will fit your needs best.

https://wiki.lazarus.freepascal.org/Inno_Setup_Usage#File_Association
Title: Re: TFileAssociation component
Post by: maurobio on December 23, 2019, 03:11:28 pm
Hi, @lainz,

Sorry for the delay in replying. In the end, I had success with the procedure below:

Code: Pascal  [Select][+][-]
  1. {$IFDEF WINDOWS}
  2. uses Registry, ShlObj;
  3.  
  4. procedure TMainForm.RegisterFileType(ExtName: string; AppName: string);
  5. var
  6.   reg: TRegistry;
  7. begin
  8.   reg := TRegistry.Create;
  9.   try
  10.     reg.RootKey := HKEY_CLASSES_ROOT;
  11.     reg.OpenKey('.' + ExtName, True);
  12.     reg.WriteString('', ExtName + 'file');
  13.     reg.CloseKey;
  14.     reg.CreateKey(ExtName + 'file');
  15.     reg.OpenKey(ExtName + 'file\DefaultIcon', True);
  16.     reg.WriteString('', AppName + ',0');
  17.     reg.CloseKey;
  18.     reg.OpenKey(ExtName + 'file\shell\open\command', True);
  19.     reg.WriteString('', AppName + ' "%1"');
  20.     reg.CloseKey;
  21.   finally
  22.     reg.Free;
  23.   end;
  24.   SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
  25. end;
  26. {$ENDIF}

Now, the name of the associated file is correctly passed to my application.

Cheers,
Title: Re: TFileAssociation component
Post by: lainz on December 23, 2019, 04:35:17 pm
Thanks for the snippet. I will add that to the wiki.
https://wiki.lazarus.freepascal.org/FileAssociation#Another_way_of_registering_file_associations
Title: Re: TFileAssociation component
Post by: lainz on December 23, 2019, 08:57:29 pm
@lainz,

Thanks again for your prompt reply.

I had already suspected that a full path was needed, but in fact that is not the only problem I am running into. It happens that no parameter is being passed to my application, therefore the expression below:

Code: Pascal  [Select][+][-]
  1. if ParamCount > 0 then
  2.   begin
  3.     s := ParamStr(1);
  4.     if ExtractFileExt(s) = '.dtz' then LoadFile(s);
  5.   end;
  6.  

always evaluates as False, even if I hardcode the full pathname in the Action property:

Code: Pascal  [Select][+][-]
  1. FileAssociation1.Action := "C:\Users\mauro\My Documents\Projetos\FreeDELTA\fde.exe" "%1"

Best regards,

I'm trying to figure what happened.

Maybe you did not setup a parameter. All parameters are mandatory. That should be on the wiki as well.

Specially ActionName, that must be 'Open' to work with double click. This is useful as well for default commands like 'Edit' and 'Print'. This must be in english for 'edit', 'open' and 'print', so it can access the right registry entry. But you can customize the translation with ActionText.
Title: Re: TFileAssociation component
Post by: maurobio on December 23, 2019, 10:01:18 pm
@lainz,

I am using the above procedure as follows, it the OnCreate event of my application:

Code: Pascal  [Select][+][-]
  1. var
  2.    reg: TRegistry;
  3.        
  4. begin
  5.   {$IFDEF WINDOWS}
  6.   reg := TRegistry.Create;
  7.   try
  8.     reg.RootKey := HKEY_CLASSES_ROOT;
  9.     if not reg.KeyExists('ext' + 'file\shell\open\command') then
  10.       RegisterFileType('ext', ExtractFilePath(Application.ExeName) + 'my_app.exe');
  11.   finally
  12.     reg.Free;
  13.   end;
  14.  
  15.  if ParamCount > 0 then
  16.   begin
  17.     s := ParamStr(1);
  18.     if ExtractFileExt(s) = '.ext' then
  19.       LoadFile(s);
  20.   end;
  21.   {$ENDIF}

Where 'ext' is the extension associated with my application.

Cheers.
TinyPortal © 2005-2018