Open a directory in the default file manager and select a file

In a program which I am writing I want to offer the functionality to open the directory where the file which I am currently processing is located and automatically select that file (so that the user does not need to search for it).

I know that I can open a directory in the default file manager using

xdg-open /path/to/directory

I know that I can open a directory in nautilus and select a file using

nautilus /path/to/file.txt

I thought that I could use

xdg-mime query default inode/directory

to get the default file manager and – if it is nautilus – call it as shown above.
But, despite the fact that nautilus is the default on my system (xdg-open opens nautilus and so does the places menu in the gnome shell), xdg-mime returns Thunar.desktop.
(I have tried find / -name Thunar.desktop -mount 2>/dev/null but it did not find anything.)

Also, I do not know how to open a directory and select a subdirectory in nautilus (with the above mentioned approach it would open the subdirectory).

How can I open a directory in the default filemanager and select a file in that directory (if selecting a directory, too, was possible that would be great, but for this application not needed)
or at least find out the default filemanager so that I can call it directly?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

1. To open a directory and select a subdirectory/file in nautilus:

nautilus --select path/to/file/or/directory

From nautilus(1) man page:

-s, –select
  Select specified URI in parent folder.

2. xdg-mime returns Thunar.desktop but xdg-open opens nautilus

xdg-mime uses mimeapps.list to determine the default application to use.

Separate mimeapps.list files exist to handle user-specific, system-specific and distribution-specific requirements. Their lookup order can be found over here.

mimeapps.list lists default applications for a given mimetype under [Default Applications] section. It allows to list multiple default applications in their decreasing order of preference. For example :

[Default Applications]
mimetype1 = default1.desktop;default2.desktop;

where mimetype1 is the mime type and *.desktop are the desktop files.

xdg-open searches for desktop file down the lookup order, across the preference list till it finds a valid desktop file. If no such file is found across all the files then the most preferred one according to the associations is chosen and is used as default application.

So in case of our example, let us suppose default1.desktop is not present on our system, so xdg-open will try to open our file using default2.desktop. However, xdg-mime returns default1.desktop which is the first entry in our mimeapps.list file.

In your case default1.desktop must be Thunar.desktop hence the output. However it is not installed on your system. So xdg-open opens your file/directories using nautilus which is present on your system. To verify this, you can check your mimeapps.list file for line containing inode/directory. For Ubuntu 17.10, the location of mimeapps.list file is : /usr/share/applications/defaults.list

NOTE: The complete algorithm to determine ‘Default Applications’ can be found here.

Method 2

You may want to consider using dbus to open your file as it is quickly becoming more popular.

The concept of a “default” file manager only really exists if you are solely focused on xdg-mime, however in dbus land, (and indeed the majority of applications out there) the story takes a different twist, and the concept of a “default” file manager ceases to exist.

This is how you open a file (/home/me/path/to/folder/or/file) in the (default?) file manager, using dbus:

dbus-send --session --print-reply --dest=org.freedesktop.FileManager1 --type=method_call /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:"file:///home/me/path/to/folder/or/file" string:""

What this command does is to look for any dbus service which implements the org.freedesktop.FileManager1 interface, and calls it with the path to the file you wish to open.

The reason I put a question mark after default is because dbus does not respect the concept of “default” file manager; all it does is send the call to the first service it finds that implements the interface and lets it handle the operation. Now it may be that the application it picks is your “default” file manager, but that’s not always a guarantee.

Most modern applications will use dbus by default and then fallback to xdg-mime if the dbus call fails, so this is what I’d suggest you do.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x