我正在尝试在Gedit中编写一个自定义命令,该命令将当前打开并处于活动状态的文档的路径(到父目录和到文件)都复制到剪贴板,因为我找不到任何可以执行此操作的gedit插件或工具。
我不知道从哪里开始,也没有任何好的参考资料,但是我知道我必须在bash脚本中进行脚本编写。
我搜索了用于从终端将任何字符串复制到剪贴板的外部命令(因为它也运行bash脚本),但是答案建议使用“ xclip”工具,我尝试过,并且由于使用xclip复制时只能粘贴任何字符串而感到失望使用“ xclip -o”命令。我需要复制的字符串可以使用Ctrl-V粘贴,以便可以在文件管理器(nautilus)中打开路径。
任何帮助/建议表示赞赏。
gedit
前面有gedit窗口,下面的小脚本从(gedit
)窗口的名称获取路径,并将其复制到剪贴板。
该脚本有两个选项:
仅将路径复制到文件目录,并使用选项运行脚本
-path
或者
复制包含文件名的路径,并使用选项运行脚本
-file
#!/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])
同时安装xdotool
和xclip
:
sudo apt-get install xdotool xclip
将脚本复制到一个空文件中,另存为 get_path.py
测试运行脚本:
gedit
文件打开一个终端窗口,运行命令:
sleep 5 && python3 /path/to/get_path.py -file
立即切换到gedit
窗口,以使tyhe命令的最后一部分在gedit
窗口在前的情况下运行。
如果一切正常,您可以通过两种方式使这些选项可用:
使两个选项在gedit
启动器中均可用:
下面的内容复制到一个空文件,将其保存为gedit.desktop
在~/.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;
在两行中:
Exec=python3 /path/to/get_path.py -path
和
Exec=python3 /path/to/get_path.py -file
替换/path/to/get_path.py
为脚本的真实路径。
注销并重新登录,以使Unity“切换”到新的本地.desktop
文件。
在gedit窗口名称中,路径显示在(
和之间)
。该脚本仅借助即可看到最前面的窗口xdotool
,然后读取这两个字符之间的路径。
由于路径是通过文本方式读取的,因此如果文件名包含其他()
字符,则脚本将失败。
前面有以下窗口:
第一个选项会将文件的路径复制到剪贴板:
~/Bureaublad
而第二个选项包括文件本身:
~/Bureaublad/some test file.txt
如您所见,空格是:)处理的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句