[wiki]NautilusScriptsHowto

参与到Ubuntu的翻译中来
回复
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

[wiki]NautilusScriptsHowto

#1

帖子 oneleaf » 2005-07-22 14:16

Notes: These scripts may be out of date. Use at your own risk

The nautilus file-manager has limited support for scripting. The following files can be copied to the nautilus-scripts folder. Many of these scripts involve root privileges, which means you can seriously damage your system. Be careful.

Nautilus-scripts folder:

~/.gnome2/nautilus-scripts

Place your scripts there and make them executable (chmod +x {script-name}). You should name the files something that make sense to you, as the filenames will be the script names that show up in your Nautilus menus. You do not need to add a .sh shell script extension. Once you have done that, your scripts will be available in all Nautilus windows under File->Script and in the right-click context menu. You will not see a File->Script menu in file manager until you add your first script to ~/.gnome2/nautilus-scripts .
Email the selected file

This one is really useful. How many times have you needed to find a file and then send it? And to be honest, from Evolution or mozilla-thunderbird this is not that simple. It is much easier to use Nautilus to find your file and then send it from there.

#!/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);
}

Added by Darrell: I found that the mailto: syntax in the above does not work with mozilla-thunderbird. I have created the following amended version which seems to work with 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);
}

You will see there is also a shell script which is required, (~/scripts/thunderbird-email-attachments in my example above, don't put it in the nautilus-scripts directory, otherwise it will show up in the context menu):

#!/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

Mount an ISO image, from Nautilus. Note from carney1979 - this script will be updated to work with filenames containing spaces soon.

#!/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

Unmount an iso image, from nautilus. Note from carney1979 - this script will be updated to work with filenames containing spaces soon.

#!/bin/bash
#
for I in `echo $*`
do
foo=`gksudo -u root -k -m "enter your password for root terminal access" /bin/echo "got r00t?"`
sudo umount $I
done
done
exit0

Set file read-only

Change file-permission to read-only.

#!/bin/bash

chmod ugo-wx $*

Edit file with gedit with root-privileges

Note from carney1979 - this script will be updated to work with filenames containing spaces soon.

#!/bin/bash
#
for I in `echo $*`
do
foo=`gksudo -u root -k -m "enter your password for root terminal access" /bin/echo "got r00t?"`
sudo gedit $I
done
done
exit0

Open Nautilus with root-privileges here

#!/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

Run file with root privileges

Note from carney1979 - this script will be updated to work with filenames containing spaces soon.

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

Open search window here

#!/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

Open terminal here

#!/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

Hide or Show Hidden Files

#!/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

Note: You can do the same without a script by pressing CTRL+H under Nautilus

https://wiki.ubuntu.com/NautilusScriptsHowto
当净其意如虚空,远离妄想及诸取,令心所向皆无碍
yongyi
帖子: 3025
注册时间: 2005-05-07 23:57
联系:

#2

帖子 yongyi » 2005-07-23 10:33

注意:这脚本可能过时。使用者请自担风险。


nautilus文件管理器在脚本支持方面比较有限。以下文件可以复制到nautilus-scripts 文件夹。这里许多脚本都包含 root 特权,这意味着你可以严重损害你的系统。小心!


Nautilus-scripts文件夹:
~/.gnome2/nautilus-scripts

把你的脚本放到那里,并使它们可执行(chmod +x 脚本名)。你应该用你知道意义的名字来命名文件,因为文件名将是显示在你 Nautilus 菜单中的脚本名。你不必加一个shell 脚本扩展名.sh 。一旦你做好这些,你的脚本将会在所有的 Nautilus 窗口都有效,通过 “文件” -> “脚本”菜单 和 右键关联菜单。你不会看到 “文件” -> “脚本”菜单,直到你把你第一个脚本添加到 ~/.gnome2/nautilus-scripts 。


把选中文件Email出去


这个真的非常有用。不知道你经历过多少次,需要寻找一个文件,然后发送出去?老实地说,在 Evolution 或者 mozilla-thunderbird 里,这可并不容易。但使用 Nautilus 你可以非常容易地找到你的文件,然后就在那里发送出去。

代码: 全选

 #!/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);
} 
由Darrell补充:我发现上面的 mailto: 语法在mozilla-thunderbird 不起作用。我已经建立以下的修正版本,它应该可以在 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);
} 

你会看到这也有一个 shell 脚本是必需的,
(在我上面的例子中的 ~/scripts/thunderbird-email-attachments,不要放在 nautilus-scripts 目录,否则它会在关联(context)菜单显示):

代码: 全选

 #!/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 

挂载 ISO


从 Nautilus 挂载一个 ISO 镜像。摘录自 carney1979 ── 这脚本不久将会被升级到可以使用包含空格的文件名。

代码: 全选

 #!/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 

卸载 ISO

从 nautilus 卸载一个 iso 镜像。摘录自 carney1979 ── 这脚本不久将会被升级到可以使用包含空格的文件名。

代码: 全选

 #!/bin/bash
#
for I in `echo $*`
do
foo=`gksudo -u root -k -m "enter your password for root terminal access" /bin/echo "got r00t?"`
sudo umount $I
done
done
exit0 

设置文件只读

修改文件权限为只读。

代码: 全选

 #!/bin/bash

chmod ugo-wx $* 

以 root 权限来用 gedit 编辑文件

摘录自 carney1979 ── 这脚本不久将会被升级到可以使用包含空格的文件名。

代码: 全选

 #!/bin/bash
#
for I in `echo $*`
do
foo=`gksudo -u root -k -m "enter your password for root terminal access" /bin/echo "got r00t?"`
sudo gedit $I
done
done
exit0 

直接以 root 权限打开 Nautilus

代码: 全选

 #!/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

以 root 权限运行文件

摘录自 carney1979 ── 这脚本不久将会被升级到可以使用包含空格的文件名。

代码: 全选

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

直接打开搜索窗口

代码: 全选

 #!/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

直接打开终端

代码: 全选

 #!/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 

隐藏或显示隐藏文件

代码: 全选

 #!/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 
注意:在 Nautilus 下,你可以不使用脚本而按下Ctrl + H 也达到同样效果。

https://wiki.ubuntu.com/NautilusScriptsHowto
独自看一看大海
总想起身边走在路上的朋友
Lenovo E290-420[Celeron-M420/256M/60G/Intel GMA950]
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

#3

帖子 oneleaf » 2005-08-01 14:56

当净其意如虚空,远离妄想及诸取,令心所向皆无碍
头像
millenniumdark
论坛版主
帖子: 4159
注册时间: 2005-07-02 14:41
系统: Ubuntu 14.04 (Kylin)
联系:

#4

帖子 millenniumdark » 2006-07-31 0:09

头像
millenniumdark
论坛版主
帖子: 4159
注册时间: 2005-07-02 14:41
系统: Ubuntu 14.04 (Kylin)
联系:

#5

帖子 millenniumdark » 2006-07-31 0:11

摘录自 carney1979
我改为carney1979案
回复