Jumat, 03 April 2015

AutoIT / Autokey ubuntu

Using Autokey Scripts to Automate Your Linux Desktop

Using Autokey Scripts to Automate Your Linux Desktop

NoteWhile 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 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.
autokey-scripting-apiref
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.
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.
autokey-scripting-newscript
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”:
notecontent = clipboard.get_selection()

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”:
notecontent = clipboard.get_selection()
notepath = "/home/aaron/clips/"
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:
import time
time.sleep(0.25)
notecontent = clipboard.get_selection()
notepath = "/home/aaron/clips/"
notename = "note" + timestrftime("%Y%m%d%H%M%S") + ".txt"

Creating the File

This is relatively easy to follow:
  1. Import the time module
  2. Get the current selection, assign to “notecontent”
  3. Assign the target directory to “notepath”
  4. 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 theSystem module:
import time
time.sleep(0.25)
notecontent = clipboard.get_selection()
notepath = "/home/aaron/clips/"
notename = "note" + timestrftime("%Y%m%d%H%M%S") + ".txt"
system.create_file(notepath + notename, content = notecontent)
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
autokey-scripting-selecttext
2. Hit the hotkey, and a new text file appears
autokey-scripting-directory
3. And there’s our selected text, shown in the less output of the text file.
autokey-scripting-notecontent
What other uses do you use the Autokey for

Tidak ada komentar:

Posting Komentar