Do not show "Open with" dialog when running xdg-open from a flatpak'd app

I am trying to open a pdf file from a python app that I am developing with Gnome Builder. I have:

import subprocess
subprocess.call(["xdg-open", pdf_path])

This opens an “Open with” dialog showing various application to open the pdf file. The same code on a simple python script opens the pdf file with Evince without asking anything.

How would I specify Evince as a default program to use when doing xdg-open from a flatpak’d app?

First of all, don’t use subprocess to spawn xdg-open to open a file: Gio and GTK have all the API you need. Calling xdg-open is only ever useful for applications that do not depend on any Linux/freedesktop library.

The dialog is part of the “portal” mechanism that lets a sandboxed app interact with the system. The basic tenet of the portals is to ensure that the user is informed and can give consent to the app actions if they want to get out of the sandbox—which is why you’re getting an explicit dialog to confirm the action of opening an external app.

The system should only ask for confirmation once per file type, per application.

1 Like

I do believe that, at least with org.gnome.Platform, that the xdg-open implementation comes from flatpak-xdg-utils which talks to the portal.

Yep, the version of xdg-open in Flatpak is just a small wrapper around the portal API, because it doesn’t need to go through the bash scripting contortions of the xdg-utils one to determine the desktop environment and call the appropriate tool.

I’m not sure if the sandbox version uses the appropriate app id of the calling application, though, which may break the setting storage that remembers if you already assigned an application for a given MIME type, which would explain why the OP is seeing the app selection dialog more than once.

I am now using Gio this way:

def on_row_activated(self, widget, row, col):
    gfile_path = get_file_path(widget,row,col)
    gfile_uri = GLib.filename_to_uri(gfile_path)
    Gio.AppInfo.launch_default_for_uri(gfile_uri)

Using Gio.AppInfo still shows the “Open with” dialog when the on_row_activated() is called the second time.

Is there a way to get which app was used? Gio.AppInfo.get_recommended_for_type() returns an empty list after Gio.AppInfo.launch_default_for_uri() is called.

BTW, Geary has the same problem. When I double click to open a pdf attachment, the “Open with” dialog always appears.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.