Thursday 21 February 2008

Nautilus scripts

Here's a nautilus script that I find very useful! If you find yourself zipping or tarring a lot of files, this will save you lots of time!

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:
  1. Copy the following text into ~/.gnome2/nautilus-scripts/tar-bzip2-here
  2. make it executable: `chmod u+x ~/.gnome2/nautilus-scripts/tar-bzip2-here`
  3. right click a file / folder that you want to archive, or select multiple files and right click on one
  4. select "Scripts > tar-bzip2-here"
  5. If you've selected multiple files, you will be prompted for an archive name. Otherwise, the file name will be selected automatically.
  6. A message appears saying "The archive should now be ready". You will find it in the same folder that your original files were in.
Easy and time saving! No more terminals just to tar -cf blah; bzip blah blah!

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";
}

 
Copyright 2009 Another Blog. Powered by Blogger Blogger Templates create by Deluxe Templates. WP by Masterplan