It had long bugged me that I always used Bash for Nautilus scripts, so I
have created a boiler-plate Nautilus script in Python (attached), which
displays the following in a whizzy little GTK TextView window:
1. Command-line arguments passed in by Nautilus
2. A couple of interesting environment variables, notably
NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
3. PWD environment variable
4. Walks the list of selected files, doing an ls -l on each one
Drop it into ~/.gnome2/nautilus-scripts and make it executable, then
give it a spin:
1. Select a bunch of files and directories in Nautilus
2. File -> Scripts -> Show Nautilus Parameters
Douglas.
=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================
#!/usr/bin/python
import os
import subprocess
import sys
import pygtk
import gtk
pygtk.require('2.0')
class TextviewWindow:
string = ""
window = None
def add(self, text):
self.string += text
def addLine(self, text):
self.string += text + "\n"
def displayWindow(self):
self.textbuffer.set_text(self.string)
self.window.show()
gtk.main()
def close_application(self, widget):
gtk.main_quit()
def __init__(self, string="", title="Text Viewer", width=640, height=480):
self.string = string
# The window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_default_size(width, height)
self.window.connect("destroy", self.close_application)
self.window.set_title(title)
self.window.set_border_width(10)
# A VBox that contains:
# ScrolledWindow
# HBox for the buttons at the bottom
vbox = gtk.VBox(False, 0)
self.window.add(vbox)
vbox.show()
# The ScrolledWindow that contains the TextView
scrolledWindow = gtk.ScrolledWindow()
scrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
scrolledWindow.show()
vbox.pack_start(scrolledWindow)
# TextView
textview = gtk.TextView()
self.textbuffer = textview.get_buffer()
scrolledWindow.add(textview)
textview.set_editable(False)
textview.set_cursor_visible(False)
textview.set_left_margin(10)
textview.set_right_margin(10)
textview.show()
# HBox for the row of buttons at the bottom
# Close
hbox = gtk.HBox(False, 10)
hbox.set_border_width(10)
vbox.pack_start(hbox, False, True, 0)
hbox.show()
# Close button
button = gtk.Button("Close")
button.connect("clicked", self.close_application)
hbox.pack_end(button, False, False, 0)
button.set_flags(gtk.CAN_DEFAULT)
button.grab_default()
button.show()
if __name__ == "__main__":
textview = TextviewWindow(title="Parameters passed in by Nautilus")
# Display the command-line args and a few of the environment variables set
by Nautilus
textview.addLine("Command-line args = " + str(sys.argv[1:]))
textview.addLine("NAUTILUS_SCRIPT_SELECTED_FILE_PATHS = " +
str(os.environ.get("NAUTILUS_SCRIPT_SELECTED_FILE_PATHS")))
textview.addLine("NAUTILUS_SCRIPT_CURRENT_URI = " +
str(os.environ.get("NAUTILUS_SCRIPT_CURRENT_URI")))
textview.addLine("PWD = " + str(os.environ.get("PWD")))
textview.addLine("")
# Assemble a list of the files
nautilus_script_selected_file_paths =
str(os.environ.get("NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"))
files = nautilus_script_selected_file_paths.split("\n")
# Trim off the last element, which is always empty -
NAUTILUS_SCRIPT_SELECTED_FILE_PATHS ends with a \n
files = files[:-1]
# A worked example, taking a long listing of the selected files and
directories
for file in files:
# Assemble the command line (quoting the file variable, as it may contain
spaces)
command = "ls -l '" + file + "'"
captured = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE).stdout.read()
# ls provides a trailing \n
textview.add(captured)
textview.displayWindow()
=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================