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
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.
- It doesn't have to use
xdg-open.Geremia– Geremia2024-05-28 01:54:23 +00:00Commented May 28, 2024 at 1:54 - 1that 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.JanKanis– JanKanis2024-08-13 14:42:47 +00:00Commented 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.desktopthen anyhttplink 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?JayCravens– JayCravens2024-08-13 15:32:38 +00:00Commented Aug 13, 2024 at 15:32
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)