#!/bin/bash
# Name: open-dirs-in-new-tabs
# Brief:
# In Nautilus, open a list of directories in new Tabs .
# The list is hard-coded into the script, but can be easily adabpted to read from a file.
# ~/ will be expanded, but $variables are treated as a literals.
# This script is intended to be run via nautilus-scripts
# sudo apt-get install nautilus-scripts-manager
#
# 'nautilus-scripts' was chosen because its menu items
# are available in Nautilus Main-menu - File (and the context menu)
# Main-menu items can have shortcut keys assigned to them.
# eg. I've assigned Alt+1 to this nautilus-script
#
# To assign a shortcut key locally (local to Nautilus),
# use 'Editable Menu Accelerators' ... For details see:
#
http://askubuntu.com/questions/5241/ubuntu-editable-menu-accelerators-on-a-per-app-basis-where-is-this-option #
# The script works in a brute-force fashion by sending keys-strokes
# to the 'active' window (which in this case is Nautilus).
# It sends a <Control>t to open an New Tab, so if you have a differnt
# key bound to this action, you must change this script to match... (see $newTab)
# The script also sends <Control>l to briefly activate 'text mode address',
# so likewise, this key-binding must match our key-binding......... (see $goLocn)
# The script also sends <Control>PgUp to briefly activate 'text mode address',
# so likewise, this key-binding must match our key-binding......... (see $goLocn)
#
# =================================================================
# Purpose:
# Open extra Tabs in Nautilus.
# This is intended for use *immediately* after initial startup.
# but it will also work if used immediately after
# your home bookmark is activated, and no files are selected.
#
# This script is intended to be triggered via a short
# sudo apt-get install nautilus-scripts-manager
#
# =================================================================
# First, try to detect a "Nautilus has just started" state.
# This is full of holes, but it is good enough(?) for this script
# as it only opens new tabs (ie. No dire consequences)
# Note: Because this script is seperate process to Nautilus,
# a different window may somehow become the active.
# This unexpected active window would then receive the key
# sent by this script.. This could cause loss of data!!!
# To mitigate this to some degree, the script checks that
# it is stil on a (not "the") Nautilus window at the top
# or the open New Tab loop... (It's not perfect solution)
# Exit if current directory is not $HOME
[[ "$NAUTILUS_SCRIPT_CURRENT_URI" != "file://$HOME" ]] && exit
# Exit if any file is currently selected
[[ "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" != "" ]] && exit
newTab=ctrl+t
goLocn=ctrl+d
prevTab=ctrl+Prior
ctTab=0
# Process the list of directories
while read dir
do
# Check that we are active on a Nautilus window
activeTitle=$(xwininfo -id "$(xdotool getactivewindo
w)" |sed -n \
"2s/^xwininfo: Window id: \(0x[[:xdigit:]]\+\) \x22\(.*\)\x22$/\2/p")
[[ ! $activeTitle =~ .*\ -\ File\ Browser$ ]] && exit
sleep .1
#
# I don't know why, but sometimes xdotools exits with
# a modifier key in a keydown state, so I've added substantial delays
# ... it seems to do the trick (on my box).
dir="${dir/~\//$HOME/}"
sleep .1
xdotool key --delay 100 $newTab
sleep .1
xdotool key --delay 100 $goLocn
sleep .1
xdotool type "$dir"
sleep .1
xdotool key --delay 100 Return
sleep .3
ctTab=$((ctTab+1))
done <<END
# The original had ~/Documents in so I have left that one in
# and added the rest and commented out the /media/dat_ext4/podcast
~/Downloads
~/Documents
~/Pictures
~/Win-Storage
~/Win-MyDocs
~/Win-Video
# /media/dat_ext4/podcast
END
while ((ctTab>0)) ; do
xdotool key --delay 100 $prevTab
sleep .1
ctTab=$((ctTab-1))
done
exit