Skip to content

Instantly share code, notes, and snippets.

@vv01f
Last active August 29, 2015 14:01
Show Gist options
  • Save vv01f/fb0c99a373a1ac8ddbbf to your computer and use it in GitHub Desktop.
Save vv01f/fb0c99a373a1ac8ddbbf to your computer and use it in GitHub Desktop.
howto: wie ein Dateityp in GNU/Linux (Bsp.: Debian Wheezy, Gnome) mit Shell/Bash-Script verknüpft werden kann

GNU/Linux (Debian Wheezy, Gnome): Dateityp mit Shell/Bash-Script verknüpfen

1. Script (showurl.sh) zum Öffnen der URL aus der Datei

#!/usr/bin/env bash
#----
# FUNCTIONS
#--
# Checks for known desktop environments
# set variable DE to the desktop environments name, lowercase
# from: xdg-open
#--
detectDE()
{
    # see https://bugs.freedesktop.org/show_bug.cgi?id=34164
    unset GREP_OPTIONS

    if [ -n "${XDG_CURRENT_DESKTOP}" ]; then
      case "${XDG_CURRENT_DESKTOP}" in
         GNOME)
           DE=gnome;
           ;;
         KDE)
           DE=kde;
           ;;
         LXDE)
           DE=lxde;
           ;;
         XFCE)
           DE=xfce
      esac
    fi

    if [ x"$DE" = x"" ]; then
      # classic fallbacks
      if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
      elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
      elif [ x"$MATE_DESKTOP_SESSION_ID" != x"" ]; then DE=mate;
      elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
      elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
      elif xprop -root 2> /dev/null | grep -i '^xfce_desktop_window' >/dev/null 2>&1; then DE=xfce
      fi
    fi

    if [ x"$DE" = x"" ]; then
      # fallback to checking $DESKTOP_SESSION
      case "$DESKTOP_SESSION" in
         gnome)
           DE=gnome;
           ;;
         LXDE)
           DE=lxde; 
           ;;
         xfce|xfce4)
           DE=xfce;
           ;;
      esac
    fi

    if [ x"$DE" = x"" ]; then
      # fallback to uname output for other platforms
      case "$(uname 2>/dev/null)" in 
        Darwin)
          DE=darwin;
          ;;
      esac
    fi

    if [ x"$DE" = x"gnome" ]; then
      # gnome-default-applications-properties is only available in GNOME 2.x
      # but not in GNOME 3.x
      which gnome-default-applications-properties > /dev/null 2>&1  || DE="gnome3"
    fi
}
# /FUNCTIONS
#----
# MAIN
#----
# debug-option
#set -x
#--
# get URL in file into a variable
url=$(strings "$1" | grep URL= |sed  -e 's/URL=//')
#--
# print message on tty
#echo opening URL: $url ...
#--
# todo: identify DE/Session and handle unexpected ones
# with detectDE
#--
# open file/path in standard application
xdg-open $url
#----
# /MAIN

2. Script als "Applikation" in Gnome registrieren

  • (alacarte installieren: apt-get install alacarte)
  • "alacarte" alias "Menu" / "Hauptmenü" starten
  • Schaltfläche "Neuer Eintrag"
    • Typ: Anwendung
    • Name: OpenURL
    • Befehl: /admin/showurl.sh %u
    • Kommentar: Open URL file to be opened with Standard Application

3. Dateityp eintragen falls noch nicht vorhanden

  • (assoGiate installieren: apt-get install assogiate)
  • "assoGiate" alias "File Types Editor" starten
  • All types / x-mswinurl hinzufügen/auswählen, Rechtsklick, Edit
  • im Register "Filenames" den Eintrag "*.url" sicherstellen

4. Datei-Typ mit Anwendung verknüpfen

  • URL-Datei auswählen
  • Rechtsklick, Eigenschaften
  • Register "Öffnen mit", Schaltfläche "Andere Anwendungen"
  • "OpenURL" auswählen und mit "Als Vorgabe festlegen" bestätigen

5. Standard-Browser festlegen

  • Anzeigen auf Shell:
update-alternatives --display x-mswinurl
  • Ändern auf Shell:
update-alternatives --config x-www-browser

6. Test

Die URL-Datei sollte sich jetzt in der Grafischen Oberfläche öffnen lassen und der Link mittels des Standard-Browsers geöffnet werden.

7. Quellen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment