Unofficial scripts collection

275 views
Skip to first unread message

Joe

unread,
Jan 12, 2012, 4:52:27 PM1/12/12
to autoke...@googlegroups.com
I may be getting my projects confused, but did someone post here about
collecting AK scripts for an examples library?
I can't find the post if there was one. I have one to contribute.

I know I would find more examples very helpful, especially because I'm
new to python and don't know any of the modules that are available to
extend it.

Joe

Al

unread,
Jan 13, 2012, 10:40:05 PM1/13/12
to autokey-users
Joe,

It was I who made that post. Goto my website at www.bowierocks.coms,
register and post me your script, or just blast it on here and i'll
copy it onto the site, giving you full credit of course. Autokey is
awesome and I want also to share what I've found and created with
others. The more of us using autokey, the better I know it'll become
and who knows perhaps Chris will find some awesome coders to help him
with the project. :D

- Al

p.s. Well it is official I have officially traveled 26,280,908,010
miles around the Sun as of today! If that were dollars, it'd be how
much the U.S. Federal debt increases every 2 months on average.
Because of that, I think taxes are not rendered moot and pointless.

Joe

unread,
Jan 14, 2012, 3:27:54 AM1/14/12
to autoke...@googlegroups.com
OK. Here's mine. It is documented in its comments. It works with a
bash script which also follows.

Shameless plug: While it's intended to work with my duplex printing
emulator package

http://sourceforge.net/projects/duplexpr/

It will help build a print queue for any use you have in mind.

Joe

# print2file

import time
import subprocess

## Copyleft 2012/01/12 - 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/
## Depends on the bash script pqnext_py being in you PATH
## and executable.
## User must manually create print queue folder (~/pq)

## 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
## Using pqnext_py and puts that in the Print to file name field
## 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

## 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>pq/")
## Call my bash script to get the next print file name (from stdout)
##output = subprocess.check_output(['pqnext_py']) ##won't work until
python 2.7
output = subprocess.Popen(['pqnext_py'],
stdout=subprocess.PIPE).communicate()[0]
## add the file name to the path and then press enter to complete the
dialog
keyboard.send_keys(output)


#!/bin/bash
## Copyleft JPmicrosystems 11/04/2011

## Get next print job number to stdout for scripts

## This version only works (correctly) if all print job
## file names are just two-digit numbers like 01 ... 99
## It can't handle file names with more than one leading zero
## But it only looks at the lexically last file name
## so only that one has to conform

## All print job file names must be the same number of digits
## Or it will return the wrong answer

cd $HOME/pq
## Get the (lexically) last print file name
NEXT="$(ls | sort -n | tail -1 )"

## Handle empty print queue
if [ -z "${NEXT}" ]
then
NEXT="00"
fi

## This lame code only works reliably with two digit file names
if [ ${#NEXT} -lt 2 ]
then
exit 1
fi

## Strip leading zero so increment will work
## (Keeping bash in base 10)
if [ "${NEXT:0:1}" == "0" ]
then
NEXT="${NEXT:1}"
fi

## This lame code can't handle file names with more than one leading zero
if [ ${#NEXT} -gt 1 ] && [ "${NEXT:0:1}" = "0" ]
then
exit 1
fi

(( NEXT++ ))

## Put the leading zero back on if needed
if [ ${NEXT} -lt 10 ]
then
NEXT='0'${NEXT}
fi

## No trailing newline so AutoKey can control that
echo -n "${NEXT}"
exit 0

Joe

unread,
Jan 14, 2012, 3:29:22 AM1/14/12
to autoke...@googlegroups.com
On 01/13/2012 10:40 PM, Al wrote:
I didn't see anything about AutoKey on your site. I didn't search it
though.

Joe

Al

unread,
Jan 14, 2012, 11:36:04 AM1/14/12
to autokey-users
Joe,

In the menu, click on collections, and then linux and then autokey
scripts.

- Al

Al

unread,
Jan 14, 2012, 12:14:57 PM1/14/12
to autokey-users
Joe,

What would you say, if I eliminated your bash script and it all wored
within your autokey script?

- Al

Try:
# we will store a number inside the scripts persistent memory to
# represent the number that was being previously written by the
# bash script, initiliazing to 0 if necessary
if not store.has_key("print_queue"):
store.set_value("print_queue",0)

# now pull and use the print_queue variable
pq = store.get_value("print_queue")

# you can use format codes to pad in 0's if required.
# using the stored internal number you can easily get more than 99 if
not more 9999
# if so desired.
output = "subprocess.Popen([%d],stdout = subprocess.PIPE).communicate()
[0]" % pq

# increment the print queue and store it
pq = pq + 1
store.set_value("print_queue",pq)

Joe

unread,
Jan 14, 2012, 6:19:07 PM1/14/12
to autoke...@googlegroups.com
On 01/14/2012 12:14 PM, Al wrote:
> Joe,
>
> What would you say, if I eliminated your bash script and it all wored
> within your autokey script?
I'd say you know python and I don't (yet) ... and Thank you.

I'm not entirely sure what you did, but it looks like you're just
keeping a counter saved with the script.

If that's the case, it won't work because the script only works for
Firefox and Thunderbird and
lots of other programs can put things in the print queue (or remove
them). That's why the bash script actually reads
the directory and finds out what the last file name/number was. Of
course, that can be done in python, if you know how.

Also, I have no idea when/if the counter gets reset. When I clear the
print queue (after printing), it needs to be reset - and that can be on
another day after one or more reboots (My computer is a notebook and
gets turned off frequently. Also, there is not always a printer
attached.) My bash script is not affected by this.

Joe

Al

unread,
Jan 14, 2012, 9:20:25 PM1/14/12
to autokey-users
Joe,

Is your script intended to work across all printing functions or just
when you do ctrl+p? If it is just one you initiate the hotkey combo
then the internal counter method should be work great. And as far as
resetting goes, I'm sure we can figure out something to that end. I
really don't know alot about python, I just chose to use a function
that the great and most awesome Chris put into Autokey.

- Al
> > ##http://sourceforge.net/projects/duplexpr/

Joe

unread,
Jan 14, 2012, 9:58:30 PM1/14/12
to autoke...@googlegroups.com
It works on <ctrl>+p , but is filtered to only be active on Mozilla
windows. Other applications have different print dialogs and i don't
want to (have to) code for each of them. These two apps cover the bulk
of what I print. The simplest approach is to just read the directory
and see what's there and not worry about how it got there. My bash
approach is very simple and works well unless I mistakenly put files in
my print queue that don't follow the pattern. Then it does weird things
because it's not currently defended.

Joe

Al

unread,
Jan 16, 2012, 7:51:43 AM1/16/12
to autokey-users
Joe,

My modification to your Autokey script eliminated the bash script
entirely. While it didn't remove how you suggested the hotkey combo or
window filter should be defined. Except for the part where you can
reset the count, it is functionally the same. In addition, literally,
as did your script, it increments the "offline" print que by 1 each
time it is run. This version of the script can create rather large
numbers.

Hmmm, if your primary concern is resetting the print que when the
offline files have been removed for whatever reason, that should be
rather easy to implement.

I'll leave it to you to figure out. :)

- Al

p.s. http://learnpythonthehardway.org/book/

Joe

unread,
Jan 30, 2012, 8:42:04 PM1/30/12
to autoke...@googlegroups.com
On 01/16/2012 07:51 AM, Al wrote:
> Joe,
>
> My modification to your Autokey script eliminated the bash script
> entirely. While it didn't remove how you suggested the hotkey combo or
> window filter should be defined. Except for the part where you can
> reset the count, it is functionally the same. In addition, literally,
> as did your script, it increments the "offline" print que by 1 each
> time it is run.
That's the part that doesn't work for me. Lots of files get into (and
out of) the print queue without going through this script so a simple
counter stored with the script just won't work.

> This version of the script can create rather large
> numbers.
>
> Hmmm, if your primary concern is resetting the print que when the
> offline files have been removed for whatever reason, that should be
> rather easy to implement.
I think that would be easy, but unnecessary when the script implements
an algorithm that works.

>
> I'll leave it to you to figure out. :)
>
> - Al
>
> p.s. http://learnpythonthehardway.org/book/
I'm checking that out along with his regex book. Thanks.

The only problem I have is that I don't like reading hypertext. I
prefer to print things out and go at it with a highlighter. It's pretty
tricky to flatten hypertext for printing and I've never quite figured it
out. I don't know if the pdf version is any better, but if I like it I
may buy it from him.

Eventually, I'll learn how to read a directory in python (it can't be
very hard) and then I can code the rest of my script in python and
eliminate the bash.

Joe

unread,
Jan 31, 2012, 6:15:51 AM1/31/12
to autoke...@googlegroups.com
Here's my latest version that works in python only - no bash script needed.

I'm sure someone who knows python will get a good laugh at the way it's
implemented (because I coded it using Google searches for syntax since I
still don't know python), but it works.

# print2file

import time
import os

## Copyleft 2012/01/31 - 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)

## 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

## 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>pq/")

## Set path to print queue
path = os.getenv("HOME") + '/pq'
## Get all the files in the print queue in a list
dirList=os.listdir(path)
## 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
if len(dirList) == 0:
output = '0'
else:
output = dirList[0]

## Increment the file number
output = str(int(output) + 1)

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

## 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)

Al

unread,
Feb 1, 2012, 8:40:17 AM2/1/12
to autokey-users
Nice work Joe. Now I get what I was missing about what you were
saying. You have other print dialogs that this macro doesn't work on,
that also print to the pq folder, so you have to check for the latest
queued file and make sure you are one beyond it. Curious now, but has
the responsiveness of the macro improved now that it doesn't have to
shell out to complete its execution?

On Jan 31, 5:15 am, Joe <jose...@main.nc.us> wrote:
> On 01/30/2012 08:42 PM, Joe wrote:
>
>
>
>
>
>
>
> > On 01/16/2012 07:51 AM, Al wrote:
> >> Joe,
>
> >> My modification to your Autokey script eliminated the bash script
> >> entirely. While it didn't remove how you suggested the hotkey combo or
> >> window filter should be defined. Except for the part where you can
> >> reset the count, it is functionally the same. In addition, literally,
> >> as did your script, it increments the "offline" print que by 1 each
> >> time it is run.
> > That's the part that doesn't work for me.  Lots of files get into (and
> > out of) the print queue without going through this script so a simple
> > counter stored with the script just won't work.
> >> This version of the script can create rather large
> >> numbers.
>
> >> Hmmm, if your primary concern is resetting the print que when the
> >> offline files have been removed for whatever reason, that should be
> >> rather easy to implement.
> > I think that would be easy, but unnecessary when the script implements
> > an algorithm that works.
>
> >> I'll leave it to you to figure out. :)
>
> >> - Al
>
> >> p.s.http://learnpythonthehardway.org/book/
> ##http://sourceforge.net/projects/duplexpr/

Joe

unread,
Feb 1, 2012, 7:06:35 PM2/1/12
to autoke...@googlegroups.com
Haven't had time to play with the delays. But, I think most delays,
when they occur, are not script-related. The biggest one is when I get
an info box saying loading page for printing. That can last from less
than a second to forever. I have no idea how to detect it and wait for
it to go away.

Joe

Joe

unread,
Feb 1, 2012, 7:11:20 PM2/1/12
to autoke...@googlegroups.com
On 02/01/2012 08:40 AM, Al wrote:
> Nice work Joe. Now I get what I was missing about what you were
> saying.
I guess I had to translate it into Parseltongue for you to understand it
;) .

Al

unread,
Feb 2, 2012, 3:23:26 PM2/2/12
to autokey-users

Now where did that wand of mine go? ...

Btw, under the API information in "lib.scripting.Window" you'll find a
lot of information for waiting on windows to become active, focused
etc ... "http://autokey.googlecode.com/svn/trunk/doc/scripting/
lib.scripting.Window-class.html"

Ronie

unread,
Jul 4, 2012, 7:52:08 PM7/4/12
to autoke...@googlegroups.com
didn't find where to put this so I am putting it here:

my first autokey script!
use it to open Kiss FM radio

# Enter script code
import subprocess
import time
subprocess.Popen(["/usr/bin/firefox"])
if window.wait_for_exist(".*Firefox.*"):
    time.sleep(2)
    keyboard.send_keys("<ctrl>+t")
    time.sleep(2)
    keyboard.send_keys("http://kissfm.com.br/portal/ouca-a-kiss<enter>")
    time.sleep(2)
    winTitle = window.get_active_title()
    window.move_to_desktop(winTitle,3)
Reply all
Reply to author
Forward
0 new messages