nautilus脚本应用实例之二:右键菜单挂载/卸载ISO

Posted by c4pr1c3 on August 30, 2010

右键挂载选中的iso

#!/usr/bin/perl -w

# This script mount the selected iso(s).

use strict;

my @files = split("\n", $ENV{NAUTILUS_SCRIPT_SELECTED_FILE_PATHS});

my $foo = system("gksudo -u root -k -m 'enter your password for root terminal access' /bin/echo 'r00t'");

if( ! $foo )
{
	foreach my $file (@files)
	{
		if ( ! -f $file && ! -l $file )
		{
			my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Can not mount $file.    \n\n    Only regular files can be mounted.    ","200", "300");
			system (@dialog);
		}
		else
		{
			$_ = $file;
			my ( $dir, $filename ) = m/(.*)[\\\/](.+)/ ? ( $1, $2 ) : ( undef, $_ );
			if( ! -d "/media/$filename" )
			{
				system("sudo mkdir /media/$filename");
			}
			system("sudo mount -o loop -t iso9660 $file /media/$filename");
		}
	}

}

右键卸载选中的iso

#!/usr/bin/perl -w

# This script unmount the selected iso(s).

use strict;

my @files = split("\n", $ENV{NAUTILUS_SCRIPT_SELECTED_FILE_PATHS});

my $foo = system("gksudo -u root -k -m 'enter your password for root terminal access' /bin/echo 'r00t'");

if( ! $foo )
{
	foreach my $file (@files)
	{
		if ( ! -f $file && ! -l $file )
		{
			my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Can not unmount $file.    \n\n    Only regular files can be unmounted.    ","200", "300");
			system (@dialog);
		}
		else
		{
			$_ = $file;
			my ( $dir, $filename ) = m/(.*)[\\\/](.+)/ ? ( $1, $2 ) : ( undef, $_ ); 

			system("sudo umount /media/$filename");
			if( -d "/media/$filename" )
			{
				system("sudo rmdir /media/$filename");
			}
		}

	}

}