nautilus脚本应用实例之一:用meld比较选中的文件或文件夹

Posted by c4pr1c3 on August 30, 2010

前两天推特上@peigen推荐了一款diff工具meld,和我之前使用的diffuse相比,最大的优点是可以进行文件夹比较。但存在的共同缺点就是如果直接使用nautilus中集成关联打开方式的方法,无法直接通过选中两个待比较的文件或文件夹右键直接进行比较,而总是打开两个应用程序实例。研究了一下nautilus的右键菜单定制方法,但总感觉功能不够强大,且无法完成这个看似非常简单的需求。

偶然间发现了这篇文章:http://ubuntu-tutorials.com/2006/12/29/right-click-to-launch-custom-scripts-with-nautilus-ubuntu-6061-610/

研究了一下,原来可以通过定制gnome-nautilus的右键菜单执行脚本来实现我的需求。下面附上我实现好的两个nautilus脚本

用meld比较选中的文件

#!/usr/bin/perl -w

# This script compares the selected file(s) with meld.

use strict;

my @files = split("\n", $ENV{NAUTILUS_SCRIPT_SELECTED_FILE_PATHS});
my $count = $#files + 1;
if( $count != 2 ) {
	my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Only 2 files can be compared at once. ","200", "300");
	system (@dialog);

}
else {
	foreach my $file (@files)
	{
		if ( ! -f $file && ! -l $file )
		{
			my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Can not compare $file.    \n\n    Only regular files can be diffed.    ","200", "300");
			system (@dialog);
		}
	}

	system("meld $files[0] $files[1]");
}

用meld比较选中的文件夹

#!/usr/bin/perl -w

# This script compares the selected dir(s) with meld.

use strict;

my @files = split("\n", $ENV{NAUTILUS_SCRIPT_SELECTED_FILE_PATHS});
my $count = $#files + 1;
if( $count != 2 ) {
	my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Only 2 directories can be compared at once. ","200", "300");
	system (@dialog);

}
else {
	foreach my $file (@files)
	{
		if ( ! -d $file )
		{
			my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Can not compare $file.    \n\n    Only directories can be diffed.    ","200", "300");
			system (@dialog);
		}
	}

	system("meld $files[0] $files[1]");
}