在nautilus 中添加右键菜单

在nautilus 中添加右键菜单

通过 nautilus-python 添加右键菜单

参考: nautilus-python

进入 ~/.local/share/nautilus-python/extensions 目录

如果没有就使用 mkdir -p ~/.local/share/nautilus-python/extensions 新建一个

在extensions 目录新建一个 compare_files_by_meld.py 文件, 并填入以下内容

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
#********************************************************************************
#Copyright © 2023 Wcq
#File Name: test.py
#Author: Wcq
#Email: wcq-062821@163.com
#Created: 2023-02-25 11:55:57 
#Last Update: 2023-02-25 15:45:19
#         By: Wcq
#Description: A nautilus plugin for comparing files with meld
#********************************************************************************
from gi.repository import Nautilus, GObject
from typing import List
import os

class compare_files_by_meld (GObject.GObject, Nautilus.MenuProvider):
    def __init__(self):
        super().__init__()
        self.left_file = None
        self.right_file = None
        print("Initialized compare_with_meld extension")

    def mark_left_file (self, menu: Nautilus.MenuItem, file: Nautilus.FileInfo,) -> None:
        self.left_file = file.get_uri()

    def compare_with_left_file (self, menu: Nautilus.MenuItem, file: Nautilus.FileInfo,) -> None:
        self.right_file = file.get_uri()
        os.system("meld \"%s\" \"%s\" &"%(self.left_file, self.right_file))

    def compare_files(self, menu: Nautilus.MenuItem, files: List[Nautilus.FileInfo]) -> None:
        compare_files = ""
        for x in files:
            compare_files += "\"%s\" "%x.get_uri()
        os.system("meld %s &"%(compare_files))

    def get_file_items(self, files: List[Nautilus.FileInfo],) -> List[Nautilus.MenuItem]:
        if len(files) < 1:
            return []

        items = []
        if len(files) == 1:
            item = Nautilus.MenuItem(name="select left file to compare", label="select left compare file", tip="select %s as left compare file" % files[0])
            item.connect("activate", self.mark_left_file, files[0])
            items.append(item)
            if self.left_file:
                item = Nautilus.MenuItem(name="compare with left file", label="compare with left file", tip="compare with %s "%self.left_file)
                item.connect("activate", self.compare_with_left_file, files[0])
                items.append(item)

        if len(files) > 1: 
            item = Nautilus.MenuItem(name="compare all selected files", label="compare all selected files", tip="compaer %s and %s ..." % (files[0].get_name(), files[1].get_name()))
            item.connect("activate", self.compare_files, files)
            items.append(item)

        return items

    # Even though we're not using background items, Nautilus will generate
    # a warning if the method isn't present
    def get_background_items(self, current_folder: Nautilus.FileInfo,) -> List[Nautilus.MenuItem]:
        return []

😠 注意: class 名字必须和文件名字一样才能被识别

重启 nautilus 即可生效

调试插件方法

1
2
3
mkdir /tmp/testing
export TMPDIR=/tmp/testing
nautilus --no-desktop

这样启动nautilus 的话, 插件里的print 可以直接在终端输出, 方便调试

通过脚本添加

这种方法添加的不怎么好, 会在一个二级目录下, 不好用

新建 ~/.meld/ 目录用于存放选择的一个文件信息

1
mkdir  ~/.meld/

进入 ~/.local/share/nautilus/scripts 目录

如果没有就使用 mkdir -p ~/.local/share/nautilus/scripts 新建一个

在scripts 目录下新建以下三个文件

  • select-left-file

    1
    2
    3
    
    #!/bin/bash
    quoted=$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | sed -e "s/\n/ /g")
    echo "$quoted" > $HOME/.meld/nautilus
    
  • compare-with-left-file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    #!/bin/bash
    arg1=$(cat $HOME/.meld/nautilus)
    arg2=$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | sed -e "s/\n/ /g")
    
    if [ "$(arg1)" -eq "0" ];then
    zenity --info --text "No files Selected!"
    exit 1
    fi
    meld "$arg1" "$arg2"
    
  • compare-all-selected-files

    1
    2
    
    #!/bin/bash
    meld $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
    

重启 nautilus 即可生效