Page Body

Copy Object's Absolute Path From GNOME Files With AutoKey and Python

If you use older versions of the GNOME desktop and are looking for a way to copy a file system object's absolute path from GNOME Files, AutoKey and Python can provide a solution.

GNOME Files

GNOME Files (also referred to as Nautilus) is the default file manager for the GNOME desktop. It offers many useful functions, but directly copying a file system object's absolute path is only supported by newer versions.

For example, after selecting a file system object, you can press Control+l to be provided with the current directory's path, but that only gets you part of the way towards obtaining the object's absolute path. If you copy a file system object and then paste the contents of the clipboard into a text editor, older versions of GNOME Files will produce something like this:

x-special/nautilus-clipboard
copy
file:///home/amnesia/Downloads

The file:///home/amnesia/Downloads string contains the file URI for the selected file system object. This is close to the object's absolute file path, but we still need to extract it from the clipboard content and transform it into a format that is appropriate for the Bash shell and other applications expecting an object absolute path.

AutoKey and Python can help us accomplish this.

Serpentine Automation

After a file system object has been copied to the clipboard, we can use a Python script tied to an AutoKey Hotkey to do automation magic.

The file URI has percent encodings, which we need to remove. We can use the urllib.parse module's unquote() function to address this:

# Enable replacing %xx escapes with their single character equivalents
from urllib.parse import unquote

We need to define the characters in the file URI that are special shell characters and specify the escaped versions of the characters that we want to replace them with. We can do that with a dictionary:

# Define special shell characters to escape
chars_to_esc = {ord('<'): '\<', ord('>'): '\>', ord('|'): '\|',
                ord('*'): '\*', ord('?'): '\?', ord('['): '\[',
                ord(']'): '\]', ord('{'): '\{', ord('}'): '\}',
                ord('~'): '\~', ord('$'): '\$', ord('='): '\=',
                ord('('): '\(', ord(')'): '\)', ord('!'): '\!',
                ord("'"): "\\'", ord('"'): '\\"', ord('#'): '\#',
                ord('&'): '\&', ord(';'): '\;', ord(' '): '\ '}

Next, we get the clipboard's content using Autokey's clipboard.get_clipboard():

cb = clipboard.get_clipboard() # Save clipboard contents

To only extract the path that we are interested in, we find the starting index of the file:// string with the str.find() string method, and then use that index to slice the file URI from the copied clipboard content:

cb = clipboard.get_clipboard()  # Save clipboard contents
f_index = cb.find('file://')  # Find starting index of file:// substring
file_uri = cb[f_index + 7:]  # Extract file URI

Previously, we set the chars_to_esc dictionary's keys to be the result returned from passing the special shell characters to the ord() function. Given a string representing one Unicode character, ord() returns an integer representing the Unicode code point of that character.

By using a Unicode ordinal as the index for chars_to_esc, we can now pass it to Python's str.translate() string method as a mapping, i.e., for every character in the string that we call str.translate() on, it will map the Unicode ordinal to its associated, escaped value in chars_to_esc, and return the escaped value:

# Create a clean absolute file path string
clean_path = unquote(file_uri).translate(chars_to_esc)

Last, we save the absolute file path to the clipboard after we remove the trailing newline using the str.rstrip() string method:

# Save absolute file path to clipboard, remove newline
clipboard.fill_clipboard(clean_path.rstrip())

To put this all together, open AutoKey, select New > Script, name your Python script (e.g., get_object_path), and paste in:

# Enable replacing %xx escapes with their single character equivalents
from urllib.parse import unquote

# Define special shell characters to escape
chars_to_esc = {ord('<'): '\<', ord('>'): '\>', ord('|'): '\|',
                ord('*'): '\*', ord('?'): '\?', ord('['): '\[',
                ord(']'): '\]', ord('{'): '\{', ord('}'): '\}',
                ord('~'): '\~', ord('$'): '\$', ord('='): '\=',
                ord('('): '\(', ord(')'): '\)', ord('!'): '\!',
                ord("'"): "\\'", ord('"'): '\\"', ord('#'): '\#',
                ord('&'): '\&', ord(';'): '\;', ord(' '): '\ '}

cb = clipboard.get_clipboard()  # Save clipboard contents
f_index = cb.find('file://')  # Find starting index of file:// substring
file_uri = cb[f_index + 7:]  # Extract file URI

# Create a clean absolute file path string
clean_path = unquote(file_uri).translate(chars_to_esc)

# Save absolute file path to clipboard, remove newline
clipboard.fill_clipboard(clean_path.rstrip())

Set a Hotkey for the script (e.g., Control+Alt+p) and select Save.

Now, you should be able to utilize a workflow like the following:

  1. Select a file system object in GNOME Files.
  2. Press Control+c to copy it to the clipboard.
  3. Press Control+Alt+p (or your preferred Hotkey) to trigger the get_object_path AutoKey Python script, which overwrites the clipboard contents with the object's absolute file path.
  4. Switch to the application where you want to paste the object's absolute path.
  5. Press Control+v (or the appropriate paste command for the application of interest) to paste the object's absolute file path.

The Benefits of Free/Libre Open Source Software (FLOSS)

By themselves, FLOSS projects can be helpful, empowering tools. An additional benefit is that they can be combined to create custom workflows that provide value for niche use cases. This is an important point to keep in mind when choosing the tool set you use for your next project.

Enjoyed this post?

Subscribe to the feed for the latest updates.