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.

12
  • Do you want a new tab or a new window?
    – Pilot6
    May 25, 2015 at 16:56
  • 1
    nautilus does not support opening a directory in a new tab from command line, only in a new window. It can be faked however, but what is the error it gives? (could you @ping me if you addres a comment to me, or anyone else? Otherwise I'd have to keep all posts opened :) ) May 25, 2015 at 17:25
  • possible duplicate of open nautilus as new tab in existing window
    – n0noob
    May 25, 2015 at 17:39
  • @n0noob and other voters (@muru?): There is a subtle difference: the supposed dupe asks for opening new directories anyway, not about opening a specific directory in a new tab. The answer there offers to do that, (although dupes are about questions), but moreover, I couldn't get it working there. May 26, 2015 at 7:09
  • @JacobVlijm Yes, and I voted to close as that is a more general question, with a possible merging of questions to get your answer there.
    – muru
    May 26, 2015 at 8:14

2 Answers 2

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.

11
  • @MaudKon Just curious, but did you try it? Jun 2, 2015 at 8:18
  • Perhaps you should go back to using proper lists for call instead of [bash, -c, "big huge command"]
    – muru
    Feb 17, 2016 at 2:43
  • 1
    Hi @Sadi updated. See the remark in the updated section. Mar 27, 2016 at 15:25
  • 1
    @Sadi exactly. The timing needs to be surprisingly slow on this system, but I need to try on my other systems once I can (tomorrow). This is my 12 years old laptop :) Wow, never noticed the link! Mar 27, 2016 at 15:42
  • 1
    @Sadi great that you found qle! It was my very first project ever. I am working on a new version, but the old version still works fine over the Ubuntu versions since 11.04 :) Mar 27, 2016 at 15:49
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.

7
  • Try without quoting ~: thunar-tab.py ~/Documents
    – muru
    Feb 17, 2016 at 2:44
  • Since my aim is to support paths with spaces, quoting is required. Feb 17, 2016 at 2:59
  • That doesn't mean you have to quote the entire thing: ~/"foo bar"/'blah!blah'
    – muru
    Feb 17, 2016 at 2:59
  • I think that’s getting more complicated than just putting the home folder in a script file several times. Feb 17, 2016 at 3:02
  • If you're calling it from a script, make use of the $HOME variable: "$HOME/foo bar".
    – muru
    Feb 17, 2016 at 3:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .