Skip to content

Instantly share code, notes, and snippets.

@achilleas-k
Last active April 15, 2020 03:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achilleas-k/dec9143224fc94a8a3a1176390220a35 to your computer and use it in GitHub Desktop.
Save achilleas-k/dec9143224fc94a8a3a1176390220a35 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
shopt -s nocasematch # for case test
browser="/usr/bin/qutebrowser --target window"
URL=$1
case "${URL}" in
*.jpg | *.jpeg | *.png)
prog="feh --scale-down"
icon="feh"
message="Opening image in feh"
;;
*youtube* | *youtu.be* | *.mp4 | *streamable* | *gfycat.com* | *tiktok.com*)
prog="mpv"
icon="mpv"
message="Opening video in mpv"
;;
*.gif | *.gifv)
prog="mpv --loop"
icon="mpv"
message="Opening gif in mpv"
;;
*.pdf | *.md)
prog="okular"
icon="okular"
message="Opening document in Okular"
;;
*)
prog=${browser}
icon="qutebrowser"
message="Opening URL in Qutebroser"
;;
esac
# rest of args are ignored
if [ -e "${URL}" ]; then
# file or dir; open with file://<absolute path>
URL=$(realpath "${URL}")
URL="file://${URL}"
fi
ecode=0
notify-send --app-name="URL handler" --icon="${icon}" "URL handler" "${message}"
stderr=$(${prog} "${URL}" 2>&1) || ecode=$?
if [[ ${ecode} != 0 ]]; then
# TODO: Add icon
notify-send "Failed to open URL" "Command ${prog} ${URL} failed:\n${stderr}"
# default to browser
${browser} ${URL}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment