Skip to content

Instantly share code, notes, and snippets.

@ljb
Forked from PotatoesMaster/fdfmdaemon.py
Last active July 7, 2024 13:08
Show Gist options
  • Save ljb/30a21952158dfe59c8168d7248176e3b to your computer and use it in GitHub Desktop.
Save ljb/30a21952158dfe59c8168d7248176e3b to your computer and use it in GitHub Desktop.
Using vifm to opening a file directly from the Firefox download manager

Description

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).

Paths

The vifm.service file should be put in the ~/.local/share/dbus-1/services directory.

The vifm-service.py file can be put anywhere as long as the same path is used in vifm.service`.

Possible conflicting services

Note that system level service files can interfere with the session level service files. Make sure that there are no other services implementing the same interface: grep -r org.freedesktop.FileManager1 /usr/share/dbus-1/services

Debugging

If there are problems the following commands can be used for debugging:

Monitoring session (user level) dbus messages

dbus-monitor "interface=org.freedesktop.FileManager1"

Sending session (user level) dbus messages

dbus-send
    --print-reply
    --dest=org.freedesktop.FileManager1
    --type=method_call
    /org/freedesktop/FileManager1
    org.freedesktop.FileManager1.ShowItems
    array:string:"file:///home/"
    string:""
#!/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/).
"""
from gi.repository import GLib
import dbus
import dbus.service
import dbus.mainloop.glib
import os
from subprocess import Popen
def open_file_manager(uri, select=False):
# This uses the vifmrun wrapper from vifmimg (which support image previews).
# If this is not desired, vifm can be used directly instead.
args = ['alacritty', '-e', 'vifmrun']
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 = GLib.MainLoop()
mainloop.run()
[D-BUS Service]
Name=org.freedesktop.FileManager1
Exec=~/Scripts/vifm-service.py
@fretzo
Copy link

fretzo commented Mar 19, 2024

Thank you, works great !!!

Could this also work for the save as window from firefox?

@WayneYam
Copy link

WayneYam commented Jul 7, 2024

It seems like the script doesn't automatically start on boot, but if I start it manually it still works

The journal says

Activation request for 'org.freedesktop.FileManager1' failed: The systemd unit 'vifm.service' could not be found.

EDIT: I copied the wrong file, and I didn't chmod +x the python file, stupid me.

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