I miss the right-click zipfile functionality that I used to get in Windows from the winzip add-on. With Nautilus, you can make up your own scripts and drop them in the scripts directory. They appear on the right-click context menu under "Scripts". You won't see this menu if you have no scripts yet.
It's easy to use:
- Copy the following text into ~/.gnome2/nautilus-scripts/tar-bzip2-here
- make it executable: `chmod u+x ~/.gnome2/nautilus-scripts/tar-bzip2-here`
- right click a file / folder that you want to archive, or select multiple files and right click on one
- select "Scripts > tar-bzip2-here"
- If you've selected multiple files, you will be prompted for an archive name. Otherwise, the file name will be selected automatically.
- A message appears saying "The archive should now be ready". You will find it in the same folder that your original files were in.
Here's the code. Copy it exactly (use cut-n-paste):
version 0.2, updated on March 7, 2008
#!/usr/bin/perl -w
#
# tar-bzip-here version 0.2
#
# This program is free for whatever use you want. You break it you get to keep
# the pieces! I would appreciate fixes / improvements being sent back to me.
#
# Changlog:
# 0.2: quote filenames so that certain characters don't confuse bash, such as
# "(". Some more error messages.
# 0.1: initial release
#
use strict;
#my $logfile = '~/tar-bzip2-here.log';
# my @NAUTILUS_SCRIPT_CURRENT_URI = split /\s/, $ENV{'NAUTILUS_SCRIPT_CURRENT_URI'};
my @NAUTILUS_SCRIPT_SELECTED_URIS;
if (defined $ENV{'NAUTILUS_SCRIPT_SELECTED_URIS'}) {
@NAUTILUS_SCRIPT_SELECTED_URIS = split /\s/, $ENV{'NAUTILUS_SCRIPT_SELECTED_URIS'};
}
else {
`zenity --error --text "Run this from nautilus!"`;
die "Run this from nautilus!\n";
}
#`echo > $logfile`;
my @files = ();
foreach my $file (@NAUTILUS_SCRIPT_SELECTED_URIS) {
next unless defined $file;
chomp $file;
if ($file !~ s|^file://||) {
`zenity --error --text "I don't know how to handle the URI '$file'"`;
#`echo 'skipped $file' >>$logfile`;
next;
}
$file =~ s/(%..)/&unq($1)/ge;
# `echo 'ok: $file >> $logfile`;
push @files, "\"$file\"";
}
my $numfiles = $#files + 1;
my $fname;
#`echo 'files are @files'>>$logfile`;
# if mixed files and dir, then ask for archive name
if ($numfiles > 1) {
$fname = `date +%Y%m%d`; chomp $fname; $fname .= '.tar.bz2';
$fname = `zenity --file-selection --save --confirm-overwrite --filename=$fname --title="Select a name for the archive"`;
chomp $fname;
&end ("Cancelled by user") unless ($fname ne '');
}
# if single file or directory, then compress to dirname.tar.bz2
elsif ($numfiles == 1) {
$fname = "$files[0].tar.bz2";
}
else {
&end ("You need to select some files in nautilus to use this script");
}
# now tar a bzip2!
`tar -c @files | bzip2 > "$fname"`;
#`echo 'tar -c @files | bzip2 > "$fname"'>>$logfile`;
# done!
`zenity --info --text "The archive '$fname' should now be ready"`;
# functions
sub unq {
my ($percent) = @_;
if ($percent eq '%20') { return ' '; }
elsif ($percent eq '%20') { return ' '; }
else {
&end ("Don't know how to handle '$percent'");
}
}
sub end {
my ($err) = @_;
`zenity --error --text "$err"`;
die "$err";
}