script help - getting some info from a bash script

40 views
Skip to first unread message

Joe

unread,
Nov 3, 2011, 2:38:15 AM11/3/11
to autoke...@googlegroups.com
I have a simple AK printing script that I use with Firefox and Thunderbird.

I print almost everything to file into a print queue directory
($HOME/pq). I name the files 01, 02, ..., 99 (so they stay in the order
printed) and then run them through my duplex printing emulator (duplexpr
on sourceforge).

I also have a bash script that looks at $HOME/pq and figures out what
the next file name (number) will be and stores it in a bash variable
(NEXT). Currently, it displays it in a pop-up (zenity - and its text
can't be selected) so I can see it in the gui without going to a
console. I then just retype it into the print dialog.

What I'd like to do is add something at the end of the following script
that, expressed as pseudocode would be:
keyboard.send_keys("<value from NEXT><enter>")

This would be the "sliced bread" of printing for me. I could just type
ctrl-p (my hotkey), and everything else would be automatic. I wouldn't
have to keep looking at my print queue to see what the next available
number is, etc..

I'm pretty good at bash, but haven't had time to learn much python yet.

If I knew enough python, I could probably just rewrite the bash script
in python on the end of the current AK script, but it's more than a few
lines of code (around 40 lines, comment-stripped) and I'm pretty sure
it's well beyond my python coding skills at the moment.

Any ideas on how to glue the two scripts together, etc. would be
appreciated.

TIA

Joe - autokey 0.80.3 - kubuntu lucid Linux x86

# print2file

import time

keyboard.send_keys("<alt>+f")
time.sleep(1.0)
keyboard.send_keys("p")
time.sleep(1.0)
keyboard.send_keys("<tab><home>")
time.sleep(1.0)
keyboard.send_keys("<tab>pq/")

Chris D

unread,
Nov 3, 2011, 5:33:48 PM11/3/11
to autokey-users
The way to do this in Python would be:

import os
next = os.environ["NEXT"]

With this sort of generic query, you should be able to find the answer
by searching for general Python howtos - there are thousands upon
thousands of these around the web.

Joe

unread,
Nov 3, 2011, 7:36:03 PM11/3/11
to autoke...@googlegroups.com
Thanks. I'll look into it now that I know where to look.

Joe

unread,
Nov 4, 2011, 6:46:24 AM11/4/11
to autoke...@googlegroups.com
On 11/03/2011 05:33 PM, Chris D wrote:
Thanks. That was all I needed to hack it together.

I wasn't clear on environment variables because I read something about
python getting a copy of them that remained static after python started
(wouldn't see updated values) and because I wasn't sure whether setting
a variable in a script (and exporting it) would work if the script was a
child process of python. I think exports only work going into
sub-processes, not coming back out. That's an area I'm fuzzy on.

Anyway, I found great stuff at
http://www.doughellmann.com/PyMOTW/subprocess/index.html#module-subprocess
which almost worked and I found the fix for that at
http://stackoverflow.com/questions/4814970/subprocess-check-output-doesnt-seem-to-exist-python-2-6-5

and the working script is now:

# print2file

import time
import subprocess

## 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(1.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(1.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)
keyboard.send_keys("<enter>")

Now, I just have to read some more so I can add a bit of code to handle
the case if my bash script returns an error.
Also, I may take the last <enter> out so it's not quite so automagical.

Thanks again.

Joe

Reply all
Reply to author
Forward
0 new messages