1

I want to make a .desktop file that opens a folder in my default file manager in a new tab. To test if it opened the right location, I tried nautilus ~/*folder*. That opens my file manager, but it says that something went wrong. The same happens when I use nautilus /home/*username*/*folder*. What command can I use to open a specific folder in home in a new tab from a .desktop file?

My goal is to have an icon on my dock (plank) that opens the map ~/Downloads in a new tab in Nautilus.

Jacob Vlijm
  • 85,475
Maud Kon
  • 561

2 Answers2

4

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

  1. Install (if necessary) both wmctrl and xdotool:

    sudo apt-get install xdotool wmctrl
    
  2. 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)

  3. 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

  • The script is a workaround, to simulate user actions. As a result, the timing "inside" the script is important. I set timing to "safe" values on my system, and made it "smart" if possible. If it might not work correctly on your system, leave a comment.
  • The script will possibly brake if the targeted window (to add the tab to) is on another workspace
  • In case of directories with spaces, use quotes:

    nautilus_tab '</directory/with spaces>'
    

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.

Jacob Vlijm
  • 85,475
0

(These sites drive me crazy with their weird requirements - this should be a comment, but I don’t have enough reputation and this kind of nonsense doesn’t encourage me to get it!)

Jacob’s answer does not work for paths with spaces in them. It requires two changes...

run("nautilus "+arg)
run('nautilus "'+arg+'"')

run("xdotool type "+arg)
run('xdotool type "'+arg+'"')

I run it with Thunar though and it doesn’t interpret ~ well. E.g. thunar-tab.py "~/Documents" will produce an error that /home/user/~/Documents cannot be found. You have to call it with an explicit path.