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
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
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
Joe
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.
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)
Joe