Perl script manages keyboard shortcuts
Automating Distribution
Of course, you wouldn't want to have to enter the short forms and hotkey actions manually in the autokey-gtk
GUI on every single Linux box you use. Also, it would also be useful to automate the process of adding new production helpers to the Autokey configuration instead of detouring via the desktop.
You need to investigate what Autokey actually does behind the scenes to implement this: After the user has pressed the buttons for the Autokey configuration, filled out the text fields, and closed the dialogs (not forgetting to press the Save button in the bar at the top), Autokey stores the data in the user's home directory below ~/config/autokey
.
The metadata for the defined keyboard shortcuts now exist in JSON format below the respective folder: My Phrases
for phrases and Sample Scripts
for actions. A fresh Autokey installation creates a number of sample phrases the first time you call autokey-gtk
; they all give you a good idea of the tool's full potential (Figure 6).
To automate the process, the Perl script [3] in the following section copies the metadata format used by Autokey from the existing examples and adds new settings defined in a fairly compact Yaml file, which you can edit without much ado to suit your own needs (Listing 1).
Listing 1
autokey.yaml
Replace Verbose JSON
The JSON files that Autokey uses to store the metadata are relatively verbose, as you can see from Figure 7. Files named .<name>.json
contain the information linked to the <name>
entry, and the matching Python script for triggering the desired action resides in <name>.py
in the same folder.
When called like this,
autokeygen autokey.yaml
the script in Listing 2 grabs and uses the two entries in the Yaml file – one for the text substitution of the hth
short form and a terminal action for the Ctrl+Alt+N shortcut – to form a total of four additional entries in the Autokey configuration tree, as you can see in Figure 8.
Listing 2
autokeygen
Each entry creates a JSON file and a text or Python file. Note that Autokey must not be running at this point in time; otherwise, it would not notice the configuration change and would simply overwrite the new data with its own entries later on.
To parse the Yaml and JSON files, Listing 2 first loads the JSON
and YAML
modules from CPAN. Lines 7 to 12 define where Autokey stores the configuration files; the for
loop in lines 14-19 iterates across the entries in the Yaml file from Listing 1; their data is available in the form of an array of hash references.
Depending on the entry type, the symbolic function call in line 18 either jumps to process_script()
or process_phrase()
. For Perl to allow audacious constructions like this in strict mode, line 17 has to reduce temporarily the level of strictness in the interpretation of symbolic references to a less stringent level.
Program by Example
To avoid the script implementing the whole mess of JSON, it bases its new creations off the sample files already put in place by Autokey; for each entry of the Yaml file, it only changes the fields in the result files that are actually different. The json_rw()
function both reads from and writes to the JSON files in the Autokey configuration tree. For example, line 28 reads the test configuration for Insert Date
, a sample script for dynamically inserting the current date into text documents. It then modifies only the hotkey
, description
, and modes
entries in lines 30-32 to adapt it to the requirements for the Yaml entry to open a terminal window for the keyboard shortcut Ctrl+Alt+N.
Using a reference to the data hash as an additional argument, the call to json_rw()
in line 34 switches to write mode and creates the .terminal.json
file in the Autokey configuration. The Python script that uses the system.exec_command()
function to call a command in the shell is then sent to the Python file terminal.py
by blurt()
in line 37.
The hotkey definition in the Yaml file of Listing 1 is defined tightly as ctrl-alt-n
, but because Autokey prefers a more complex data structure, the hotkey()
function in line 44 prepares the raw data.
The process_phrase()
function in lines 58-80 creates the hth
text replacement I went through earlier in the format required by Autokey. The slurp()
and blurt()
functions from the CPAN Sysadm::Install module do the donkey work here. During the test phase, if a script won't run as expected, take a look at the ~/.config/autokey/autokey.log
file. This is where system errors are logged, and the messages are typically quite helpful.
Infos
- "Start programs simply by name" by Michael Schilli, Linux Magazine, Issue 93, 2008: http://www.linux-magazine.com/index.php/Issues/2008/93/Perl-Spotty/(language)/eng-US
- Autokey project: https://code.google.com/p/autokey
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/magazine/173
« Previous 1 2
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
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.