Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-01

Cross platform 'open file with default program'

10 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luiz Americo Pereira Camara

Does someone implemented a function to open the system default app associated of a given file type? Similar to OpenURL?

Luiz

Reply by email

Graeme Geldenhuys
to Luiz Americo Pereira Camara

2010/1/24 Luiz Americo Pereira Camara :

Does someone implemented a ?function to open the system default app associated of a given file type? Similar to OpenURL?

Under Linux with distros supporting the freedesktop.org standards you can execute the 'xdg-open' command. This will use the mime-type and the each users preference on that system. This works for URL's too.

If the distro doesn't support xdg, I recommend you email the maintainers of that distor. Also xdg tools are downloadable from freedesktop.org, is small and user installable (without root access).

Anyway, in my projects I have abstracted various platforms into a single command, similar to OpenURL. Our projects only need to support Linux and Windows though, so I don't know the solutions for other platforms.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

Juha Manninen
to Graeme Geldenhuys

Hello.

Under Linux with distros supporting the freedesktop.org standards you can execute the 'xdg-open' command. This will use the mime-type and the each users preference on that system. This works for URL's too.

Great! For Windows you can use a wrapper for ShellExecute function. I copied my code below.
If OSX supports your 'xdg-open' then most environments are covered.

This makes sense only with GUI. Command line progs can't use it. This should be included in FPC/Lazarus libraries, but which one?

Regards,
Juha Manninen

-------------------------------------

procedure OpenWithAssociatedProg(aFilePath, aFileName: string); overload;
var
  Params: string;
begin
  Params := '';
    if (aFileName  '') and FileExists(aFilePath + aFileName) then
      // Calls automatically the program associated with the file type.
      ShellExecute(Application.MainForm.Handle, 'open',
                PChar(aFileName),             // FileName
                PChar(Params),                // Parameters
                PChar(aFilePath),             // Current Directory
                SW_SHOWNORMAL);               // ShowCmd
end;
procedure OpenWithAssociatedProg(aFileNameFull: string); overload;
var
  FilePath, FileName: string;
begin
  FilePath := ExtractFilePath(aFileNameFull);
  FileName := ExtractFileName(aFileNameFull);
  OpenWithAssociatedProg(FilePath, FileName);
end;

Reply by email

dmitry boyarintsev
to Juha Manninen
Juha Manninen wrote
If OSX supports your 'xdg-open' then most environments are covered.

It doesn't, by default. There's an external tool 'open' available for such task. But, it's better to use launcher API (see OpenURL darwin code).

thanks,
dmitry

Reply by email

Graeme Geldenhuys
to Juha Manninen: Hello. Great! For Windows you can use a wrapper for ShellExecute function. I copied my…

2010/1/24 Juha Manninen :

If OSX supports your 'xdg-open' then most environments are covered.

I doubt it will. As far as I know xdg tools are for Linux only (maybe other unix OSes too, but ain't sure).

This makes sense only with GUI. Command line progs can't use it.

There are other scripts for command line tools too, like opening URL's with lynx, or editing files with a console editor (vi, mcedit, joe,...) etc..

This should be included in FPC/Lazarus libraries, but which one?

I know that Lazarus IDE already uses xdg-open under Linux for OpenURL
- before trying other known web browsers. As for where in LCL such code should go, I have no idea. Where is OpenURL defined?

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

Luiz Americo Pereira Camara
to Graeme Geldenhuys
Graeme Geldenhuys wrote 8 lines
2010/1/24 Juha Manninen : > This should be included in FPC/Lazarus libraries, but which one? > I know that Lazarus IDE already uses xdg-open under Linux for OpenURL - before trying other known web browsers. As for where in LCL such code should go, I have no idea. Where is OpenURL defined

LCLIntf

Luiz

Reply by email

dmitry boyarintsev
to Juha Manninen: OpenURL already implements what you are asking! I tested with a local '.patch' file and…
Juha Manninen wrote
One thing is missing though. There is no portable way of getting application's icon like ExtractAssociatedIcon does in Windows? This is somewhat related to opening the file.

It depends on what you exactly need.
If you need just to get an icon associated with a file extension, you'll need to use ShellAPI functions in Windows, and different API in OSX. I guess there should some similar API in Gtk/Qt for Linux, too.

If you just need to extract an Icon of the program's exectuable... well you can use Application.Icon.

thanks,
dmitry

Reply by email

Juha Manninen
to Luiz Americo Pereira Camara: Does someone implemented a function to open the system default app associated of a given…

Amazing,
OpenURL already implements what you are asking!
I tested with a local '.patch' file and it opened nicely in Kompare (KDE prog for diffs).

OpenURL uses ShellExecuteW for Windows, 'xdg-open' for *nix and some API functions for Darwin.

There seems to be lots of quality code in FPC/Lazarus. Good! One thing is missing though. There is no portable way of getting application's icon like ExtractAssociatedIcon does in Windows? This is somewhat related to opening the file.
I asked about it some time ago in FPC mailing list and didn't get answers, so I guess there is no such function. Does anyone have an idea about how to implement it (except for Windows)?

Regards,
Juha Manninen

Reply by email

cobines
to Juha Manninen

2010/1/24 Juha Manninen :

Show quoted text 6 lines
One thing is missing though. There is no portable way of getting application's icon like ExtractAssociatedIcon does in Windows? This is somewhat related to opening the file. I asked about it some time ago in FPC mailing list and didn't get answers, so I guess there is no such function. Does anyone have an idea about how to implement it (except for Windows)?

You can use freedesktop.org standards in Linux:
- shared-mime-info
(http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html) to get MIME type of file
- icon-theme (http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html) to get icon for the MIME type

I'm not aware of any implementations in pascal.

cobines

Reply by email

Luiz Americo Pereira Camara
to Juha Manninen: Hello. Great! For Windows you can use a wrapper for ShellExecute function. I copied my…
Juha Manninen wrote 11 lines
Hello. > Under Linux with distros supporting the freedesktop.org standards you > can execute the 'xdg-open' command. This will use the mime-type and > the each users preference on that system. This works for URL's too. > Great! For Windows you can use a wrapper for ShellExecute function. I copied my code below.

Thanks. I'm using your code with one modification: instead of Application handle i'm using hInstance so i can call before main form is created (e.g: a login dialog)

Luiz

Reply by email