Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ##############################################################################################
- # Running this script will launch user's default email client.
- ##############################################################################################
- # How it works:
- # 1. xdg-mime finds the .desktop file associated with sending email (e.g., icedove.desktop)
- # 2. the function finds the actual .desktop file (e.g., /usr/share/applications/icedove.desktop), extracts command_word from it (e.g., icedove)
- # 3. the function then uses "which" to get command_which (e.g., /usr/bin/icedove)
- # 4. for some major overkill, the function then uses "readlink" just in case command_which is a symlink and not the actual binary (as in this case--command_actual is /usr/lib/icedove/icedove)
- ##############################################################################################
- # Notes:
- # - This script can be use to launch user's default app for something else by changing 'x-scheme-handler/mailto' as appropriate.
- # - The function is almost verbatim from xdg-email (I simply got rid of first_word--I find the head+cut combo much more clear)
- ##############################################################################################
- desktop_file_to_binary()
- {
- search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
- # The :- is used to set fallback values. Therefore, if both $XDG_DATA_HOME and $XDG_DATA_DIRS are defined,
- # then the above is equivalent to "$XDG_DATA_HOME:$XDG_DATA_DIRS"
- # If, on the other hand, neither of those environmental variables are set,
- # then the above is equivalent to "$HOME/.local/share:/usr/local/share:/usr/share"
- desktop="`basename "$1"`"
- IFS=:
- for dir in $search; do
- unset IFS
- [ "$dir" ] && [ -d "$dir/applications" ] || continue
- file="$dir/applications/$desktop"
- [ -r "$file" ] || continue
- # Remove any arguments (%F, %f, %U, %u, etc.).
- command_word="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | head -n 1 | cut -d' ' -f 1`"
- # The regex matches 'Exec' followed by optional square brackets (filled with optional junk) followed by '='
- # Therefore, all of these would match: Exec= Exec[]= Exec[junk]=
- # I'm not exactly sure why such ugly regex is necessary, as all of my .desktop files have a simple Exec=
- command_which="`which "$command_word"`"
- command_actual="`readlink -f "$command_which"`" # if $command_which is a symlink, $command_actual will be the link's target
- return
- done
- }
- desktop_file="$(xdg-mime query default 'x-scheme-handler/mailto')"
- desktop_file_to_binary "$desktop_file"
- $command_actual &>/dev/null &
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement