[Thunar] Opening files via a file manager in their associated programs

I’m not sure if pcmanfm uses defined MIME type associations. xdg-open on my machine opens mp3 files in the web browser, while pcmanfm opens them in Audacious.

obraz

Interestingly, thunar in BL launches default application for the file type. Noticed they have some config/script installed from their repos. Check it out.

You can do all of it with xdg-open, directories would be opened in a FMand files with their respective programs.

I know xdg and mime types are a pain to configure, check out my soap fork Bitbucket, it’s a simple xdg-open replacement and for guys like us it is much easier to configure with fallback to xdg-open.

1 Like

Indeed. I don’t understand it at all. I have just one ~/.config/mimeapps.list file and changes seem to take no effect. I assigned mp3 and wma files to Audacious:

[piotr@msi nwg-drawer]$ xdg-mime query default audio/mp3
audacious.desktop
[piotr@msi nwg-drawer]$ xdg-mime query default audio/x-mp3
audacious.desktop
[piotr@msi nwg-drawer]$ xdg-mime query default audio/wma
audacious.desktop

…but mp3 keep on opening in Firefox, and wma in mpv.

Well, I can’t make nwg-drawer users install and configure xdg-open replacements for the program to work. But yes, soap works perfectly well, thanks. Let users take care of their file associations. :smiley:

I think I’ll assign ‘xdg-open’ to LMB and ‘Open In FM’ to RMB, and then open like this:

func open(filePath string, xdgOpen bool) {
	var cmd *exec.Cmd
	if xdgOpen {
		cmd = exec.Command("xdg-open", filePath)
	} else {
		cmd = exec.Command(*fileManager, filePath)
	}
	cmd.Start()

	gtk.MainQuit()
}

I can look into this again as I did have it figured out to add new associations with xdg but have since forgot.

Maybe do your own config with some regex much like soap and let users configure it through a text config, not ideal but results are good. Checking file types are fairly easily programmatically

I’ll mention this possibility and add my config.h as an example. I won’t fork your fork to add reading config from a text file. My C is too rudiment, and it’s not going to change.

I more meant something in python, it would be fairly easy

Maybe one day, in python or go. Now I’m working on 2 old and 1 new project at the same time. :slight_smile:

@natemaia Finally I decided to make it possible to add a simple users preferred-apps.json file, e.g. like this:

{
  "\\.pdf$": "atril",
  "\\.svg$": "inkscape",
  "\\.(jpg|png|tiff|gif)$": "feh",
  "\\.(mp3|ogg|flac|wav|wma)$": "audacious",
  "\\.(avi|mp4|mkv|mov|wav)$": "mpv",
  "\\.(doc|docx|xls|xlsx)$": "libreoffice",
  "\\.(htm|html)$": "firefox"
}

It’s being read into a map, and overrides xdg-open association if a match found:

func open(filePath string, xdgOpen bool) {
	var cmd *exec.Cmd
	if xdgOpen {
		cmd = exec.Command("xdg-open", filePath)
		// Look for possible custom file association
		for key, element := range preferredApps {
			r, err := regexp.Compile(key)
			if err == nil && r.MatchString(filePath) {
				cmd = exec.Command(fmt.Sprintf("%v", element), filePath)
				break
			}
		}
	} else {
		cmd = exec.Command(*fileManager, filePath)
	}
	fmt.Printf("Executing: %s", cmd)
	cmd.Start()

	gtk.MainQuit()
}

Seems to work like a charm. Thank you and @chroot for ideas.

1 Like