Start programs simply by name

Quick Starter

© Designbüro*Marx, photocase.com

© Designbüro*Marx, photocase.com

Article from Issue 93/2008
Author(s):

The Spotlight utility for the Macintosh has even the most hardened Apple fans scurrying back from the mouse to the keyboard. A short Perl script implements the utility for the Linux desktop.

If you have ever searched a cluttered desktop for an icon belonging to a particular application, you might have asked yourself who invented the approach of using the mouse to select applications. If you know the application's name, there is really no need to waste valuable time picking an icon from dozens on your desktop.

Spotty, a piece of Perl code, gives you a customizable hotkey that immediately pops up a window at the top right side of your desktop (Figure 1). The keyboard focus automatically shifts to the input box. As soon as you start typing – fi, for example – Spotty knows that the program you want to launch must be Firefox, pushes the name to the top of the selection list, and gives you the option of pressing the Tab key to accept the suggestion and launch the application.

All of this takes less than two seconds and works like pressing Alt+F2 in Gnome or KDE; however, coding your own script means that you learn something new and can modify it to your heart's content.

Spotty learns from successful launches and "remembers" the program names it has found by keeping them in a persistent database. The first time you use Spotty to launch an application, you must type firefox, for example, and confirm the entry by pressing the Enter key.

Spotty then searches through the directories defined by your $PATH environment variable and then launches the program. The next time you use the program, it attempts to compare your input with the program names it has learned and shows matches on the right side of the input box. After Spotty has pushed the program you are looking for to the top of the list, just press the Tab key to tell Spotty to fire up the application.

No Way Back after Exec

To launch the application, Spotty uses exec. This overloads the current process (the Perl script) with the external application, thus removing Spotty from the process table and leaving the launched application in its place. Thus, the exec in the launch function in line 133 is the end of the script because the process does not return from it.

The database in which the program names are stored is a persistent hash that uses the CPAN DB_File module employing a Berkeley DB. The script updates the database whenever the hash associated with it by the tie command is changed. To close everything gracefully, line 125 issues an untie shortly before the exec command to untie the hash from the database and store the changes.

Drawing with Tk

As you can see from Listing 1, Spotty uses the CPAN Tk module to draw the application window with the input box. To prevent the window appearing just anywhere on the desktop and to keep it firmly in the top right-hand corner, Spotty then calls the geometry() method with the -0+0 parameters. -0 stands for the x coordinate on the far right, and +0 stands for the topmost y coordinate.

Listing 1

Spotty

 

The main window, $top, is of the MainWindow type and contains two widgets: an Entry type input field on the left and a Label type display to its right. Linked to the $entry input box widget is a text variable, $input, which Tk uses to store the text typed by the user and which is refreshed after each keystroke. Because the -validate option has a value of "key", Tk jumps to the validate() function (lines 75ff.) for each keystroke; the -validatecommand option is used to pass a reference to the function in to the widget. Of course, the function doesn't actually validate anything here because it always returns a 1; it simply serves to execute a callback that searches the database for matches for each character the user types. The Label widget to the right of the Entry widget monitors a text variable, $label_text, and the Tk Manager updates the display whenever its value changes.

If the validate() function notices that matches() (lines 85ff.) finds one or more matches for the word entered by the user, it separates them by line breaks and concatenates them to a single string that it stores in $label_text. This prompts Spotty to display the matches to the right of the input window without any additional programming effort.

The packer (lines 52 and 54) uses the -side => "left" option to pack both widgets into the container object, the main window $top. If "left" moves multiple objects into a container, the packer lines them up from left to right. The "left" option tells the packer to glue any new widgets to the left border of the available space. If a widget is already sitting in this slot, the next one is dropped on the far left of the free space to its right.

Spotty reacts to the Return and Tab keys. Return confirms the string entered thus far, and Tab accepts the first element in the list of suggestions. Perl-Tk uses bind calls in lines 59 and 61 to bind the keys to the launch() (lines 110ff.) and complete() (lines 102ff.) functions, respectively. The latter simply sets the variable for the Entry widget to the top match, $first_match, and then calls launch() so the user does not need to press Enter to launch the application. The bind entry in line 57 tells Spotty to cancel the action and exit the program if somebody presses Ctrl+Q.

The call to focus() in line 64 shifts the keyboard focus to the Entry widget, which is important because the user would otherwise need to drag the mouse and click the entry box to get the widget to accept keyboard input.

After defining all these settings, MainLoop in line 65 launches the GUI, which runs until an application launches or the user bails out of the program by pressing Ctrl+Q. In this case, or in case of an error, the bail function (line 68) helps to clean up by calling the top window's destroy method, thus removing the GUI.

Choosing a keyboard shortcut for your Spotty desktop hotkey that's not in use by any other application program makes sense; remember that pressing it shifts the keyboard focus to the tool. This might not be easy in the case of key hogs, such as Vim. I opted for Ctrl+U because it's easy to press and is not on my list of frequently used Vim commands. (Of course, Vim does use the combination to scroll the edited text up, but I use Ctrl+B instead.)

Unfortunately, the Gnome desktop on my Ubuntu installation thinks it knows better and only lets me bind selected applications to hotkeys, not just any old program. To resolve this issue, I had to run gconf-editor (Figure 2). If the tool is missing, you can issue sudo apt-get install gconf-editor to install the package.

Below Apps, you will see an entry for Metacity (the Gnome Window manager), and below this, the items global_keybindings and keybinding_commands. Set run_command_1 below global_keybindings to the required hotkey combination – for example, Ctrl+U – and then add the path to Spotty below keybinding_commands.

Extensions

Launching a program that needs root privileges causes a minor problem because you would normally have to enter your password. The Ubuntu package manager, synaptic, is one example of this. The program will run, but without root privileges, which means that it can query packages but cannot install new ones.

Spotty solves this with the hash defined in line 7: %sudo_programs. If the program selects one of the programs listed here, line 130 not only issues an exec, but Spotty also launches an Xterm terminal that calls sudo to launch the program in question. The effect of this is that the shell in the xterm that pops up onscreen first prompts you for a password, and if you enter the right one, it runs the program with root privileges.

If you would like to search for commands in paths outside of your $PATH environment variable, you can add entries to the @misc_paths array in line 11. path_search() automatically finds programs in the extended path.

Also, if you prefer to use the cursor keys instead of typing letters, you can extend the Perl code to draw a list box populated with matches to the right of the input box and select an entry from the list.

Regardless of which approach you prefer, if you know what you are looking for, you will find it much faster with a little help from Spotty.

Infos

  1. Listings for this article: http://www.linux-magazine.com/resources/article_code

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: PerlPanel

    One panel has a neat collection of applets and another has spectacular looks – but a combination of the two is rare. Now help draws nigh for the desktop: PerlPanel is extensible with do-it-yourself widgets.

  • Perl: Barcodes

    Barcodes efficiently speed us through supermarket checkout lines, but the technology is also useful for totally different applications. An inexpensive barcode scanner can help you organize your private library, CD, or DVD collection.

  • Perl: Internet Video Organizer

    Video files are ready for download, but your storage space is limited, and, if you're like most people, you may have a hard time letting go of past favorites. You need a higher authority to decide when it’s time to trim down your collection.

  • Perl: Web Regression Test

    Testing complex web applications does not necessarily mean investing in expensive, proprietary tools such as Test Director or Silk Performer. Selenium is for free; it can remotely control any major browser, and it is programmable in Perl.

  • navi

    When the history function fails and the manpage is too long, navi comes to the rescue with an interactive cheat sheet for the shell.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News