How to Use Python to create scripts for GNOME desktop, screenlets architecture, and Nautilus to deliver highly productive environments? Desktop scripts enable drag-and-drop and quick access to common information and services. This article describes how to use Python to add features for desktop Nautilus extension.
For GNOME Desktop Users, Nautilus programming may be one of the more common applications. It can solve all file copy, movement, rename, and search problems through a simple graphic interface. On the surface, there seems to be no file-related transactions that Nautilus cannot process-unless you consider executing a task with shell scripts.
The Nautilus development tool provides multiple methods to add new features without having to open the main code library. The simplest method is to use a bash or bash script that can execute commands that are usually executed at a terminal prompt. This method allows you to use this command to ensure that they complete the task they want to complete first. You can also use other languages, including the C scripting language, GnomeBasic, Perl, and Python. This article describes how to use the Python language to add new features to Nautilus. It is assumed that the reader has an understanding of the Python language and the Python standard library.
Nautilus script
The first method to expand Nautilus is to find a specific directory named. gnome2/nautilus-scripts in/home. When you right-click a file or folder under the Scripts menu, All executable files in the directory will appear. You can also select multiple files or folders and use the same right-click method to pass the file list to the script.
When a script is called, Nautilus supports multiple environment variables that contain the current directory and selected files. Table 1 shows these environment variables.
Table 1. Nautilus Environment Variables
Environment Variable |
Description |
NAUTILUS_SCRIPT_SELECTED_FILE_PATHS |
Split the path of the new line of the selected file (for local only) |
NAUTILUS_SCRIPT_SELECTED_URIS |
The new line of the selected file is split into URIs. |
NAUTILUS_SCRIPT_CURRENT_URI |
Current location |
NAUTILUS_SCRIPT_WINDOW_GEOMETRY |
Position and size of the current window |
In Python, you can call the OS. environ. get function to obtain the values of these variables, as follows:
selected = os.environ.get('NAUTILUS_SCRIPT_SELECTED_FILE_PATHS,'')
This call returns a string containing all selected files separated by linefeeds. Python uses the following code to simplify the operation of returning this string to the list that can be iterated:
targets = selected.splitlines()
At this point, you may need to stop and discuss user interaction. When the control is transferred from Nautilus to the script, there are indeed no restrictions on the script at this point. Depending on the role of the script, there is no user feedback, except for some types of complete or error messages, which can be processed through some simple message boxes. Since the gtk indexing wing toolkit was used when writing Nautilus, although this is not necessary, it is logical to adopt the same approach. You can easily use TkInter or wxPython.
For the purpose of this article, you will use gtk. To generate a simple message box for communication completion status, only a few lines of code are required. For the convenience of reading, this code is most suitable if you want to create a simple function to generate a message. A total of four lines of code are required:
def alert(msg):dialog = gtk.MessageDialog()dialog.set_markup(msg)dialog.run()