Start programs simply by name
Quick Starter
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
- Listings for this article: http://www.linux-magazine.com/resources/article_code
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.