1

I have the following bash script I want to use as my "standard browser" with xdg-open.

It should prompt dmenu for me to choose the browser to open the url in.

Now xdg-open passes the url as an argument to the program (I suppose) and as I'm cycling through an array of browsers using the @ symbol, it confuses this one with the argument (url) and errors on the dmenu command.

Is there a workaround to this problem or am I doing something completely wrong? --This problem was solved

#!/usr/bin/env bash

set -euo pipefail
_path="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo ".")")" && pwd)"
if [[  -f "${_path}/_dm-helper.sh" ]]; then
  # shellcheck disable=SC1090,SC1091
  source "${_path}/_dm-helper.sh"
else
  # shellcheck disable=SC1090
  echo "No helper-script found"
fi

# script will not hit this if there is no config-file to load
# shellcheck disable=SC1090

source "$(get_config)"

main() {

if [ -t 0 ]
then
    _url=$1
else
    read _url
fi

_browser=$(printf '%s\n' "${!browsers[@]}" | sort | ${DMENU} 'Select browser: ') # "$@") ## Thx to @jhnc
_command=${browsers[${_browser}]}


if [[ -n ${_url} ]];then
        $_command $_url
fi

}

[[ "${BASH_SOURCE[0]}" == "${0}" ]] && main "$@"

(get config) loads the dmenu command: DMENU=dmenu -i -l 20 -p as well as the array of browsers:

declare -A browsers
browsers[brave]="brave-browser"
browsers[firefox]="firefox"
browsers[opera]="opera"
browsers[badwolf]="badwolf"

from my config file.

Originally if i ran xdg-open "https://" or if I clicked on a url in some other program, brave was opened with on that site.

Now after xdg-settings set default-web-browser dmenu-script.desktop with the following .desktop file:

[Desktop Entry]
Version=1.0
Name=Dmenu Browser Script
GenericName=Web Browser
# Gnome and KDE 3 uses Comment.
Comment=Access the Internet
Exec=$HOME/.local/bin/dmenu-browser %U
StartupNotify=true
Terminal=false
Icon=brave-browser
Type=Application
Categories=Network;WebBrowser;
MimeType=application/pdf;application/rdf+xml;application/rss+xml;application/xhtml+xml;application/xhtml_xml;application/xml;image/gif;image/jpeg;image/png;image/webp;text/html;text/xml;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ipfs;x-scheme-handler/ipns;
Actions=new-window;new-private-window;

[Desktop Action new-window]
Name=New Window
Exec=$HOME/.local/bin/dmenu-browser

[Desktop Action new-private-window]
Name=New Incognito Window
Exec=$HOME/.local/bin/dmenu-browser --incognito

It only works if I execute xdg-open from my command line. (I modified the .desktop file of brave-browser, because I had no clue how to write one.)

  • 2
    Why are you passing `"$@"` as arguments to `${DMENU}`? What is value of `${DMENU}`? – jhnc Jun 04 '22 at 19:52
  • DMENU="dmenu -i -l 20 -p" which is defined in my config – AzureOrange404 Jun 05 '22 at 18:34
  • @jhnc I don't know what I was thinking there. Anyways this works know. When I open a url using xdg-open in my terminal, it now runs my script, but if I try to open a url anywhere else (e.g. from thunderbird mail), is doesnt. – AzureOrange404 Jun 05 '22 at 18:42
  • Maybe `[ -t 0 ]` test is backwards. You say "xdg-open passes the url as an argument to the program". – jhnc Jun 05 '22 at 19:14
  • I'm not shure I can follow, I tried it without the whole if statement (because I would not pipe into the script anyways) and the problem stays the same. It works from command line but not in "real use" situations. – AzureOrange404 Jun 06 '22 at 09:43
  • Yes that's how I supposed it to work. This is how xdg-open works: https://gitlab.freedesktop.org/xdg/xdg-utils/-/blob/v1.1.3/scripts/xdg-open.in Mabe it isn't an xdg-open problem at all? I don't know :S – AzureOrange404 Jun 06 '22 at 09:54
  • maybe trace execution and see what it is actually doing. eg. `exec > >(tee /my/log/file) 2>&1; { set -vx; } 2>/dev/null` – jhnc Jun 06 '22 at 10:03
  • Looks like fundamentally another metastasis of https://mywiki.wooledge.org/BashFAQ/050 – tripleee Jun 06 '22 at 10:19
  • @tripleee it does work from command line (`xdg-open "URL"`), but it doesn't run the script at all if I click on a link somewhere else (e.g. in an Email), which starts xdg-open by default. – AzureOrange404 Jun 06 '22 at 11:53

0 Answers0