EDIT:
As a result of ongoing insight, below an improved version of the script.
Due to the fact that the script now pastes the path instead of typing it (using xdotool
), the script is more reliable. Although the script is not actually faster (consider it is a work around, due to nautilus' lack of cli options) the script "feels" more "crispy". Fact remains however that the script simply needs to wait for the GUI to be ready for each next step, since it simulates user actions. Timing inside the script is on the save side; on faster systems, one might be able to do some optimization.
Use the script exactly as explained in the original answer; it (still) needs both wmctrl
and xdotool
to be installed. This version furthermore rquires xclip
:
sudo apt-get install xclip
The new version of the script:
#!/usr/bin/env python3
import subprocess
import sys
import time
arg = sys.argv[1:]
arg = arg[0] if arg else ""
def get(cmd):
return subprocess.check_output(cmd).decode("utf-8").strip()
try:
pid = get(["pidof", "nautilus"])
except subprocess.CalledProcessError:
wlist = []
else:
wlist = [l for l in get(["wmctrl", "-lp"]).splitlines() if pid in l\
and "_NET_WM_WINDOW_TYPE_NORMAL" in get(["xprop", "-id", l.split()[0]])]
if wlist:
w = wlist[-1].split()[0]
subprocess.call(["wmctrl", "-ia", w])
subprocess.call(["/bin/bash", "-c", "printf '"+arg+"' | xclip -selection clipboard"])
subprocess.Popen(["xdotool", "key", "Control_L+t"])
time.sleep(0.4)
subprocess.Popen(["xdotool", "key", "Control_L+l"])
time.sleep(0.2)
subprocess.call(["xdotool", "key", "Control_L+v"])
subprocess.Popen(["xdotool", "key", "Return"])
else:
subprocess.Popen(["nautilus", arg])
Old answer:
Nautilus does not have a command line option to open a new tab, however, you can "fake" it with the help of a script, using xdotool
and wmctrl
.
How to use
Install (if necessary) both wmctrl
and xdotool
:
sudo apt-get install xdotool wmctrl
Copy the script below into an empty file, save it as nautilus_tab
(no extension) in ~/bin
, and make it executable.
If the directory ~/bin
didn't exist yet, create it, and run source ~/.profile
to make the directory "show up" in $PATH
. (or alternatively, log out/in)
Test-run it by running the command:
nautilus_tab <directory>
It should:
- if no nautilus window is open, open a new nautilus window in the directory
- if a window is open, open a new tab in the directory
The script
#!/usr/bin/env python3
import subprocess
import time
import sys
get = lambda cmd: subprocess.check_output(cmd).decode("utf-8")
def run(cmd):
subprocess.call(cmd)
try:
arg = sys.argv[1]
except:
arg = ""
try:
pid = get(["pidof", "nautilus"]).strip()
except subprocess.CalledProcessError:
run(["nautilus ", arg])
else:
w = [l.split() for l in get(["wmctrl", "-lp"]).splitlines() if pid in l][-1]
print(w)
w_id = w[0]
if len([l for l in get(["xprop", "-id", w_id]).splitlines() if all(
["_NET_WM_WINDOW_TYPE(ATOM)" in l, "_TYPE_NORMAL" in l])]) != 0:
run(["wmctrl", "-ia", w[0]]); time.sleep(0.1)
run(["xdotool", "key", "Control_L+t"])
if arg != "":
run(["xdotool", "key", "Control_L+l"])
time.sleep(0.2)
run(["xdotool", "type", arg])
time.sleep(0.02*len(arg))
run(["xdotool", "key", "Return"])
else:
run(["nautilus", arg])
Notes
If no argument (-directory) is used, the script will open a new tab in the same directory as the currently opened nautilus
window. If no `nautilus window was opened, a new window will open in your home directory.
~/Downloads
) – Jacob Vlijm May 26 '15 at 08:37