如何為 nautilus 腳本指派鍵盤快速鍵?

如何為 nautilus 腳本指派鍵盤快速鍵?

我已經設定了一個鸚鵡螺腳本。我已將腳本放入其中/home/sumeet/.local/share/nautilus/scripts,它確實出現在右鍵選單中。並且也按預期工作。我只想為腳本分配一個快捷方式。


如何為我的 nautilus 腳本建立鍵盤快速鍵?

上述問題中給出的答案針對特定版本並且完全過時,除了與此主題相關的問題之外,我找不到任何其他內容。

答案1

如何做到

當您右鍵單擊 nautilus 腳本的檔案或資料夾時,所選檔案將作為參數傳遞給腳本。大多數情況下是這樣的:

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

....以最簡單的形式使用 python3。

如果將其替換為:

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模組只是生成路徑,file://在使用時將其剝離pyperclip.paste()(這不會直接粘貼,而是使路徑在腳本內可用)。

答案2

如果目標是選擇檔案並執行操作,則可以僅使用帶有 和 的 shell 腳本來xdotool完成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 -e s#\"\"## | \
            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"

答案3

  • 使用程式此處概述,您可以將 Nautilus 中的捷徑加入到 nautilus 腳本中。從 Nautilus 40 開始,這再次起作用。
  • 在 nautilus 腳本的名稱中使用_,您可以為腳本指派熱鍵。例如,名為「_Combine PDF」的腳本將顯示,並帶有下劃線的 C。選擇文件後,您可以使用Shift+快速存取腳本F10 c

您可以在子資料夾中組織腳本。這些子資料夾也顯示為選單選項,導致子選單。也可以使用此命名約定為它們指派熱鍵。

相關內容