Recent

Author Topic: Which program(s) opens such file?  (Read 911 times)

lucamar

  • Hero Member
  • *****
  • Posts: 2023
Which program(s) opens such file?
« on: July 25, 2019, 03:56:34 pm »
Is there some function to get which program(-s) is configured in the system to open some type of file or must I parse the mimetypes database(s)?

What for?
Suppose (for example) you have a FileListBox, you clcik/select one file and an "Open with..." listbox (or a popup-menu or whatever) is populated with the programs that can open that file so you can select one and do it.

Bonus points if, among several possible programs, it can be ascertained which program is the default.
« Last Edit: July 25, 2019, 03:58:10 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

ASerge

  • Hero Member
  • *****
  • Posts: 1408
Re: Which program(s) opens such file?
« Reply #1 on: July 25, 2019, 07:39:20 pm »
Is there some function to get which program(-s) is configured in the system to open some type of file or must I parse the mimetypes database(s)?
OS?

lucamar

  • Hero Member
  • *****
  • Posts: 2023
Re: Which program(s) opens such file?
« Reply #2 on: July 25, 2019, 09:01:56 pm »
Since we are in "Programming > Operating Systems > Linux" ... Linux :D

This machine uses Ubuntu 12.04 (if it matters) but I'm looking for a generic Linux solution.

From your answer I infer it isn't already there, is it? I'lll have to go parse the mimetypes config <sight> :(
« Last Edit: July 25, 2019, 09:42:33 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

toby

  • Jr. Member
  • **
  • Posts: 65
Re: Which program(s) opens such file?
« Reply #3 on: July 25, 2019, 09:20:24 pm »
are you looking in lsof ?

you an bring it all into a stringlist with aprocess.execute and i _think_ what you are asking about will be there?

lucamar

  • Hero Member
  • *****
  • Posts: 2023
Re: Which program(s) opens such file?
« Reply #4 on: July 25, 2019, 09:41:13 pm »
Thanks toby but that's not what I'm asking.

What I'm looking for is something like the "file associations" in windows, i.e. when the user clicks, for example, on a ".pdf" file I want to get which programs open PDFs; evince, gedit, gimp, ...

Basicaly, what shows when you right-click a file in the file manager and select "Open with ..."
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

winni

  • Sr. Member
  • ****
  • Posts: 339
Re: Which program(s) opens such file?
« Reply #5 on: July 25, 2019, 09:47:36 pm »
Hi!

It seems that all the distros now agree that the whole mime stuff is located in
/usr/share/mime

The official list of the mimetypes of the Iana is here
https://www.iana.org/assignments/media-types/media-types.xhtml

A module to implement the mimetypes database you'll find here
https://nim-lang.org/0.19.0/mimetypes.html

Winni

toby

  • Jr. Member
  • **
  • Posts: 65
Re: Which program(s) opens such file?
« Reply #6 on: July 25, 2019, 09:53:02 pm »


sorry i misread - i thought the file was already 'opened' and you wanted to know what opened it - programmatically (that is what i wanted to help you with !!!)

check xdg-*
xdg-desktop-icon   xdg-email          xdg-mime           xdg-screensaver   
xdg-desktop-menu   xdg-icon-resource  xdg-open           xdg-settings

isn't the file types associated IN what the program you are running that opens files is?
like firefox?

or file manager?
https://libre-software.net/change-the-default-application-linux-mint-ubuntu/


lucamar

  • Hero Member
  • *****
  • Posts: 2023
Re: Which program(s) opens such file?
« Reply #7 on: July 25, 2019, 10:07:33 pm »
check xdg-*
xdg-desktop-icon   xdg-email          xdg-mime           xdg-screensaver   
xdg-desktop-menu   xdg-icon-resource  xdg-open           xdg-settings

Yeah, that ammounts to checking the mimedb, which is what I was hoping to avoid :)

Quote
isn't the file types associated IN what the program you are running that opens files is?
like firefox?
or file manager?
https://libre-software.net/change-the-default-application-linux-mint-ubuntu/

Huh? Sorry but I can't understand what you're driving to. If you mean whether the file-type is associated with my application or if I want it to be so, the answer (to both) is no.

To make it more easy to understand, imagine that I'm writing a file manager and that I want an easy way to populate that "Open with ..." submenu that I talked about in my previous post.

I know how to do it the hard way, searching the various desktop config files, the mimetypes DBs, etc. but I wanted to know if there was a ready function like, say,
  GetFileAssociations(AType: String; AList: TStrings):
that you might call for example with:
  GetFileAssociations('.txt', ListBox.Items);
or
  GetFileAssociations('text/plain', ListBox.Items);
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8108
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Which program(s) opens such file?
« Reply #8 on: July 25, 2019, 10:46:26 pm »
There's currently no such functionality, feel free to contribute ;)

I can give you some pointers for starting point:
  • These are all locations where application list to open certain mimetypes are stored, the format resembles .ini files and I believe TIniFile can be used to read it
  • There's additional processing needed to parse the values are they're both separated and terminated by a semicolon
  • Those values point to .desktop files can be without path, in which they must be prepended with $XDG_DATA_DIRS/applications, where $XDG_DATA_DIRS (expand it with SysUtils.GetEnvironmentVariable) may have multiple colon separated values
  • Once you get the path to .desktop, again open it with TIniFile and look for Exec key. The value is what you're looking for but it may contain some parameter placeholder like %f that you might want to remove

lucamar

  • Hero Member
  • *****
  • Posts: 2023
Re: Which program(s) opens such file?
« Reply #9 on: July 25, 2019, 11:33:58 pm »
Thanks Leledumbo. That's basically the information I gathered in the ... hmmm ... "information gathering" phase of this project (some I already had, like a full mirror of freedesktop.org and the xdg-specs repo ;) ).

I see I'll have to write the function myself. Once completed (gods willing, in a couple days) I'll post it here.

Thanks everyone. ;D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

sfeinst

  • Full Member
  • ***
  • Posts: 186
Re: Which program(s) opens such file?
« Reply #10 on: July 26, 2019, 02:09:43 pm »
lclintf unit contains an OpenDocument function.  I don't think it lists all possible apps for a type (like you would use for an Open With... option), but you might be able to look at its source code (you'll have to use the OS specific source) to see how it finds the default and possibly use that in conjunction with your code that is reading mime types to get a list of apps that will support the file type.

toby

  • Jr. Member
  • **
  • Posts: 65
Re: Which program(s) opens such file?
« Reply #11 on: July 26, 2019, 02:38:48 pm »

my music file types
fmask := '*.asf;*.avi;*.flac;*.flv;*.mkv;*.mov;*.mpeg;*.mp2;*.mp3;*.mp4;*.mpg;*.ogg;*.ogv;*.opus;*.qt;*.vob;*.wav;*.webm;*.wma;*.wmv';

mask property in tfilelistbox
mask := fmask;

filelistbox1 := tfilelistbox.create(self); // file
//filelistbox1 := tcustomlistbox.create(self);
//filelistbox1 := tcustomfilelistbox.create(self);



lucamar

  • Hero Member
  • *****
  • Posts: 2023
Re: Which program(s) opens such file?
« Reply #12 on: July 26, 2019, 09:15:17 pm »
lclintf unit contains an OpenDocument function.  I don't think it lists all possible apps for a type (like you would use for an Open With... option), but you might be able to look at its source code (you'll have to use the OS specific source) to see how it finds the default and possibly use that in conjunction with your code that is reading mime types to get a list of apps that will support the file type.

Thanks for the suggestion. Unfortunately OpenDocument either calls an underlying widgetset function, if it exists (like p.e. in Android or Windows) or simply looks for 'xdg-open', 'gnome-open', etc. comand and, if found, runs it with RunCmdInPath(); basicaly, it delegates the hard work to an external program. :)

Which, BTW, I wouldn't mind doing if a command existed to do what I need. :(

my music file types [... rest snipped ...]

Hmmm ... what? Mistook this one for another thread, didn't you? ;)
« Last Edit: July 26, 2019, 09:18:34 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

toby

  • Jr. Member
  • **
  • Posts: 65
Re: Which program(s) opens such file?
« Reply #13 on: July 26, 2019, 09:41:13 pm »

didn[t get that far yet? that would be the code you would use in your open directory code for the file types
that was the example for music files

lucamar

  • Hero Member
  • *****
  • Posts: 2023
Re: Which program(s) opens such file?
« Reply #14 on: July 26, 2019, 10:03:33 pm »
didn[t get that far yet? that would be the code you would use in your open directory code for the file types
that was the example for music files

Uuuhhh ... welll my sincere thanks for your ... huh .... attempt? ... to help, but this thread is not about that. It's about finding file-type associations in Linux :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.