pythonmouseeventnautilus

How to use python3.6 to extract filename from nautilus GUI when mouse pointer clicks on a file?


I would like to write a nautilus script using python3.6 that will extract the name of a file from nautilus when the file is clicked on by the mouse pointer. Which python module can I use, or how do I write such a script from scratch?

For this script to be executable, I will make the script to start with:

#!/usr/bin/env python3

Beyond that, I will need advice on how to do what I want.


Solution

  • Step 1: I created the python script as shown below and saved it in folder ~/.local/share/nautilus/script with a filename (here I called myscript.py):

    #!/usr/bin/env python3
    
    from pathlib import Path
    from os import environ
    
    a = Path('/home/user/tmp/name.txt')
    paths = environ['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'].splitlines()
    for p in paths:
        a.write_text(p)
    

    Step 2: I had to make the myscript.py executable with this terminal command:

    $ chmod +x ~/.local/share/nautilus/scripts/myscript.py