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.
- 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.
- In the left hand column is the return type, or what you get back if you use that method.
- In the more detailed listing for each method, you’ll have the method name listed again.
- 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.
- 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: 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.
Selecting the text
Hit the hotkey, and a new text file appears
And there’s our selected text, shown in the less output of the text file.
What other uses do you use the Autokey for? Tip: Check out our regular expressions cheatsheet.