Which program(s) opens such file? lazarus / 32-Linux ---------------------------------------------------------------------- lucamar · 2019-07-25 13:56 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. ---------------------------------------------------------------------- ASerge · 2019-07-25 17:39 Quote from: lucamar on July 25, 2019, 03:56:34 pmIs 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 · 2019-07-25 19:01 Since we are in "Programming > Operating Systems > Linux" ... Linux 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 ---------------------------------------------------------------------- toby · 2019-07-25 19:20 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 · 2019-07-25 19:41 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 ..." ---------------------------------------------------------------------- winni · 2019-07-25 19:47 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 · 2019-07-25 19:53 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 · 2019-07-25 20:07 Quote from: toby on July 25, 2019, 09:53:02 pmcheck 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 Quoteisn'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); ---------------------------------------------------------------------- Leledumbo · 2019-07-25 20:46 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 itThere's additional processing needed to parse the values are they're both separated and terminated by a semicolonThose 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 valuesOnce 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 · 2019-07-25 21:33 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. ---------------------------------------------------------------------- sfeinst · 2019-07-26 12:09 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 · 2019-07-26 12:38 my music file types fmask := '*.asf;*.avi;*.flac;*.flv;*.mkv;*.mov;*.mpeg;*.mp2;*.mp3;*.mp 4;*.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 · 2019-07-26 19:15 Quote from: sfeinst on July 26, 2019, 02:09:43 pmlclintf 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. Quote from: toby on July 26, 2019, 02:38:48 pmmy music file types [... rest snipped ...] Hmmm ... what? Mistook this one for another thread, didn't you? ---------------------------------------------------------------------- toby · 2019-07-26 19:41 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 · 2019-07-26 20:03 Quote from: toby on July 26, 2019, 09:41:13 pmdidn[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 ---------------------------------------------------------------------- winni · 2019-07-26 20:24 Hi The linux command line utility  to achieve that is named file Read the man page about the three different ways to get the info, especially about the "magic" file info. And if you can read C look into the sources of file Winni ---------------------------------------------------------------------- lucamar · 2019-07-26 21:24 Thanks, but using file would be just one step in the process: that of obtaining the mime-type of the file. And I can do that relatively easily by consulting the mime database directly. I don't use file very much, truth be told; I find it useful only to guess the type of strange files or to ensure that a file is really what it appears to be: for example, whether a .doc is an MSWord file or plain text (which was very common in the old days before Word apropiated the extension), etc. What I would really need is a command (or function) like: Code: [Select]whocanopen somefile.extBut it seems I'll have to write it myself ---------------------------------------------------------------------- toby · 2019-07-26 21:32 technically anything can open anything - i'm sure you have done    vi aa.html      or   dillo a.mp4  or   dillo a,txt    you can even  do   mplayer 1,txt the end result may not be what you want   (i don't recomment doing  vi a.mp4   or even vi google-saved-search-page-html  (have you seen all the crap in what google sends you to track everything) it is up to you to decide what the file can open  that is why i gave you the fmask/mask info ---------------------------------------------------------------------- lucamar · 2019-07-26 22:23 Quote from: toby on July 26, 2019, 11:32:48 pmtechnically anything can open anything Sure, and I do have my system set so that gedit tries to open some strange "not-really-but-almost text" files and so that ghex opens anything. But users are used to see that some kind of files can be opened with "these programs", other kind with "those programs" and so on; and one has to take into account that "users" are prone to customizations: "no, I want this kind of files be opened with this  other program". Quoteit is up to you to decide what the file can open  that is why i gave you the fmask/mask info I think your'e misunderstanding what this thread is about. It's not about what my program can open but about which programs the sytem has configured to open "this file" whose name my program has. As I said above, IIRC, imagine that my program is a file manager: It doesn't open files by itself but needs to know which programs open each file. ---------------------------------------------------------------------- winni · 2019-07-26 22:33 Hi! Okay. As far as I know the linux system does not have such a service. That job is up to the different desktops. So you should look or ask what the gnome and KDE teams do to configure the relationship between filetype and application. Think that's the right direction. Winni ---------------------------------------------------------------------- toby · 2019-07-26 22:47 okay you win when you do get to coding it - are you planning on using sqlite or?? just a text file? for the file types? ---------------------------------------------------------------------- winni · 2019-07-26 23:02 @lucamar KDE stores for every desktop application one file with the suffix .desktop in /usr/share/applications. After a lot of multi-language stuff they contain all the mimetypes they can handle. But it seems that there are only installed applications, so you will never get a complete list. And so this is only the list for a specific desktop. Winni ---------------------------------------------------------------------- lucamar · 2019-07-27 00:01 Quote from: winni on July 27, 2019, 12:33:14 amSo you should look or ask what the gnome and KDE teams do to configure the relationship between filetype and application. Think that's the right direction. Thanks. I already know (more or less) what they do: they store all that info following (more or less) the recomendations/specifications of freedesktop.org and the XDG. Leledumbo's post above explains it  quite well. Quote from: toby on July 27, 2019, 12:47:23 amwhen you do get to coding it - are you planning on using sqlite or?? just a text file? for the file types? I've started already (yesterday, in fact). I don't plan to store anything (it's not that kind of app); it's just a couple functions (which will multiply to a dozen "helpers", you'll see ); kind of: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetCmdsFor(const AFilename:String): TStringList;function GetDefaultCmdFor(const AFilename:String): String; BTW, I accept suggestions for the functions' names ---------------------------------------------------------------------- winni · 2019-07-27 00:09 Captain Cook sails to Australia! Good luck! Winni ---------------------------------------------------------------------- MarkMLl · 2019-10-22 08:33 The nearest you will get that's installed as standard is xdg-mime. That's one of a group of programs that have standardised command lines but OS-specific implementations, I think that on Linux it uses  file  to look at content so will only work with files that exist (i.e. you can't ask about *.pdf). MarkMLl