0

How can I tell xdg-open to open, say, YouTube URLs in a specific app, but use my default browser for all other URLs?

2 Answers 2

1

The closest you can get with xdg is using url-scheme-handlers, i.e. mailto, http, ftp... and so on.

xdg-settings list default-url-scheme-handlers

This will make Google Chrome the default handler for all https links.

xdg-settings set default-url-scheme-handler https google-chrome.desktop

You can't achieve domain specificity with xdg-open.
Using a script that intercepts YouTube links and opens them with a specific application is really the only way.

This is similar to what you're after.

It's the approach you want, only without the yad dialog. It really depends on how and/or what is opening the URL.

3
  • It doesn't have to use xdg-open. Commented May 28, 2024 at 1:54
  • 1
    that command doesn't work for me, I need to run something like xdg-settings get default-url-scheme-handler http. xdg-settings version 1.1.3. Commented Aug 13, 2024 at 14:42
  • @JanKanis The execution, as Geremia noted, doesn't have to be xdg-open. If you xdg-settings set default-url-scheme-handler http google-chrome.desktop then any http link will be opened with chrome, regardless of what triggers the URL. You'd need to be more specific for your use case... What is launching the browser? Commented Aug 13, 2024 at 15:32
0

Use a wrapper script:

#!/usr/bin/bash

url=$1

if [[ $url == *youtube.com/watch* ]] || 
  [[ $url == *youtu.be* ]] || 
  [[ $url == *vimeo.com* ]] || 
  [[ $url == *instagram.com* ]] ||
  [[ $url == *facebook.com* ]] || 
  [[ $url == *twitter.com* ]] || 
  [[ $url == *x.com* ]]; then
  mpv $url
  if [[ $? -ne 0 ]]; then
    kdialog --error "Error playing video $url"
    exit 1
  fi
elif [[ $url == ^mailto:* ]]; then
  thunderbird $url
else
  firefox $url
fi
$ chmod +x ~/.local/bin/url-handler.sh

~/.local/share/applications/url-handler.desktop:

[Desktop Entry]
Name=URL Handler
Exec=/home/YOUR_USERNAME/.local/bin/url-handler.sh %u
Terminal=false
Type=Application
MimeType=x-scheme-handler/http;x-scheme-handler/https;
$ xdg-mime default url-handler.desktop x-scheme-handler/http
$ xdg-mime default url-handler.desktop x-scheme-handler/https

🎩-tip: this Google Gemini transcript (with my edits)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.