3

I am trying to write a custom command in Gedit which copies the currently open and active document's path(both upto parent dir & upto file) to clipboard, as I couldn't find any gedit-plugins or tools that can do this.

I have no clue yet as to where to start from, nor have any good references, but I know I have to do scripting in bash script.

I searched for external command to copy any string to clipboard from terminal(as it also runs bash script) but the answers suggest use of "xclip" tool, which I have tried and am disappointed as any string when copied with xclip can only be pasted with "xclip -o" command. I need the copied string to be paste-able with Ctrl-V so I can open the path in file manager(nautilus).

Any help/suggestion is appreciated.

  • use xclip -selection CLIPBOARD to set the clipboard that is pasted by ctrl-v. – meuh Aug 13 '15 at 18:44
  • Posted my answer. Please let me know if you manage. – Jacob Vlijm Aug 13 '15 at 19:08
  • Hi Vivek, completely rewrote the answer and combined the two scripts. – Jacob Vlijm Aug 14 '15 at 17:58
  • Have you tried this? github.com/gfxmonk/gedit-plugins – A.B. Aug 15 '15 at 15:37
5

Script to copy the path of a file, opened in gedit

With the gedit window in front, the small script below derives the path from the (gedit) window's name, and copies it to the clipboard.

The script has two options:

  1. Only copy the path to the file's directory, running the script with the option

    -path
    

    or

  2. Copy the path including the file name, running the script with the option

    -file
    

The script

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

name = subprocess.check_output(["xdotool", "getactivewindow", "getwindowname"]).decode("utf-8").strip()
if all(["(" in name, ")" in name]):
    path = name[name.find("(")+1:name.find(")")]
    if sys.argv[1] == "-file":
        fname = name[:name.find("(")]
    elif sys.argv[1] == "-path":
        fname = ""
    command = "echo "+'"'+path+"/"+fname+'"'+" | xclip -selection clipboard"
    subprocess.Popen(["/bin/bash", "-c", command])

How to use

  1. Install both xdotool and xclip:

    sudo apt-get install xdotool xclip
    
  2. Copy the script into an empty file, save it as get_path.py

  3. Test run the script:

    • open an existing gedit file
    • open a terminal window, run the command:

      sleep 5 && python3 /path/to/get_path.py -file
      

      immediately switch to the gedit window, to make the last part of tyhe command run with the gedit window in front.

    • Press Ctrl+V somewhere to paste the just copied path.
  4. If all works fine, you can make the options available in two ways:

    1. Create two shortcut keys for both options: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add both commands to two different shortcuts.
    2. Make both options available in the gedit launcher:

      enter image description here

      Copy the content below into an empty file, save it as gedit.desktop in ~/.local/share/applications

      [Desktop Entry]
      Name=gedit
      GenericName=Text Editor
      Comment=Edit text files
      Exec=gedit %U
      Terminal=false
      Type=Application
      StartupNotify=true
      MimeType=text/plain;
      Icon=accessories-text-editor
      Categories=GNOME;GTK;Utility;TextEditor;
      X-GNOME-DocPath=gedit/gedit.xml
      X-GNOME-FullName=Text Editor
      X-GNOME-Bugzilla-Bugzilla=GNOME
      X-GNOME-Bugzilla-Product=gedit
      X-GNOME-Bugzilla-Component=general
      X-GNOME-Bugzilla-Version=3.10.4
      X-GNOME-Bugzilla-ExtraInfoScript=/usr/share/gedit/gedit-bugreport
      Actions=Window;Document;divider1;Copy current file's directory;Copy path+file name;
      
      Keywords=Text;Editor;Plaintext;Write;
      X-Ubuntu-Gettext-Domain=gedit
      
      [Desktop Action Window]
      Name=Open a New Window
      Exec=gedit --new-window
      OnlyShowIn=Unity;
      
      [Desktop Action Document]
      Name=Open a New Document
      Exec=gedit --new-document
      OnlyShowIn=Unity;
      
      [Desktop Action Copy current file's directory]
      Name=Copy current directory
      Exec=python3 /path/to/get_path.py -path
      OnlyShowIn=Unity;
      
      [Desktop Action divider1]
      Name=.....................................
      OnlyShowIn=Unity;
      
      [Desktop Action Copy path+file name]
      Name=Copy current directory, include file name
      Exec=python3 /path/to/get_path.py -file
      OnlyShowIn=Unity;
      

    In both lines:

    Exec=python3 /path/to/get_path.py -path
    

    and

    Exec=python3 /path/to/get_path.py -file
    

    replace /path/to/get_path.py by the real path to the script.

    Log out and back in to make Unity "switch" to the new, local .desktop file.

Explanation

In the gedit window name, the path is displayed between ( and ). The script simply sees the frontmost window with the help of xdotool, then reads the path between those two characters.

Notes

Since the path is read in a textual way, the script will fail if the file's name includes other () characters.

Examples

With the following window in front:

enter image description here

the first option will copy to the clipboard the path to the file:

~/Bureaublad

while the second option includes the file itself:

~/Bureaublad/some test file.txt

As you can see, spaces are taken care of :).

  • Thank you for your effort and suggestion Jacob, I will try in my gedit(in office system) and let you know :) – Vicky Dev Aug 13 '15 at 19:16
  • Hi Jacob, I tried your code and created shortcut after installing xdotool(xclip is already installed), but it didn't copy the document location to main clipboard :(. Can I run this code by creating an external tool(command) in Gedit ? Please explain a way for doing this. – Vicky Dev Aug 14 '15 at 15:31
  • @VIVEKSHAH could you open a document in gedit (saved on disc) and open a terminal. Then run the command: sleep 5 && python3 /path/to/get_path.py, immediately switching to the gedit window (so thet the gedit window is in front once the script runs)? Then after the five seconds have passed, press Ctrl+V somewhere (not in terminal :) ). Possibly there is an error in the chosen shortcut key. The script (both of them) run without a single error here. – Jacob Vlijm Aug 14 '15 at 15:37
  • Yes about shortcut key I think I may have created a conflicting shortcut key. And I will run your python file in terminal, but can you tell me how can I make this run as Gedit menu item like Edit > "Copy document path" or in "external tool" menu ? – Vicky Dev Aug 14 '15 at 15:46
  • Breaking into the menu structure of gedit is a completely different type of sport, which I cannot play :). You can however make it available as a shorcut in the gedit launcher. Would that be an option? btw did you get it to work? – Jacob Vlijm Aug 14 '15 at 15:58
3

After some documentation search I was able find solution so I am gonna answer my question here.

Open Gedit and go to "Tools" >> "Manage External Tools" and create new tool.

Add following command just like below:

echo -n $GEDIT_CURRENT_DOCUMENT_URI | xclip -sel clip;exit;

Close it and then restart Gedit(just for confirmation).

Open any document and then go to Tools >> External Tools and then click on the command label just created. The path will be in your clipboard.

  • +1, Very neat! Do you still need my update? – Jacob Vlijm Aug 24 '15 at 8:07
  • Yes I would also like to learn python I am also learning it at my home, so a little bit of insight in complex coding would be good, solely if you don't mind. – Vicky Dev Aug 24 '15 at 9:12
0

compare with $GEDIT_CURRENT_DOCUMENT_URI
variable $GEDIT_CURRENT_DOCUMENT_PATH seems better for me:

  • without file:// prefix
  • display multibyte characters (not urlencode)

ref:
https://wiki.gnome.org/Apps/Gedit/Plugins/ExternalTools#Available_Environment_Variables https://wiki.gnome.org/Apps/Gedit/ExternalToolsPluginCommands

0

Have you tried the Copy File Path plugin?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.