Skip to content

Contributed Scripts

chenleo92 edited this page Aug 1, 2017 · 4 revisions

Introduction

This page contains user contributed scripts to demonstrate the advanced capabilities of AutoKey's scripting service.

Feel free to use the scripts presented for your own purpose. However, if you significantly modify them or come up with something new as a result of using them please post them into our forums so one of the wiki moderators can install them here for all to benefit.

All submitted scripts are publicly licensed as GNU GPL v3

For specific details on the custom functions available to AutoKey scripts, see the API reference.

Choose Browser On Link Selection

Discovered at: Linux Journal

Author: Mike Diehl

Description: This Autokey script takes a highlighted url from any program and triggers a selection of browsers from which you can choose to load the page.

choices = ["konqueror", "firefox", "chrome"]
retCode, choice = dialog.list_menu(choices)
if retCode == 0:
  system.exec_command(choice + " " + clipboard.get_selection())

Health Potion Drinker

Author: Itscool - Al

Description: I created this Autokey script to help me manage the drinking of health potions. The game lacks a mechanism to quickly imbibe a potion without some difficulty. It is far easier to hit one key that does it for you. In the case of this script, you are also taken back to safe zone if you are out of health potions. All you have to do is make sure you have pot in all of your slots. It will work even if you don’t have pot in all of the slots. For use with the flash client at Realm of the Mad God.

if not store.has_key("heal"):
# set the key 'persistant variable' if we don’t have it already
store.set_value("heal",1)

pot = store.get_value("heal")
if pot > 8:
  # also send us back to the nexus
  pot = 1
  keyboard.send_key("<f5>")

#do the pot thing
output = str(pot)
keyboard.send_key(output)
#increment the pot

pot = pot + 1

store.set_value("heal",pot)

Ask For Assistance

Author: Itscool - Al

Description: I created this Autokey script to do two things. 1) Generate a randomized shout message to gain assistance and 2) drink a manna pot from one of the bottom four inventory slots. For use with the flash client at Realm of the Mad God.

from random import randrange
announcement = ["OMG","","DARN","","OH BOY","","COME HERE","","TP ME","","MESHUGANA","","OY",""]
declaration = ["I could seriously use some help here","The devs must be crazy","How many minions did Oryx say he had to feed?","Dodge bullets? Sure. But this","MONSTERS"]
expletives = ["!","!!","!!!","!!!!1","!!!1!","!!1!1!"]

if not store.has_key("mana"):
    # set the key 'persistant variable' if we don't have it already
    # or if the value is greater than 8 reset it to 5
    store.set_value("mana",5)

mana = store.get_value("mana")
if mana > 8:
    mana = 5

part1 = announcement[randrange(len(announcement))]
part2 = declaration[randrange(len(declaration))]

if part1 != "":
    part1 = part1 + expletives[randrange(len(expletives))]
if part2 != "":
    part2 = part2 + expletives[randrange(len(expletives))]

output = "/yell " + part1 + " " + part2 + "\n"
keyboard.send_keys(output)

#do the mana thing
output = str(mana)
keyboard.send_key(output)
#increment the mana
mana = mana + 1
store.set_value("mana",mana)

Ask For A Cleric

Author: Itscool - Al

Description: I created this Autokey script will help you manage drinking health potions in the top four inventory slots. Additionally it'll generate a random yell asking for some type of healing support. For use with the flash client at Realm of the Mad God.

from random import randrange
announcement = ["HELP","","HP","","HEAL","","MEDIC","","DR. WHO","","1-800-PRIEST",""]
declaration = ["I could use some professional help here","Anyone in the businesses of hp","Bring lots of that RED STUFF in a jar","I don't wanna die","Mommy","You can leave a msg at GHOSTBUSTERS","I'm going to meet the maker","Great, I'm another Father Dowling Mystery","Agatha Christi?! Posh","Should've listened to Momma","I don't want this chocolate","..."]
expletives = ["!","!!","!!!","!!!!1","!!!1!","!!1!1!"]

if not store.has_key("pot"):
    # set the key 'persistant variable' if we don't have it already
    # or if the value is greater than 4 reset it to 1
    store.set_value("pot",1)

pot = store.get_value("pot")
if pot > 4:
    pot = 1

part1 = announcement[randrange(len(announcement))]
part2 = declaration[randrange(len(declaration))]

if part1 != "":
    part1 = part1 + expletives[randrange(len(expletives))]

if part2 != "":
    part2 = part2 + expletives[randrange(len(expletives))]

output = "/yell " + part1 + " " + part2 + "\n"
keyboard.send_keys(output)

#do the pot thing
output = str(pot)
keyboard.send_key(output)
#increment the pot
pot = pot + 1
store.set_value("pot",pot)

Gnome Screenshot

Author: Dave

Description: The Autokey script created here will allow you to quickly screenshot your desktop while moving your mouse off to the right. Dave had originally submitted it to the group forums there and at least 4 of us helped him to modify it and make it faster.

#
# replace ~/Downloads with the location of where you told gnome-screenshot to save the pic
#
system.exec_command("bash -c 'rm ~/Downloads/today.png'",False)
system.exec_command("gnome-screenshot -w", False)
window.wait_for_exist("Save Screenshot", timeOut=5)
system.exec_command("xte 'mousemove 725 399'", False)
window.activate("Save Screenshot", switchDesktop=False)
keyboard.send_keys("today")

Switch Or Start Firefox

Author: Jack

Description: this script switches to the Firefox window. if Firefox is not already running, the script starts it. i use similar scripts for the applications i use most.

#
#start firefox or switch to firefox
import subprocess
command = 'wmctrl -l'
output = system.exec_command(command, getOutput=True)

if 'Firefox' in output:
    window.activate(‘Firefox’,switchDesktop=True)

else:
    subprocess.Popen(["/usr/bin/firefox"])
#

RSS Zoomer

Author: Jack

Description: several actions to go on the same key… i have this on this script sends '^+' when in Firefox or Chromium to do a zoom in. when i'm reading RSS feeds in Akregator, it sends '=' to move to the next unread item.

#

winTitle = window.get_active_title()

if 'Mozilla Firefox' in winTitle:
    output = "+"
    keyboard.send_keys(output)

elif 'Chromium' in winTitle:
    output = "+"
    keyboard.send_keys(output)

elif 'Akregator' in winTitle:
    output = "="
    keyboard.send_keys(output)

else:
    output = ""
    keyboard.send_keys(output)

#

Move Active Window

Author: Jack

Description: here are 4 tiny scripts to move the active window left, right, up, down. fwiw i have them on !Super+AppropriateArrow.

Move Window Left:

# move active window left
thisWin = window.get_active_title()
windetails = window.get_active_geometry() # returns tuple of: x, y, width, height
newX = windetails[0] - 40
window.resize_move(thisWin, xOrigin=newX, yOrigin=-1, width=-1, height=-1, matchClass=False)

Move Window Right:

# move active window right
thisWin = window.get_active_title()
windetails = window.get_active_geometry() # returns tuple of: x, y, width, height
newX = windetails[0] + 40
window.resize_move(thisWin, xOrigin=newX, yOrigin=-1, width=-1, height=-1, matchClass=False)

Move Window Up:

# move active window up
thisWin = window.get_active_title()
windetails = window.get_active_geometry() # returns tuple of: x, y, width, height
newY=windetails[1] - 80 # must bigger than approx 40, otherwise window moves down!
window.resize_move(thisWin, xOrigin=-1, yOrigin=newY, width=-1, height=-1, matchClass=False)

Move Window Down:

# move active window down
thisWin = window.get_active_title()
windetails = window.get_active_geometry() # returns tuple of: x, y, width, height
oldY = windetails[1]
newY=windetails[1] + 20
window.resize_move(thisWin, xOrigin=-1, yOrigin=newY, width=-1, height=-1, matchClass=False)

Offline Print Queue

Author: Joe

Description: Changes <ctrl>+p for Mozilla applications to print to a file in a special offline print queue using incremented file names, intended for use with duplexpr.

# print2file

import time
import os

## Copyleft 2012/02/27 - JPmicrosystems GPL
## Change <ctrl>+p for Firefox and Thunderbird
## to print to file in a special print queue using
## numbered file names, 01, 02, ... so the print jobs stay in order
## Intended for use with duplexpr
## http://sourceforge.net/projects/duplexpr/
## User must manually create print queue folder (~/pq)
## and set all *.print_to_filename variables in prefs.js (about:config)
## to path to print queue/01.ps
## e.g. /home/shelelia/pq/01.ps

## Hotkey <ctrl>+p
## Window Filter .*Mozilla.*

## Changes <ctrl>+p to
## Print to file and looks at the print queue (~/pq)
## Finds the last print file number and increments it by one
## Doesn't send final <Enter> so additional options like Print Selection
## can be set by the user
##Fails if Loading file to print takes longer than the second delay

def filenum ():
    ## Gets next file number
    ## Set path to print queue
    path = os.getenv("HOME") + '/pq'
    ## Get all the files in the print queue in a list
    ## Handle filesystem error
    try:
        dirlist=os.listdir(path)
    except:
        return "EE"

    ## And sort it in reverse order
    ## So the largest numbered file is first
    dirlist.sort(reverse=True)

    ## If there aren't any files then
    ## Set last file to 0
    ## else, set it to the last file with a valid number
    ## Defend script against non-numeric file names
    ## go down the reverse sorted list until a numeric one is found
    fn=""
    if len(dirlist) == 0:
      fn = "00"
    else:
        files = len(dirlist)
        i = 0
        while (i < files) and (fn == ""):
            name = dirlist[i]
            if name.isdigit():
                fn = str(int(name) + 1)
            i += 1

    if fn == "":
      fn = "01"

    ## If it's less than 2 characters long,
    ## Left pad it with a zero
    ## To maintain the sorting order
    if len(fn) < 2:
      fn = '0' + fn

    return fn


## Open the File menu
## (can't use <ctrl>+p because that's the hotkey)
keyboard.send_keys("<alt>+f")
time.sleep(1.0)
## Select Print
keyboard.send_keys("p")
time.sleep(2.0)
## tab to the printer selection list, then to the top of the list
## which is the Print to File selection
keyboard.send_keys("<tab><home>")
time.sleep(2.0)
## tab to the file name field and enter the print queue directory path
keyboard.send_keys("<tab>")

output = filenum()

## complete the file name field in the print dialog
## But don't send an enter so the user can select
## options before printing
keyboard.send_keys(output)

Key Wrapper

Author: tpower21

Description: Wrap selected text with one or two keys/sets of keys, or output them if nothing's selected, and keep your current clipboard. Can call it using engine.run_script(description) then bind whatever's calling it to something.

Real World Use: Highlight a load of text and that you need wrapped in something. Possibly say quotes, brackets, braces, parens, pipes, asterisks etc ... That is why the script is in two parts so you can have multiple assignments to the same engine.

Alternate Use: Wrapping a load of text with words or phrases like you would use in html, xml, etc ... where wrap could be <div> and wrap_close could be </div>.

binder script:

wrap = "<shift>+9" 
wrap_close = "<shift>+0" 
engine.run_script("wrap_selection")

script: wrap_selection

# wrap_selection 
# get keys 
try: 
    wrap 
except NameError: 
    dialog.info_dialog("Need at least one key to wrap selection with.") 
try: 
    wrap_close 
except NameError: 
    wrap_close = wrap 
# below needed to get working in some apps, otherwise 
# clipboard.get_selection() quicker/better 
# get clipboard/selection 
try: 
    clip_board = clipboard.get_clipboard() 
except: 
    clip_board = "" 
keyboard.send_keys("<ctrl>+x") 
time.sleep(0.01) 
try: 
    selection = clipboard.get_clipboard() 
except: 
    selection = "" 
# clipboard won't update if selection empty 
if clip_board == selection: 
    selection = "" # problem if clipboard and selection are the same 
# and not empty, but not the end of the world 
# wrap and clean up 
keyboard.send_keys(wrap+selection+wrap_close+"<left>")
#tpower21 added the following two lines on February 7 2012
for s in selection:
   keyboard.send_keys("<shift>+<left>") 
clipboard.fill_clipboard(clip_board) 
del clip_board, selection, wrap, wrap_close 

WordPress Hebrew Concordance Linker

Author: Itscool - Al

Description: This AutoKey script takes a selected entry in your WordPress article that may or may not contain parens "()", extracts the number enclosed and converts that to a link to the concordance at eliyah.com. The number is assumed to be a HEBREW lexicon indicator as pertains to the Strong's Concordance that is keyed to the New King James Bible. I have my hot-key combo set to "<alt>+<shift>+h". With no further ado I present the following Python script for AutoKey that does what I claim.

selection = clipboard.get_selection()
if selection[0] == "(":
    selection = selection[1:-1]
 
if selection[-1] == ")":
    selection = selection[0:-2]
    
keyboard.send_keys("<alt>+<shift>+a")
 
keyboard.send_keys("http://www.eliyah.com/cgi-bin/strongs.cgi?file=hebrewlexicon&isindex=%s" % selection)
keyboard.send_keys("<tab>")
keyboard.send_keys("Strong's Concordance HEBREW %s" % selection)
keyboard.send_keys("<enter>")

Function to Type Text Slowly

Author: Joseph Pollock - JPmicrosystems

Description:* Normally, AutoKey will emit characters from a phrase or macro as fast as the system will allow. This is quite a bit faster than typing and some applications may not be able to keep up.

The following macro emits/types a string of characters one at a time with a delay between each one.

The first parameter is the string of characters to type. The second optional parameter is the number of seconds to delay between each keypress. It defaults to 0.1 seconds.

type_slow():

# Filename: typing_aids.py

import time
import scripting

## Type a string character by character
##   from within AutoKey
##   with a small delay between characters
def type_slow(string, delay=0.1):
  if delay < 0:
    delay = 0

  for c in string:
    scripting.keyboard.send_key(c)
    time.sleep(delay)

Use Chrome to Google

Author: Tongjie Chen

Description:* This will use Google to search a question with just a hotkey, which I usually set as <super> + g.

# Filename: to_google.py
retCode, to_search = dialog.input_dialog(title='To Google', message='Enter your question', default='')
## This will use a dialog to collect a string to Google, if cancel no action will happen
if retCode == 0:
    system.exec_command("chrome http://www.google.com/search?q="+ "'" + to_search + "'")