1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Automatize booklet printing

Discussion in 'All Other Software' started by API, Oct 7, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. API

    API Thread Starter

    Joined:
    Oct 7, 2008
    Messages:
    6
    There are currently several possibilities to print a document in the form of a booklet (many software or printer drivers offer this possibility, some printers even fold the output automatically). However, the result is rather poor if too many pages (the limit being around 20) are printed together (folding them together becomes increasingly difficult). Therefore, I use to print large documents in sets of 20 pages. This implies the use of several printing commands for each group of 20 pages until you cover the whole document, which can be pretty exhauting and time-consuming for big documents :(

    Is there a software (if possible freeware) or some manner to automatize this procedure and therefore printing a whole document with booklet style in groups of 20 pages each with a single printing command?

    Thank you very much in advance for the help.
     
  2. stantley

    stantley

    Joined:
    May 22, 2005
    Messages:
    7,091
    Hi, welcome to TSG.

    You could use AutoHotkey to write a script that would be a series of Print commands.
     
  3. API

    API Thread Starter

    Joined:
    Oct 7, 2008
    Messages:
    6
    Thanks Stantley :p,

    I had heard of this keyword and mouse recording programs before but never used any of this kind and not remotely thought of them as an alternative.

    Looks like a pretty complete programming language. I will try to get it to fullfil the purpose of booklet automatization. After having a quick look, I still have some questions open regarding the interaction with the application used for printing, such as how to obtain variables such as the total number of pages of the document to be printed. I will have a closer look when I have the time and then put a post here indicating whether I managed or not, for the case that someone else could also be interested.
     
  4. stantley

    stantley

    Joined:
    May 22, 2005
    Messages:
    7,091
    Yes, that's true. It's powerful enough to do some very complicated tasks, but the syntax is simple and an average user can pick it up quickly.

    A good way to start is go through the quick start tutorial http://www.autohotkey.com/docs/Tutorial.htm and write a few simple scripts to see how it all works.
     
  5. API

    API Thread Starter

    Joined:
    Oct 7, 2008
    Messages:
    6
    Finally I have managed to get it automized and it works great!

    Thank you, Stantley. The program is much more powerful that I would have ever thought! Most of the work can be done by simply recording the clicks of the mouse (and it takes into account the window you are working in, rather than merely the absolute position of the mouse!). It is only needed to add by hand some loops and variables to perform the iterations. I only find the working with variables in this program pretty confusing, since they are always considered as strings and when you want to use them as numbers, you must be careful. I was for instance forced to use the command abs() in order to compare two variables with each other as numbers, since otherwise they were treated as strings and I got completely unexpected behaviour. I know, there is probably a more appropriate manner to do it, but I did not find a better way.

    I do not post the script here, since it is very application specific and would certainly not work for another person willing to do the same. If someone wants to do a similar thing, besides the aforementioned caution with the variables, he will probably encounter the following problem: it was at least for me difficult to deal with the fact that each printing command takes a very variable amount of time, and until the printing job gets done, the print command in the menu is disabled. If the script tries to open the printing window again, the window does not appear and the programs hangs up. The ideal would be to wait until the printing job is done before continuing, but I did not managed to find a manner to identify the end of the printing job. The only two possibilities left are then either to wait some predetermined amount of time (long enough for for the printing job to get done in the worst case) or (the one I selected) to try to open the printing window over and over again (every 3-5 second) until successful. I know, not really elegant, but effective :)
     
  6. stantley

    stantley

    Joined:
    May 22, 2005
    Messages:
    7,091
    You're welcome. I'm glad you got it to work. If you want to post your code, I'll look at it and see if I can help you streamline the process.

    Yes, working with variables can be a bit confusing, especially when you first start using them. When comparing the values of two variables make sure all expressions are enclosed in parenthesis. So

    If x > y
    do something

    should be written as

    If (x > y)
    do something

    "If the script tries to open the printing window again, the window does not appear and the programs hangs up."

    You might be able to handle that with a WinWaitActive command, which will pause the script until the window you need becomes the active window.

    WinActivate, WinTitle
    WinWaitActive, WinTitle
     
  7. API

    API Thread Starter

    Joined:
    Oct 7, 2008
    Messages:
    6
    At the end comes the script.

    I have already changed the way I dealt with variables. The trick with the parentheses does away with the need of using the abs() commant. Thanks again, Stantley.

    Regarding the activation of the printing window: as you see, I already employ in all cases the waiting command for the window. The problem is that if a printing process is still being carried out, the menu File->Print is disabled and even if a click is done, no printing window pops up. I tried to detect either that the printing command was ended (there is no new printing process which I could identify, but rather the same parent application, i.e. MS. Office Document Imaging, which uses some CPU for the printing) or that the menu File->Print changed from disabled to enabled, but I did not manage to find a way to do that. This is why I have the nested loop which repeats the mouse click in File->Print over and over again every five second as long as the Print Window is not open. I know this is not elegant, but I could not do better.


    Here is the script:

    NumberOfPagesPrintedPerBooklet:=20
    ; opening the printing menu
    WinWait, description.mdi - Microsoft Office Document Imaging,
    IfWinNotActive, description.mdi - Microsoft Office Document Imaging, , WinActivate, description.mdi - Microsoft Office Document Imaging,
    WinWaitActive, description.mdi - Microsoft Office Document Imaging,
    MouseClick, left, 7, 32
    Sleep, 100
    MouseClick, left, 83, 275
    Sleep, 100
    WinWait, Print,
    IfWinNotActive, Print, , WinActivate, Print,
    WinWaitActive, Print,

    ; select printer and configure it for booklet print
    MouseClick, left, 399, 67
    Sleep, 100
    MouseClick, left, 199, 172
    Sleep, 100
    MouseClick, left, 485, 72
    Sleep, 100
    WinWait, B&W LAN Printer Document Properties,
    IfWinNotActive, B&W LAN Printer Document Properties, , WinActivate, B&W LAN Printer Document Properties,
    WinWaitActive, B&W LAN Printer Document Properties,
    MouseClick, left, 448, 40
    Sleep, 100
    MouseClick, left, 182, 136
    Sleep, 100
    MouseClick, left, 597, 567
    Sleep, 100

    ; copy in clipboard the total number of pages and setting the page range for the first printing command from page 1 (not changed) to page "NumberOfPagesPrintedPerBooklet"
    WinWait, Print,
    IfWinNotActive, Print, , WinActivate, Print,
    WinWaitActive, Print,
    MouseClick, left, 265, 281
    Sleep, 100
    Send, {END}{SHIFTDOWN}{HOME}{SHIFTUP}{CTRLDOWN}x{CTRLUP}%NumberOfPagesPrintedPerBooklet%

    ; first print command
    MouseClick, left, 418, 363
    Sleep, 100

    ; Variable inicialization
    TotalNumberOfPages:=(clipboard)
    FirstPageToPrint:=NumberOfPagesPrintedPerBooklet+1
    LastPageToPrint:=NumberOfPagesPrintedPerBooklet*2

    ; loop to send all (starting from the second) printing commands in sets of pages "NumberOfPagesPrintedPerBooklet" until all pages are processed
    Loop
    {

    ; check whether all pages are printed or the last page to be printed exceeds the number of pages
    if (FirstPageToPrint) > (TotalNumberOfPages)
    break ; Terminate the loop
    if (LastPageToPrint) > (TotalNumberOfPages)
    LastPageToPrint:=TotalNumberOfPages
    ; keep on selecting from the menu File->Print until the previous printing command is carried out and the File->Print command is again enabled
    Loop
    {
    MouseClick, left, 31, 36
    Sleep, 100
    MouseClick, left, 35, 271
    Sleep, 100
    WinWait, Print, , 1
    if ErrorLevel
    continue
    else
    break
    }

    IfWinNotActive, Print, , WinActivate, Print,
    WinWaitActive, Print,

    ; select correct page range for current iteration
    MouseClick, left, 173, 284
    Sleep, 100
    Send, {END}{SHIFTDOWN}{HOME}{SHIFTUP}%FirstPageToPrint%
    MouseClick, left, 264, 278
    Sleep, 100
    Send, {END}{SHIFTDOWN}{HOME}{SHIFTUP}%LastPageToPrint%
    ; Print command
    MouseClick, left, 436, 369
    Sleep, 100
    ; update variables
    FirstPageToPrint+=NumberOfPagesPrintedPerBooklet
    LastPageToPrint+=NumberOfPagesPrintedPerBooklet
    }
     
  8. stantley

    stantley

    Joined:
    May 22, 2005
    Messages:
    7,091
    But the bottom line is it works. Many times with these scripts if you can't find the best way to accomplish a task you have to improvise and come up with something that works and that's just what you did. For your first script you did a good job.

    Something I would try changing is:

    if (FirstPageToPrint) > (TotalNumberOfPages)
    break

    to

    if (FirstPageToPrint > TotalNumberOfPages)
    break

    It's the expression that should be enclosed in parenthesis and not the variables. It doesn't make much difference now, but when you get into more complex expressions it will.

    Another thing is I'm not crazy about is the way the screen recorder generates code.

    WinWait, Print,
    IfWinNotActive, Print, , WinActivate, Print,
    WinWaitActive, Print,

    can be simplified to

    WinActivate, Print
    WinWaitActive, Print

    The Sleep, 100 after every mouse click might not be needed. The script should run faster without them, but I would take them out one at a time and test it because sometimes the commands come too fast for Windows to handle.

    Also I've found that sending keyboard shortcuts instead of mouse clicks is usually more reliable.

    But, like I said, the bottom line is it works and that's all that really matters.
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/756918

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice