0
votes

I'm new to AutoHotKey, so this could be something bone-headed on my part. I put this together yesterday, and it worked fine all day. Today it is failing to fully execute at random times.

What I'm doing is forcing Chrome to use the System Print Dialog, and then printing only the first page.

!NumpadDiv::
{
    Send ^P              ; CTRL+SHIFT+p forces System Print Dialog
    WinWait, Print, , 2  ; Wait for Print Dialog to appear
    if ErrorLevel
    {
        MsgBox, WinWait timed out.
        return
    }
    else
    {
        IfWinActive, Print    ; Added to ensure Print window was active
        {
        SetKeyDelay, 300    ; Upped key delay. Thought this may help. It hasn't
            Send {TAB 4}        ; Tabs over to Page Range
            Send 1              ; Print only first page
            Send {Enter}
        }
    }
    return
}

Today the script started failing at random times. Either it does not tab over to the page range, and will print all pages, or it will tab to it, enter 1 then not print. In both cases, it triggers my PC's error sound. Any help would be appreciated, I'm very confused since this was working without issue all day yesterday.

1
Don't send keys if you can access window controls directly. Instead of tabbing and sending, try this: ControlSetText, Edit4, 1. Maybe, the control has another name, you can check that out with window spy. Also, the IfWinActive is absolutely redundant, since WinWait guarantees that anyway. - MCL
MCL - you should propose an answer, I think. - bgmCoder
@MCL Thanks for the suggestions. Like I said, still very new to this, so I'll give it a shot and test it out - Casey Langford
@MCL ControlSetText did the job. It looks to be working consistently. Thanks for the help! I also removed the redundant WinWait, which I knew was probably not going to help anything to begin with. - Casey Langford
Please post your solution as an answer and accept it. - MCL

1 Answers

0
votes

I want to do similar thing in Lotus Notes too. I've revisited clangford1174's code based on MCL's advice as below. It works on my Win7 machine.

!NumpadDiv::
{
    Send ^P              ; CTRL+SHIFT+p forces System Print Dialog
    WinWait, Print, , 2  ; Wait for Print Dialog to appear
    if ErrorLevel
    {
        MsgBox, WinWait timed out.
        return
    }
    else
    {   
        ControlSetText, Edit4, 1
        Send {Enter}        
    }
    return
}

BTW, This thread on selecting printer with AHK is relevant too. How to select printer and print with AutoHotKey