Wiki Ubuntu-it

Indice
Partecipa
FAQ
Wiki Blog
------------------
Ubuntu-it.org
Forum
Chiedi
Chat
Cerca
Planet
  • Pagina non alterabile
  • Informazioni
  • Allegati

Versione 1 del 28/05/2005 21.27.13

Nascondi questo messaggio

ItalianNautilusScriptsHowto

    Draft

Nautilus scripting

http://www.ubuntulinux.org/wiki/IconNautilus.png

Il file-manager di nautilus supporta lo scripting anche se in modo limitato. I seguenti file possono essere copiati nella cartella nautilus-scripts. Molti di questi scripts coinvolgono i privilegi di root, che possono danneggiare seriamente il vostro sistema . Fate attenzione.

Nautilus-scripts folder:

     ~/.gnome2/nautilus-scripts

Disponete i vostri scripts nella cartella , e rendeteli eseguibili (chmod +x {script-name}). Una volta fatto questo , i vostri scripts saranno disponibili in tutte le finestre di Nautilus sotto la voce File->Script, e saranno richiamabili attraverso il right-click.

Manda a destinatario posta

Questo è veramente utile. Quante volte avete avuto la necessità di cercare un file per poi inviarlo? Sono onesto, da Evolution o mozilla-thunderbird questo non è molto semplice. E' veramente semplice usare Nautilus per cercare un file e inviarlo per email dalla posizione in cui ci si trova.

    #!/usr/bin/perl -w
    # By John Russell

    # This script sends the selected file(s) with your email client.

    use strict;

    my $MAILTO_URL="mailto:?";
    my @files = split("\n", $ENV{NAUTILUS_SCRIPT_SELECTED_FILE_PATHS});
    my $count = 0;
    foreach my $file (@files)
    {
        if ( ! -f $file && ! -l $file )
        {
            my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Can not send $file.    \n\n    Only regular files can be mailed.    ","200", "300");
            system (@dialog);
        }
        else
        {
           $MAILTO_URL = $MAILTO_URL . "attach=" . $file . "&";
               shift;
            $count += 1;
        }
    }


    if ($count > 0)
    {
        my @command = ("gnome-open", $MAILTO_URL);
        system(@command);
    }

Aggiunto da Darrell: Ho notato che la sintassi mailto: non funziona con mozilla-thunderbird. Ho creato la seguente versione che sembra funzionare con Thunderbird:

    #!/usr/bin/perl -w
    # By John Russell
    # and hacked to work with thunderbird by Darrell

    # This script sends the selected file(s) with mozilla-thunderbird.

    use strict;

    my $attach_string="\"attachment='";
    my @files = split("\n", $ENV{NAUTILUS_SCRIPT_SELECTED_FILE_PATHS});
    my $count = 0;
    foreach my $file (@files)
    {
        if ( ! -f $file && ! -l $file )
        {
            my @dialog = ("gdialog","--title","Error","--msgbox", "\nError: Can not send $file. \n\n    Only regular files can be mailed.    ","200", "300");
            system (@dialog);
        }
        else
        {
           $attach_string = $attach_string . "file://" . $file . ",";
               shift;
            $count += 1;
        }
    }


    if ($count > 0)
    {
        $attach_string = $attach_string . "'\"";
    # replace spaces with '%20' as demanded by mozilla/thunderbird    
        $attach_string =~ s/\s/%20/g;
    # invoke shell script to call thunderbird differently depending on whether it's running already or not
        my $command = ("~/scripts/thunderbird-email-attachments " . $attach_string);
        system($command);
    }

Vedrete inolche che è richiesto uno shell scrips, (~/scripts/thunderbird-email-attachments nel mio esempio qua sopra, non è posizionato nella cartella nautilus-scripts, altrimenti sarebbe visibile nel menù contestuale):

    #!/bin/sh

    # if thunderbird is already running open a compose window in the running instance

            if `ps xc | grep -q mozilla-thunder`; then 
                    exec mozilla-thunderbird -a Mozilla-Thunderbird -remote         "xfeDoCommand(ComposeMessage,$1)"

    # else start a new instance with the compose window only

            else
                    exec mozilla-thunderbird -compose $1 
            fi

Mount ISO

Montare un immagine ISO, da Nautilus. Nota da carney1979 - questo script sarà presto modificato per lavorare anche con file che contengono spazi nel nome.

    #!/bin/bash
    #
    #By modprobing loop it makes sure that the module is loaded and ready for 
    #use.  If it is already loaded then nothing will happen.
    modprobe loop
    for I in `echo $*`
    do
       foo=`gksudo -u root -k -m "enter your password for root terminal access" /bin/echo "got r00t?"`
    sudo mount -o loop -t iso9660 $I /media/iso
       done
    done
    exit0

Unmount ISO

Smontare immagine ISO , da nautilus. questo script sarà presto modificato per lavorare anche con file che contengono spazi nel nome.

    #!/bin/bash
    #
    for I in `echo $*`
    do
       foo=`gksudo -u root -k -m "inserire la tua password di root" /bin/echo "got r00t?"`
    sudo umount $I
      done
    done
    exit0

Impostare il file in sola lettura

Cambiare i permessi del file in sola lettura.

    #!/bin/bash

    chmod ugo-wx $*

Editare il file con gedit usando i privilegi di root

Questo script sarà presto modificato per lavorare anche con file che contengono spazi nel nome.

    #!/bin/bash
    #
    for I in `echo $*`
    do
       foo=`gksudo -u root -k -m "inserei la tua password di root" /bin/echo "got r00t?"`
    sudo gedit $I
       done
    done
    exit0

Aprire Nautilus con i privilegi di root

    #!/bin/bash
    # root-nautilus-here
    # opens a root-enabled instance of a nautilus window in selected location
    # requires sudo privileges and gksudo, which may involve security risks.
    #Install in your ~/Nautilus/scripts directory.
    #
    # Placed in the public domain by Shane T. Mueller 2001
    # Fixes provided by Doug Nordwall
    #
    # 2004.04.18 -- keith@penguingurus.com - Added gksudo usage to provide popup
    #               password window if sudo has expired.  Line only echos got
    #               root to std output.  But gksudo updates your sudo access
    #               privs, so running nautilus with sudo will succeed
    #               without asking for a password.
    
    
    foo=`gksudo -u root -k -m "enter your password for nautilus root access" /bin/echo "got r00t?"`
    sudo nautilus --no-desktop $NAUTILUS_SCRIPT_CURRENT_URI

Avviare file con i privilegi di root

Questo script sarà presto modificato per lavorare anche con file che contengono spazi nel nome.

    #!/bin/bash
    #
    for I in `echo $*`
    do
       /usr/bin/gksudo $I
       done
    done
    exit0

Aprire la finestra di ricerca

    #!/bin/sh
    # From Johnathan Bailes
    # This script opens a gnome-search-tool in the directory you select.
    #
    # Distributed under the terms of GNU GPL version 2 or later
    #
    # Install in your ~/Nautilus/scripts directory.
    # You need to be running Nautilus 1.0.3+ to use scripts.
    
    cd $NAUTILUS_SCRIPT_CURRENT_URI
    exec gnome-search-tool

Aprire il terminale

    #!/bin/bash
    #
    # This script opens a gnome-terminal in the directory you select.
    #
    # Distributed under the terms of GNU GPL version 2 or later
    #
    # Install in ~/.gnome2/nautilus-scripts or ~/Nautilus/scripts
    # You need to be running Nautilus 1.0.3+ to use scripts.
    
    # When a directory is selected, go there. Otherwise go to current
    # directory. If more than one directory is selected, show error.
    if [ -n "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]; then
        set $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
        if [ $# -eq 1 ]; then
            destination="$1"
            # Go to file's directory if it's a file
            if [ ! -d "$destination" ]; then
                destination="`dirname "$destination"`"
            fi
        else
            zenity --error --title="Error - Open terminal here" \
               --text="You can only select one directory."
            exit 1
        fi
    else
        destination="`echo "$NAUTILUS_SCRIPT_CURRENT_URI" | sed 's/^file:\/\///'`"
    fi
    
    # It's only possible to go to local directories
    if [ -n "`echo "$destination" | grep '^[a-zA-Z0-9]\+:'`" ]; then
        zenity --error --title="Error - Open terminal here" \
           --text="Only local directories can be used."
        exit 1
    fi
    
    cd "$destination"
    exec x-terminal-emulator

Nascondi o visualizza file nascosti

    #!/bin/sh
    # When hidden files (.emacs, etc) are hidden, shows "Show Hidden Files" option.
    # When hidden files are shown, shows "Hide Hidden Files" option.
    # Uses gconf to toggle between the two Nautilus options.
    # Should be placed in ~/.gnome2/nautilus-scripts/ with executable permission.
    OLDSTATE=$(gconftool-2 --get "/desktop/gnome/file_views/show_hidden_files")
    if [ "$OLDSTATE" == "false" ] ; then
      NEWSTATE="True"
      mv ~/.gnome2/nautilus-scripts/Show\ Dot\ Files ~/.gnome2/nautilus-scripts/Hide\ Dot\ Files
    else
      NEWSTATE="False"
      mv ~/.gnome2/nautilus-scripts/Hide\ Dot\ Files ~/.gnome2/nautilus-scripts/Show\ Dot\ Files
    fi
    gconftool-2 --set "/desktop/gnome/file_views/show_hidden_files" --type boolean $NEWSTATE

Nota: Potete fare lo stesso premendo CTRL+H su Nautilus