Select which browser will open links in KDE (WIP)

HI all,

I use the Activity feature in KDE. Currently one for work (WorkActivity) and one personal (PersonalActivity). I also have two Firefox profiles configured for the same reason.

When I’m on the WorkActivity I want to open almost all of the links in the firefox WorkProfile, but want to have the option of opening it on my Default one. When I’m on the PersonalActivity, I want all the links to open in the firefox PersonalProfile.

I would have like to be able to trigger this kdialog menu with a shortcut, like Alt + click, but couldn’t figure that out…

This workflow may or may not have anything of interest to you, but I’m guessing you can get some interesting ideas from how I managed it.

I added a shell script and a .desktop file for it in home.nix:

  home.packages = with pkgs; [
    (pkgs.writeShellScriptBin "BrowseWhich" ''
      if [ $(qdbus org.kde.ActivityManager /ActivityManager/Activities ActivityName `qdbus org.kde.ActivityManager /ActivityManager/Activities CurrentActivity`) = "WorkActivity" ]; then
        kdialog --title "BrowseWhich" --menu "Select Browser:" "firefox -P WorkProfile $1" WorkProfile "firefox $1" PersonalProfile | bash
      else
        firefox "$1"
      fi
    '')
    (pkgs.makeDesktopItem {
      name = "BrowseWhich";
      desktopName = "BrowseWhich";
      type = "Application";
      exec = "BrowseWhich %U";

    })
  ];

In the if statement above, I’m just getting the current Activity name, then either using kdialog to show a menu of the two firefox profiles, or just open the link in the PersonalProfile. The .desktop file creates an “Application” that I can then set as the default for browsers. I’m guessing I could have just set it as the default through the System Settings > Apps & Windows > Default Applications option:

But I ended up trying to also declare the mimetype associations with xdg.mimeApps.defaultApplications. I’m not sure these will keep up to date and am wondering if I should just remove this. I’d love to hear if anybody has any experience declaring appiclation/mimetype pairs in this way…

  xdg = {
    mimeApps = {
      enable = true;
      defaultApplications = {
        "application/rss+xml" = [ "userapp-Thunderbird-YMQ6R2.desktop" ];
        "application/x-extension-htm" = [ "BrowseWhich" ];
        "application/x-extension-html" = [ "BrowseWhich" ];
        "application/x-extension-ics" = [ "userapp-Thunderbird-QOQES2.desktop" ];
        "application/x-extension-rss" = [ "userapp-Thunderbird-YMQ6R2.desktop" ];
        "application/x-extension-shtml" = [ "BrowseWhich" ];
        "application/x-extension-xht" = [ "BrowseWhich" ];
        "application/x-extension-xhtml" = [ "BrowseWhich" ];
        "application/x-matroska" = [ "vlc.desktop" ];
        "application/xhtml+xml" = [ "BrowseWhich" ];
        "audio/mpeg" = [ "org.kde.amarok.desktop" ];
      };
    };
  };

Please let me know if you can think of a better way to do this. Happy to see what others are doing or what a terrible idea this was. :smile:

3 Likes