Skip to content

Instantly share code, notes, and snippets.

@PotatoesMaster
Created December 19, 2013 12:47
Show Gist options
  • Save PotatoesMaster/8038613 to your computer and use it in GitHub Desktop.
Save PotatoesMaster/8038613 to your computer and use it in GitHub Desktop.
A simple daemon implementing freedesktop.org's file manager interface.This interface is used by Firefox download manager to select a downloaded file in the file manager (since Firefox 28).
#!/usr/bin/env python
# This program is free software. It comes without any warranty, to the extent
# permitted by applicable law. You can redistribute it and/or modify it under
# the terms of the Do What The Fuck You Want To Public License, Version 2, as
# published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
"""
This is a simple daemon implementing freedesktop.org's file manager interface
(http://www.freedesktop.org/wiki/Specifications/file-manager-interface/).
"""
import gobject
import dbus
import dbus.service
import dbus.mainloop.glib
import os
from subprocess import Popen
def open_file_manager(uri, select=False):
args = ['urxvt', '-e', 'ranger']
if select:
args.append('--select')
path = str(uri)
if path.startswith('file://'):
path = path[7:]
args.append(path)
if os.fork() == 0:
Popen(args)
os._exit(0)
else:
os.wait()
class FmObject(dbus.service.Object):
@dbus.service.method("org.freedesktop.FileManager1",
in_signature='ass', out_signature='')
def ShowFolders(self, uris, startupId):
open_file_manager(uris[0])
@dbus.service.method("org.freedesktop.FileManager1",
in_signature='ass', out_signature='')
def ShowItems(self, uris, startupId):
open_file_manager(uris[0], select=True)
@dbus.service.method("org.freedesktop.FileManager1",
in_signature='ass', out_signature='')
def ShowItemProperties(self, uris, startupId):
open_file_manager(uris[0], select=True)
@dbus.service.method("org.freedesktop.FileManager1",
in_signature='', out_signature='')
def Exit(self):
mainloop.quit()
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
session_bus = dbus.SessionBus()
name = dbus.service.BusName("org.freedesktop.FileManager1", session_bus)
object = FmObject(session_bus, '/org/freedesktop/FileManager1')
mainloop = gobject.MainLoop()
mainloop.run()
@vitaly-zdanevich
Copy link

What to do with that? How to install it?

@PotatoesMaster
Copy link
Author

PotatoesMaster commented Oct 12, 2023

Hi @vitaly-zdanevich.

The goal of the script is to open ranger (a file manager for the terminal) when "open directory" is clicked/actioned for a downloaded file in Firefox.

This program is intended to work as a daemon, meaning you run it in the background all the time (during the user session) for it to be able to respond to events when they happen.

I do not know if this script is still working 10 years later.

I read here that you may need to change some Firefox config: https://wiki.archlinux.org/title/Firefox (section 4.4 XDG Desktop Portal integration).

Also as of now, I would personnaly use a more robust implementation rather than this loosy script with hardcoded command. 🙂
See for example https://github.com/GermainZ/xdg-desktop-portal-termfilechooser

@ljb
Copy link

ljb commented Oct 13, 2023

I got a modified version of this working yesterday:
https://gist.github.com/ljb/30a21952158dfe59c8168d7248176e3b

My solution uses another terminal and vifm instead of ranger. But that can easily be changed.

But maybe https://github.com/GermainZ/xdg-desktop-portal-termfilechooser is a cleaner solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment