Ubuntu – How to access the locational data of the currently selected file or folder

command lineexecute-commandnautilus

I want to be able to select a file, run a script with a shortcut and that script gets the path of the file I've selected (or am dragging). I've selected it in nautilus.

I know how to run the command, I just don't know how to get the location of the selected file. I am assuming it is possible, because if I drag and drop a file to terminal, it pastes the path…


What did I use this for? I have a program called synergy which is for using 1 mouse and 1 keyboard across many computers, using a local network.

You can set it up to have your screens next to each other like so:

However, (because this is the free version), I can't drag and drop files between them. So this dock is set up between the two screens, and when I drag a file I can drop it there – and it copies it to the same path on the second computer (if it exists) or to the misc folder.

Best Answer

With a little reaserch, I found out what you may be looking for. This can be done using .desktop files. Let me demonstrate.

Create a file in your home diretory(as an example) with the extension .desktop with the following content:

#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Type=Application
Terminal=true
Exec=bash <path-to-your-script> %u
Name=visible name here
Comment=comment here
Icon=icon path here

Replace <path-to-your-script> with path of the script you want to execute. Here %u is the path of the file which you will be dragging on the .desktop file to trigger the script. This will obviously be passed as an argument to your script.

Don't forget to make your .desktop executable as well by:

chmod u+x test.desktop

To check if it is working you can point it to this script:

#!/bin/sh

echo $1 > ~/out.txt

You will see the full path of the file which you dragged onto your .desktop file stored in out.txt in you home directory.

Finally, you can set up a dock to have just this in it, so you have it at the side of the screen.

References:

Desktop Entry Specification

Gnome Developer - Desktop Files