如何为nautilus脚本分配键盘快捷键?

我已经设置了Nautilus脚本 。 我把脚本放在/home/sumeet/.local/share/nautilus/scripts ,它确实出现在右键菜单中。 并且也按预期工作。 我只想为脚本分配一个快捷方式。


如何为我的nautilus脚本创建键盘快捷键?

上面问题中给出的答案针对的是特定版本,并且已经完全过时,除了有关此主题的问题之外,我找不到任何其他内容。

如何做到这一点

右键单击nautilus脚本的文件或文件夹时,所选文件将作为参数传递给脚本。 在大多数情况下,例如:

 import os subject = os.getenv("NAUTILUS_SCRIPT_CURRENT_URI") 

…使用python3,最简单的forms。

如果您将其替换为:

 import pyperclip subprocess.call(["xdotool", "key", "Control_L+c"]) subject = pyperclip.paste() 

…当前选定的文件在脚本中用作参数

你需要什么

要使用此解决方案(16.04及更高版本),您需要同时安装xdotoolpython3-pyperclip

 sudo apt-get install python3-pyperclip xdotool 

完整的脚本,在评论中提到

然后成为:

 #!/usr/bin/env python3 import subprocess import os import sys import pyperclip # --- set the list of valid extensions below (lowercase) # --- use quotes, *don't* include the dot! ext = ["jpg", "jpeg", "png", "gif", "icns", "ico"] # --- set the list of preferred filenames # --- use quotes specs = ["folder.png", "cover.png", "monkey.png"] # --- # retrieve the path of the targeted folder subprocess.call(["xdotool", "key", "Control_L+c"]) dr = pyperclip.paste() for root, dirs, files in os.walk(dr): for directory in dirs: folder = os.path.join(root, directory) fls = os.listdir(folder) try: first = [p for p in fls if p in specs] first = first[0] if first else min( p for p in fls if p.split(".")[-1].lower() in ext ) except ValueError: pass else: subprocess.Popen([ "gvfs-set-attribute", "-t", "string", os.path.abspath(folder), "metadata::custom-icon", "file://"+os.path.abspath(os.path.join(folder, first)) ]) 

将其添加到快捷键将为所选目录中的所有目录设置图标。

将其添加到快捷键(!)

添加快捷键,运行(脚本使用 – ) xdotool命令按下另一个组合键可能会很棘手。 要防止两种键组合相互干扰,请使用:

 /bin/bash -c "sleep 1 && python3 /path/to/script.py" 

说明

在选择文件时按Ctrl + C时,文件的路径将复制到剪贴板。 我们用以下方式模拟按键:

 subprocess.call(["xdotool", "key", "Control_L+c"]) 

pythonpyperclip模块只是生成路径,在使用pyperclip.paste()时从file://pyperclip.paste() (这不会直接粘贴,但在脚本中使路径可用)。

如果目标是选择文件并执行操作,则可以使用xdotoolxclip shell脚本来xclip 。 所以先安装它们:

 sudo apt-get install xdotool xclip 

然后使用循环内的操作创建以下脚本:

 #!/bin/bash file=$(mktemp) xdotool key "Control_L+c" variable=$( xclip -out -selection clipboard) variable=$( echo -e "$variable" | \ awk 'BEGIN { FS = "\n" } { printf "\"%s\" ", $1 }' | \ sed -es#\"\"## | \ sed 's/" "/"\n"/g') echo "$variable" > $file if [ -s "$file" ]; then while read absolute_path_file; do absolute_path_file=$(eval echo "$absolute_path_file") base_name=$(basename "$absolute_path_file") ### Execute the actions with the selected files here ### echo "$absolute_path_file" ### echo "$base_name" done < $file fi 

此脚本不依赖于NAUTILUS变量,您可以使用它创建快捷方式:

 /bin/bash -c "sleep 1 && /path/script.bash"