Keyboard shortcut/command for opening a new nautilus window and raise it

Solution 1:

By default, the keyboard shortcut to launch the file browser is the special key Explorer, available on some keyboards (along with other special keys to laucnh music players, internet browsers and so on). You can change this to something else in the Shortcuts section of Keyboard settings:keyboard shortcut screenshot Change the Home folder shortcut to something else.

Solution 2:

To open a general window (12.04 / 14.04+)

I assume you would like to have a key combination to open a new Nautilus window (and raise it), no matter what application is in front.

You can do that, using a small script and adding a key combination to your shortcuts to run it.

  • If not installed, install wmctrl:

    sudo apt-get install wmctrl
    
  • Save the script below as "new_window" and make it executable. The script has a small difference for 14.04 or 12.04:

For 12.04:

#!/usr/bin/env python
import subprocess
import socket

def read_windowlist():
    get_pid = subprocess.Popen(["wmctrl", "-l", "-p"], stdout=subprocess.PIPE)
    wlist = [(item[14:21].split(" ")[0], item.split(socket.gethostname()+" ")[-1]) for item in get_pid.communicate()[0].decode("utf-8").split("\n")]
    return wlist

def read_pid():
    get_pid = subprocess.Popen(["pidof", "nautilus"], stdout=subprocess.PIPE)
    return get_pid.communicate()[0].decode("utf-8").replace("\n", "")

def find_window():
    nautilus_window = [item for item in read_windowlist() if item[0] == read_pid()][-1]               
    subprocess.Popen(["wmctrl", "-a", nautilus_window[1]])
    subprocess.Popen(["nautilus"])

find_window()

For 14.04+:

#!/usr/bin/env python3
import subprocess
import socket

def read_windowlist():
    get_pid = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8").split("\n")
    return [(item[14:21].split(" ")[0], item.split(socket.gethostname()+" ")[-1]) for item in get_pid]

def read_pid():
    get_pid = subprocess.Popen(["pidof", "nautilus"], stdout=subprocess.PIPE)
    return get_pid.communicate()[0].decode("utf-8").replace("\n", "")

def find_window():
    nautilus_window = [item for item in read_windowlist() if item[0] == read_pid()][-1]                       
    subprocess.Popen(["wmctrl", "-a", nautilus_window[1]])
    subprocess.Popen(["nautilus", "--new-window"])

find_window()

Now add a command and a key combination of your preference to run the script (System Preferences > Keyboard > Shortcuts > Custom Shortcuts)

The commmand:

/path/to/script/new_window (don't forget to make the script executable)

Open a new window in a specific directory (14.04+)

To open a specific directory in nautilus on top of everything, call the small script below to open the directory (under your key combination) with the directory as an argument, so that instead of using the command:

nautilus <directory>

to open the directory, you can use the command:

python3 <script> <directory>

How to use
This script also uses wmctrl. To install it:

sudo apt-get install wmctrl

The script

#!/usr/bin/env python3

import subprocess
import sys

window = sys.argv[1]

subprocess.call(["nautilus", window])
wname = '"'+window.split("/")[-1]+'"'
subprocess.Popen(["wmctrl", "-a", wname])

Copy it into an empty file, save it as raise_nautilus.py and run it by the command:

python3 /path/to/raise_nautilus.py </path/to/folder/to/open>

*Note: if your directory contains spaces, use quotes around it.