Last active
January 4, 2025 15:43
-
-
Save paloha/17fa21cc7719b43a9d92c11f09bc25c9 to your computer and use it in GitHub Desktop.
GNOME Nautilus extension to support a keyboard shotrcut to open Guake terminal in current directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################# | |
# Author: Pavol Harar ([email protected]) | |
# Created: 04. January 2024 | |
# Licence: MIT | |
# Prerequisits: `apt install python3-nautilus gir1.2-gtk-4.0` | |
# Copy this script into `~/.local/share/nautilus-python/extensions/` | |
# Kill all Nautilus processes with `nautilus -q` | |
# Restart Nautilus | |
# Tested on Ubuntu 24.04.01 | |
# At the time of writing and testing this extension, I already had the | |
# nautilus-open-any-terminal extension installed (may or may not be relevant) | |
# | |
# In case you navigate to places that do not have a defined path, such as | |
# Recent, or Starred, the extension opens the terminal in the last valid CWD. | |
# | |
# Relevant docs: | |
# https://gnome.pages.gitlab.gnome.org/nautilus-python/ | |
# https://docs.gtk.org/gtk4/method.Application.set_accels_for_action.html | |
############################################################################# | |
import gi | |
import os | |
import subprocess | |
gi.require_version('Nautilus', '4.0') | |
gi.require_version('Gtk', '4.0') | |
from gi.repository import Nautilus, GObject, Gio, Gtk | |
class ProvideCWDOnFocus(GObject.GObject, Nautilus.MenuProvider): | |
def __init__(self): | |
super().__init__() | |
# Variable to store the previous CWD | |
self.previous_cwd = None | |
# Create keybindings | |
self.setup_keybindings() | |
def setup_keybindings(self): | |
""" | |
Setup custom keybindings for the extension. | |
""" | |
app = Gtk.Application.get_default() | |
if app is None: | |
print("No Gtk.Application found. Keybindings cannot be set.") | |
return | |
# Create a custom action | |
action = Gio.SimpleAction.new("open_guake", None) | |
action.connect("activate", self.on_open_guake) | |
app.add_action(action) | |
# Set the keybinding | |
desired_keybindings = ["<Alt>T"] # zero or more | |
app.set_accels_for_action("app.open_guake", desired_keybindings) | |
print(f'Keybinding {desired_keybindings} registered for opening Guake in CWD.') | |
def on_open_guake(self, action, param): | |
""" | |
Action handler for the keybinding to open a Guake tab in the current directory. | |
""" | |
if self.previous_cwd: | |
# Open a new Guake tab in the current CWD | |
print(f"Opening Guake tab in: {self.previous_cwd}") | |
subprocess.run(["guake", "--show", "--new-tab", self.previous_cwd]) | |
else: | |
print("No CWD available to open Guake tab") | |
def get_background_items(self, current_folder: Nautilus.FileInfo): | |
""" | |
It seems this function is triggered everytime a focus is changed. I.e. seems to | |
be called when the folder view gains focus or when the user navigates to a different | |
folder (e.g., by double-clicking a folder or clicking a breadcrumb). | |
This probably happens because Nautilus refreshes the context menu dynamically. | |
""" | |
# Update the current working directory | |
if current_folder is not None: | |
folder_path = Gio.File.new_for_uri(current_folder.get_uri()).get_path() | |
if folder_path: | |
# Check if the path is different from the previous CWD | |
if folder_path == self.previous_cwd: | |
return [] # Path hasn't changed | |
self.previous_cwd = folder_path | |
print(f"Updated CWD to: {self.previous_cwd}") | |
return [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment