Autokey, covered previously on Make Tech Easier, is a great place to store commonly-used text. But one of Autokey’s more advanced features is the ability to script it to do other things. We’ll take a look at two ways to tap Autokey for advanced desktop scripting.
Note: While the below script worked on both 12.10 and 13.04 using the “autokey-qt” flavor, there appears to be some issues with the “autokey-gtk” version in the standard repositories. If you’re using regular (Unity-based) Ubuntu, you should consider installing from the PPA, which contains a newer version of Autokey.
Autokey Scripting Basics
Autokey scripting is done in Python, a popular programming language used in everything from Ubuntu’s Ubiquity installer to Google Maps. While you can do some useful things with only the functions specific to Autokey, you can also use Autokey features in general Python programs to do just about anything you want.
The Autokey Wiki contains some useful topics like Installation and FAQ’s. The API Reference, hidden as a link at the top of the Sample Scripts page, is where the good stuff is. Looking around the page:
1. At the top of the page are all the methods, or functions, of the current module you’re looking at which are explained in more detail below.
2. In the left hand column is the return type, or what you get back if you use that method.
3. In the more detailed listing for each method, you’ll have the method name listed again.
4. Next to the name, in parentheses, are the arguments, or the things you need to provide the method in order for it to do its job.
5. Lastly, there’s an example of usage for the method.
Take some time to look through the API reference – you won’t get far without it. The following is a walk-through of the creation of a script from beginning to end.
Creating an Autokey Script
Autokey scripting is very much like shell scripting, in that you build them one line at a time, using variables and functions (or in this case methods) to get the desired result. In my case, I want to be able to highlight some text and stash it away in a text file for later. The first step is to create a new script in Autokey… you can do this by right-clicking on one of the folders and selecting “New Script,” as shown below.
Give it a name, then the empty text area to the right is all yours – this is where you’ll enter the script. In this case, let’s look at what we want to do step-by-step:
Getting the Currently-Selected Text
Autokey is all about automation, so there should be no reason for us to copy this text, to the clipboard or otherwise. We want to hit the hotkey and go, knowing the text is safely tucked away for later. Fortunately, the API Reference shows there’s a method just for this: the QtClipboard method. The following line of code will grab the currently-selected text for us, as if we’d done an “Edit & Copy” or Ctrl-C ourselves. Instead of the system’s clipboard, we’ll store this in the variable “notecontent”:
Designating the Full Path to the File
Next, we want to store this somewhere. We’ll need to designate a place for it. Let’s add a line that shows the folder we want to use, assigned to the variable “notepath”:
For the file name, I’d like to include a date and time stamp of when it was captured. A little Googling reveals there’s a function in Python’s “time” module that will give me the current date, in YYYY-MM-DD-HH-mm-ss format on command, which we’ll combine with the prefix “note” and the suffix “.txt” and assign to the variable “notename.” We’ll need to importthe time module so we can use it (as it turns out, we need this anyway, as forums recommend adding the second line to avoid conflicts between the script and the keyboard/mouse). This makes the script look like this:
Creating the File
This is relatively easy to follow:
- Import the time module
- Get the current selection, assign to “notecontent”
- Assign the target directory to “notepath”
- Assign the text “note,” then the current date/time, followed by “.txt” to the variable “notename.”
All we need to do now is actually create the file. Fortunately, Autokey provides a method for that too, in the System module:
That’s it! Assign a hotkey, copy some text, and see what happens. With the key functions Autokey provides you, and some Google prowess, you can use Autokey for advanced desktop scripting.
1. Selecting the text
2. Hit the hotkey, and a new text file appears
3. And there’s our selected text, shown in the less output of the text file.
What other uses do you use the Autokey for?