[Tutorial] Improving Thunar in XFCE by opening new tabs instead of windows with Bash.

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
happysadhu

[Tutorial] Improving Thunar in XFCE by opening new tabs instead of windows with Bash.

Post by happysadhu »

The default of Thunar is to open a new window for each directory opened outside of the file manager. For example, right-clicking on a group of folders on the desktop and selecting "Open All" opens a separate window for each directory. Selecting a shortcut through the "Places" add-on on the launcher panel also results in a separate window. Because of this, I would end up with a confusing barrage of Thunar windows. To remedy this, I created a short bash script, "thunartab", inspired by this one: https://askubuntu.com/questions/55656/o ... ing-window

The "thunartab" script works for each separate workspace so that there can be a distinct instance of Thunar for each one. If you do want other instances of Thunar on the same workspace, just select "Thunar" in the panel start menu or type "thunar &" in the terminal. Right clicking on a directory in Thunar and selecting "Open as Root" will also open a new instance of Thunar but as root. Whatever Thunar window is raised receives the new tabs created from opening directories outside of Thunar including the Places add-on mentioned earlier.

I am hoping that in the next version of Thunar the one-window per workspace option will be added. Surprisingly, not many of Linux File managers have this one-window option, including recent versions of Nautilus, Nemo and Caja. However, the light-weight LXDE's file manager PCManFM does. As a side note, it should be fairly easy to modify "thunartab" to work with other file managers in XFCE (See comment section at bottom of script for example).

1. To use, after downloading the attached script into your Downloads folder, run in the terminal:

Code: Select all

cd ~/Downloads && tar -zxvf thunartab.tar.gz && sudo chmod 755 thunartab && sudo cp thunartab /usr/local/bin/
2. Install the following two minor dependencies:

Code: Select all

sudo apt install gridsite-clients xdotool
3. After rebooting, in XFCE's GUI "Preferred Applications", under "File manager" in the Utilities tab, choose "Other" and select/enter "thunertab". Now the command "exo-open --launch FileManager" will execute the thunartab script which means that selecting "File Manager" in the start menu will open a new tab in Thunar, if it is already running, instead of a new window. However, selecting "Thunar" in the menu will still start up a new instance of Thunar not a new tab, in case you want that.

Although I do have website coding experience with html, css, etc., I am a novice when it comes to computer programming languages. So, If you have any suggestions for improving the script please share!

BASH Script:

Code: Select all

#!/bin/bash
# Script to open new file manager tab instead of window (e.g., Thunar) in XFCE. 
# Version 1. 2017-04-18

# Lock script to process multiple instances consecutively when "Open All" on Desktop. 
if [[ $1 == "file:///home/$USER/Desktop/"* ]]; then
	ME=`basename "$0"`;
	LCK="/tmp/${ME}.LCK";
	exec 8>$LCK;
	flock -x 8;  # lock $LCK
	trap 'rm -f $LCK' exit # remove $LCK regardless of exit status
	sleep .1 # hack to give time for each script to execute properly--weird!
fi

# Convert desktop file urls to thunar-friendly local paths. 
# Accommodates special characters in directory names (!@#$, etc).
fileurl=$1
filepath="$(urlencode -d ${fileurl#file://})/"

# Check for running instances of $app on current desktop/workspace.
app=thunar
wid=( $(xdotool search --desktop $(xdotool get_desktop) --class $app) ) 
lastwid=${wid[*]: -1} # Get PID of newest active thunar window.

# If $wid is null launch app with filepath.
if [ -z $wid ]; then
	$app "$filepath"

# If app is already running, activate it and use shortcuts to paste filepath into path bar.
else  
	xdotool windowactivate --sync $lastwid key ctrl+t ctrl+l # Activate pathbar in thunar 
	xdotool type -delay 0 "$filepath" # "--delay 0" removes default 12ms between each keystroke
	xdotool key Return
fi
exit 0

# Easy Installation: 
# cd ~/Downloads && tar -zxvf thunartab.tar.gz && sudo chmod 755 thunartab && sudo cp thunartab /usr/local/bin/
# Install dependencies: sudo apt install gridsite-clients xdotool # gridsite-clients contains urlencode 
# Reboot # if you want to run "thunartab" as a command in terminal
# Select script in the XFCE's GUI "Preferred Applications"
#
# For alternatives for script location run "echo $PATH" (without quotes) in terminal
# Troubleshooting: If more than one thunar window opens with "Open All", try increasing sleep value on line 12
# 
# For other file-managers (e.g., Nemo, Caja, Nautilus), change app name on line 21 
# You may also have to modified  the xdotool shortcuts on line 31.
# For example for Nemo, change line 31 to something like
# xdotool windowactivate --sync $lastwid key ctrl+t ctrl+l ctrl+v Return ctrl+l Escape
# Manually test shortcuts in file manager first. 
# 
# Main sources for learning bash for this script include: 
# http://ryanstutorials.net/bash-scripting-tutorial/ "Bash Scripting Tutorial" (Very good for beginners like myself"
# http://jdimpson.livejournal.com/5685.html "Using flock to protect critical sections in shell scripts"
# https://askubuntu.com/questions/55656/open-nautilus-as-new-tab-in-existing-window (the starting idea for this script)
#
# Bug reports or suggestions, please email me, Sam, at sampub@islandroots.ca
Habitual

Re: [Tutorial] Improving Thunar in XFCE by opening new tabs instead of windows with Bash.

Post by Habitual »

I almost cried:

Code: Select all

# Main sources for learning bash for this script include: 
# http://ryanstutorials.net/bash-scripting-tutorial/ "Bash Scripting Tutorial" (Very good for beginners like myself"
# http://jdimpson.livejournal.com/5685.html "Using flock to protect critical sections in shell scripts"
# https://askubuntu.com/questions/55656/open-nautilus-as-new-tab-in-existing-window (the starting idea for this script)
I live for inspiration, daily.

Good use of good documentation style, IMO. Respect.

Can't wait to see your next post :idea:
happysadhu

Re: [Tutorial] Improving Thunar in XFCE by opening new tabs instead of windows with Bash.

Post by happysadhu »

"I almost cried". Funny!
Thanks for your encouraging comment, Habitual.
TechInMD
Level 1
Level 1
Posts: 4
Joined: Wed Apr 29, 2015 12:37 pm

Re: [Tutorial] Improving Thunar in XFCE by opening new tabs instead of windows with Bash.

Post by TechInMD »

This did exactly what I needed to do. Thank you! :D :D :D :D
happysadhu

Re: [Tutorial] Improving Thunar in XFCE by opening new tabs instead of windows with Bash.

Post by happysadhu »

Your welcome.

For firefox, I had to do the following so that "Open containing folder" on downloads launched the thunartab script instead of a new instance of thunar: reset the default application for the "inode/directory" mime type by choosing another default application and then reselecting "File Manager", using the MIME Type editor GUI which is already installed by default. Restart Firefox in between each change.

If reseting the MIME type still doesn't work try choosing "thunartab" (without quotes): click on the default application, select "Choose Application", then "custom command" and enter "thunartab". Restart Firefox.

I've read on a couple of older posts that Firefox sometime doesn't recognize the user-selected default File Manager. It seems by reseting the MIME type for "inode/directory" Firefox finally recognizes the change.
Post Reply

Return to “Tutorials”