Advertisement
anjishnu

thunar-terminal.sh

Nov 25th, 2011
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.74 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ## Works best when using Thunar > View > Location Selector > Toolbar Style
  4. ## Requirements: xdotool, xclip, xfce4-terminal
  5.  
  6. # Todo:
  7. # *) Support for other terminal other than xfce4-terminal?
  8. # *) [Done] Open terminal when viewing remote desktop
  9. #   Was having problem in opening to the correct directory if opened via
  10. #   gsshfs. However fixed in version 0.4.
  11. # *) [Done] Check for dependencies
  12. # *) [Done] Write installation and HelpText
  13. # *) Bug: Doesn't always close the "Open Location" window when Thunar has
  14. #       "Pathbar Style" location. In other words, inconsistence behaviour
  15. #       for "Pathbar Style".
  16. # *) [Done] Bug: Opening terminal to $HOME even when thunar not active
  17. #       -> Typo was removed.
  18. # *) [Done] Activate terminal window after opening
  19. #       -> Ugly hack
  20.  
  21. ScriptName=$(basename $0)
  22. Author="Anjishnu Sarkar"
  23. Version="0.4"
  24. Requirements="xdotool xclip xfce4-terminal"
  25. HelpText="$ScriptName: Script to open a terminal in the same directory as
  26. thunar file manager via shortcut key. Written specifically for openbox.
  27. Can be configured to use in other windows managers as well.
  28.  
  29. The script has support for opening remote folders too.
  30. If thunar is viewing remote desktop and has the location bar in the format
  31. sftp://username@server/remote/home/some/folder
  32. then pressing \"F4\" in that directory, opens a terminal via ssh.
  33.  
  34. Requirements are $Requirements.
  35. Author: $Author, Version: $Version
  36.  
  37. Installation:
  38. Make the script executable
  39.    chmod +x ${ScriptName}
  40. Copy it in your \$PATH
  41.    cp thunar-terminal.sh ~/bin/
  42.  
  43. Openbox configuration:
  44. Open the file ~/.config/openbox/rc.xml and copy these lines under
  45. <!-- Keybindings for running applications -->
  46.    <keybind key=\"F4\">
  47.      <action name=\"execute\">
  48.        <execute>thunar-terminal.sh</execute>
  49.      </action>
  50.    </keybind>
  51.  
  52. Thunar configuration:
  53. Works best if \"Location Selector\" of thunar is in \"Toolbar Style\".
  54. Thunar > View > Location Selector > Toolbar Style.
  55.  
  56. Restart X (Logout and login). Now open thunar, move to any directory and
  57. press \"F4\" to open a terminal in the same directory.
  58. "
  59. ErrMsg="$ScriptName: Unspecified option. Aborting."
  60.  
  61. while test -n "$1"
  62. do
  63.     case "$1" in
  64.         -h|--help)  echo -n "$HelpText"
  65.                     exit 0 ;;
  66.  
  67.         *)          echo "$ErrMsg"
  68.                     exit 1 ;;
  69.     esac
  70.     shift
  71. done
  72.  
  73. for Software in $Requirements
  74. do
  75.     if ! which ${Software} >/dev/null;then
  76.         echo "The software \"${Software}\" is not installed. Aborting."
  77.         exit 1
  78.     fi
  79. done
  80.  
  81. ## Get current active window
  82. ActiveWinID=$(xdotool getactivewindow)
  83.  
  84. ## Get all thunar windows ids
  85. # WindowIDS=$(xdotool search --class --name ".*File Manager")
  86. WindowIDS=$(xdotool search --class "Thunar")
  87.  
  88. ## Check whether the active window id actually belongs to a thunar window
  89. ## or not
  90. for WinID in $WindowIDS
  91. do
  92.     ## If activewindow is same as thunar window, then
  93.     if [ "$WinID" == "$ActiveWinID" ];then
  94.         echo "Found active thunar window."
  95.         ## Get thunar pathbar
  96.         xdotool key --window "$ActiveWinID" --clearmodifiers "Ctrl+l"
  97.  
  98.         ## Select all
  99. #         xdotool key --window "$ActiveWinID" --clearmodifiers "Ctrl+a"
  100.  
  101.         ## Save old clipboard value
  102.         OldClip=$(xclip -o)
  103.  
  104.         ## Copy thunar directory path to clipboard
  105.         xdotool key --window "$ActiveWinID" --clearmodifiers "Ctrl+c"
  106.  
  107.         ## Should remove the pathbar if selected
  108.         xdotool key --window "$ActiveWinID" --clearmodifiers "Return"
  109. #         xdotool search --name "Open Location" windowkill
  110.  
  111.         ## Copy the path to variable
  112.         Location=$(xclip -o)
  113.  
  114.         ## Restore old clipboad value
  115.         echo "$OldClip" | xclip -i
  116.  
  117.         ## If connected to remote server, grab user name and server
  118.         User_Server=$(echo "$Location" | sed 's/\//\n/g' | grep '@' \
  119.             | cut -d: -f1)
  120.  
  121.         if [ -z "$User_Server" ];then
  122.             ## Open terminal in location
  123.             xfce4-terminal --working-directory="$Location" &
  124.             PID=$(echo "$!")
  125.         else
  126.             WorkingDir="$(echo "$Location" | sed 's/^.*@//' \
  127.                | sed 's/::/\//' | sed 's/:/\//g' \
  128.                | sed 's/\//@/' \
  129.                | sed 's/^.*@/\//' | sed 's/ /\\ /g')"
  130.             xfce4-terminal -x ssh -tX "$User_Server" "cd ${WorkingDir} 2>/dev/null; bash" &
  131.             PID=$(echo "$!")
  132.         fi
  133.  
  134.         ## Wait before searching for the latest window
  135.         sleep 0.5s
  136.         XtermID=$(xdotool search --pid "$PID" --class "Xfce4-terminal" \
  137.             | tail -n 1)
  138.  
  139.         ## Activate latest window
  140.         xdotool windowactivate "$XtermID"
  141.         exit 0
  142.     fi
  143. done
  144.  
  145. echo "Thunar window was not active. Aborting."
  146. exit 1
  147.  
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement