Advertisement
Guest User

default-email

a guest
Mar 30th, 2016
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.74 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ##############################################################################################
  4. # Running this script will launch user's default email client.
  5. ##############################################################################################
  6. # How it works:
  7. # 1. xdg-mime finds the .desktop file associated with sending email (e.g., icedove.desktop)
  8. # 2. the function finds the actual .desktop file (e.g., /usr/share/applications/icedove.desktop), extracts command_word from it (e.g., icedove)
  9. # 3. the function then uses "which" to get command_which (e.g., /usr/bin/icedove)
  10. # 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)
  11. ##############################################################################################
  12. # Notes:
  13. # - This script can be use to launch user's default app for something else by changing 'x-scheme-handler/mailto' as appropriate.
  14. # - The function is almost verbatim from xdg-email (I simply got rid of first_word--I find the head+cut combo much more clear)
  15. ##############################################################################################
  16.  
  17. desktop_file_to_binary()
  18. {
  19.     search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
  20.     # The :- is used to set fallback values. Therefore, if both $XDG_DATA_HOME and $XDG_DATA_DIRS are defined,
  21.     #    then the above is equivalent to "$XDG_DATA_HOME:$XDG_DATA_DIRS"
  22.     # If, on the other hand, neither of those environmental variables are set,
  23.     #    then the above is equivalent to "$HOME/.local/share:/usr/local/share:/usr/share"
  24.     desktop="`basename "$1"`"
  25.     IFS=:
  26.     for dir in $search; do
  27.         unset IFS
  28.         [ "$dir" ] && [ -d "$dir/applications" ] || continue
  29.         file="$dir/applications/$desktop"
  30.         [ -r "$file" ] || continue
  31.         # Remove any arguments (%F, %f, %U, %u, etc.).
  32.         command_word="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | head -n 1 | cut -d' ' -f 1`"
  33.         # The regex matches 'Exec' followed by optional square brackets (filled with optional junk) followed by '='
  34.         # Therefore, all of these would match: Exec=   Exec[]=   Exec[junk]=
  35.         # I'm not exactly sure why such ugly regex is necessary, as all of my .desktop files have a simple Exec=
  36.         command_which="`which "$command_word"`"
  37.         command_actual="`readlink -f "$command_which"`" # if $command_which is a symlink, $command_actual will be the link's target
  38.         return
  39.     done
  40. }
  41.  
  42. desktop_file="$(xdg-mime query default 'x-scheme-handler/mailto')"
  43. desktop_file_to_binary "$desktop_file"
  44. $command_actual &>/dev/null &
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement