Get or Set selected printer in Print Window Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MonuKashyap
Posts: 112
Joined: 06 Jun 2016, 21:32

Get or Set selected printer in Print Window

05 Dec 2021, 04:30

How to Get and/or Set "Selected" Printer (Not Default Printer) in Print Window as shown in image
and :happybday: to you in Advance :dance:
Attachments
PrintIssue.png
PrintIssue.png (23.52 KiB) Viewed 1466 times
User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Get or Set selected printer in Print Window

05 Dec 2021, 06:45

This is my cheater's method. If you search the forum for "ExtListView", you will find a library that provides more features. I have not tried it.

Code: Select all

printerNumber = 4
F3::
Send ^p
WinWaitActive, Print ahk_class #32770,, 3
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the print dialog.
Else Send % "{Home}{Down " printerNumber - 1 "}"
Return
MonuKashyap
Posts: 112
Joined: 06 Jun 2016, 21:32

Re: Get or Set selected printer in Print Window

05 Dec 2021, 20:29

mikeyww wrote:
05 Dec 2021, 06:45
This is my cheater's method. If you search the forum for "ExtListView", you will find a library that provides more features. I have not tried it.

Code: Select all

printerNumber = 4
F3::
Send ^p
WinWaitActive, Print ahk_class #32770,, 3
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the print dialog.
Else Send % "{Home}{Down " printerNumber - 1 "}"
Return
hey mikey,,
thanks for the reply and taking interest in the topic :thumbup:

The idea is good, but its not reliable,,
sometimes it will select another printer
(mostly, at the time of system hanging)
User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Get or Set selected printer in Print Window

05 Dec 2021, 20:36

OK. You could try the ExtListView. You might have better luck than I did.
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Get or Set selected printer in Print Window  Topic is solved

05 Dec 2021, 22:39

Code: Select all

$q::SelectPrinter("Bullzip PDF Printer")    ; <<--- put the printer name you want to print with here

SelectPrinter(Printer)
{
    static wintitle := "Print ahk_class #32770"
    WinWait, % wintitle,, 10
    if (ErrorLevel)
        return false
    ControlGet, LV, List,, SysListView321, % wintitle
    Loop, Parse, LV, `n, `r
    {
        currPrinter := SubStr(A_LoopField, 1, InStr(A_LoopField, A_Tab) - 1)
        if (currPrinter = Printer) {
            ControlGet, HLV, Hwnd,, SysListView321, % wintitle
            return LV_DoubleClickRow(HLV, A_Index)
        }
    }
    return false
}

LV_DoubleClickRow(HLV, Row)    ; You can thank Justme for this function
{   ; HLV : ListView's HWND, Row : 1-based row number
    HPROC := 0
    MyPID := DllCall("GetCurrentProcessId", "UInt")
    WinGet, LVPID, PID, ahk_id %HLV%
    If !(LVPID)
    Return False
    VarSetCapacity(RECT, 16, 0)
    If (LVPID <> MyPID) {
        If !(HPROC := DllCall("OpenProcess", "UInt", 0x0438, "Int", False, "UInt", LVPID, "Ptr"))
            Return False
        If !(Addr := DllCall("VirtualAllocEx", "Ptr", HPROC, "Ptr", 0, "UPtr", 16, "UInt", 0x1000, "UInt", 4, "UPtr"))
            Return (DllCall("CloseHandle", "Ptr", HPROC) & 0) ; False
    }
    Else
        Addr := &RECT
    SendMessage, 0x1013, % (Row - 1), 1, , ahk_id %HLV% ; LVM_ENSUREVISIBLE
    SendMessage, 0x100E, % (Row - 1), %Addr%, , ahk_id %HLV% ; LVM_GETITEMRECT
    If (HPROC) {
        DllCall("ReadProcessMemory", "Ptr", HPROC, "Ptr", Addr, "Ptr", &RECT, "UPtr", 16, "Ptr", 0)
        DllCall("VirtualFreeEx", "Ptr", HPROC, "Ptr", Addr, "UPtr", 0, "UInt", 0x8000)
        DllCall("CloseHandle", "Ptr", HPROC)
    }
    POINT := NumGet(RECT, 0, "Short") | (NumGet(RECT, 4, "Short") << 16)
    PostMessage, 0x0201, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONDOWN
    PostMessage, 0x0202, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONUP
    PostMessage, 0x0203, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONDBLCLK
    PostMessage, 0x0202, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONUP
    Return True
}
HTH
garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Get or Set selected printer in Print Window

06 Dec 2021, 13:47

toggle between 2 printers ( make default )

Code: Select all

printerA:="Pantum M6600NW Series"
;printerB:="Samsung SCX-4200 Series (USB001)"
printerB:="Samsung Network PC Fax"
return

!h::
T:=!T
if (T=1)
  {
  msgbox,%printera%
  runwait,%comspec% /c rundll32 printui.dll`,PrintUIEntry /y /n "%printerA%",,hide    ;- make printer-A default
  }
else
  {
  msgbox,%printerb%
  runwait,%comspec% /c rundll32 printui.dll`,PrintUIEntry /y /n "%printerB%",,hide    ;- make printer-B default
  }
;run,shell:PrintersFolder
run,control printers
return
;============================================================================================

MonuKashyap
Posts: 112
Joined: 06 Jun 2016, 21:32

Re: Get or Set selected printer in Print Window

06 Dec 2021, 21:28

Xtra wrote:
05 Dec 2021, 22:39

Code: Select all

$q::SelectPrinter("Bullzip PDF Printer")    ; <<--- put the printer name you want to print with here

SelectPrinter(Printer)
{
    static wintitle := "Print ahk_class #32770"
    WinWait, % wintitle,, 10
    if (ErrorLevel)
        return false
    ControlGet, LV, List,, SysListView321, % wintitle
    Loop, Parse, LV, `n, `r
    {
        currPrinter := SubStr(A_LoopField, 1, InStr(A_LoopField, A_Tab) - 1)
        if (currPrinter = Printer) {
            ControlGet, HLV, Hwnd,, SysListView321, % wintitle
            return LV_DoubleClickRow(HLV, A_Index)
        }
    }
    return false
}

LV_DoubleClickRow(HLV, Row)    ; You can thank Justme for this function
{   ; HLV : ListView's HWND, Row : 1-based row number
    HPROC := 0
    MyPID := DllCall("GetCurrentProcessId", "UInt")
    WinGet, LVPID, PID, ahk_id %HLV%
    If !(LVPID)
    Return False
    VarSetCapacity(RECT, 16, 0)
    If (LVPID <> MyPID) {
        If !(HPROC := DllCall("OpenProcess", "UInt", 0x0438, "Int", False, "UInt", LVPID, "Ptr"))
            Return False
        If !(Addr := DllCall("VirtualAllocEx", "Ptr", HPROC, "Ptr", 0, "UPtr", 16, "UInt", 0x1000, "UInt", 4, "UPtr"))
            Return (DllCall("CloseHandle", "Ptr", HPROC) & 0) ; False
    }
    Else
        Addr := &RECT
    SendMessage, 0x1013, % (Row - 1), 1, , ahk_id %HLV% ; LVM_ENSUREVISIBLE
    SendMessage, 0x100E, % (Row - 1), %Addr%, , ahk_id %HLV% ; LVM_GETITEMRECT
    If (HPROC) {
        DllCall("ReadProcessMemory", "Ptr", HPROC, "Ptr", Addr, "Ptr", &RECT, "UPtr", 16, "Ptr", 0)
        DllCall("VirtualFreeEx", "Ptr", HPROC, "Ptr", Addr, "UPtr", 0, "UInt", 0x8000)
        DllCall("CloseHandle", "Ptr", HPROC)
    }
    POINT := NumGet(RECT, 0, "Short") | (NumGet(RECT, 4, "Short") << 16)
    PostMessage, 0x0201, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONDOWN
    PostMessage, 0x0202, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONUP
    PostMessage, 0x0203, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONDBLCLK
    PostMessage, 0x0202, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONUP
    Return True
}
HTH

OMG!! OMG!! OMG!!



its, its... working!!


I would like to know 2 things
1) How to select only [No doubble click, No print, only select]
2) Is Justme = X
in comment line of function ---> "; You can thank Justme for this function"
MonuKashyap
Posts: 112
Joined: 06 Jun 2016, 21:32

Re: Get or Set selected printer in Print Window

06 Dec 2021, 21:34

garry wrote:
06 Dec 2021, 13:47
toggle between 2 printers ( make default )

Code: Select all

printerA:="Pantum M6600NW Series"
;printerB:="Samsung SCX-4200 Series (USB001)"
printerB:="Samsung Network PC Fax"
return

!h::
T:=!T
if (T=1)
  {
  msgbox,%printera%
  runwait,%comspec% /c rundll32 printui.dll`,PrintUIEntry /y /n "%printerA%",,hide    ;- make printer-A default
  }
else
  {
  msgbox,%printerb%
  runwait,%comspec% /c rundll32 printui.dll`,PrintUIEntry /y /n "%printerB%",,hide    ;- make printer-B default
  }
;run,shell:PrintersFolder
run,control printers
return
;============================================================================================

Hi garry,,
Thank you too for showing interest in this topic

The problem is "pritner selection is getting changed after once print button clicked for next print, even another printer is already defaulted"
and,
my situation is the print pop-up is coming by a website online,,.
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Get or Set selected printer in Print Window

06 Dec 2021, 22:11

MonuKashyap wrote:
06 Dec 2021, 21:28
I would like to know 2 things
1) How to select only [No doubble click, No print, only select]
2) Is Justme = X
in comment line of function ---> "; You can thank Justme for this function"
Its using double click so you don't have to select and then click the print button clearly easier? I don't see a point in selecting a printer and doing nothing else.
:arrow: @just me is the one you can thank.

Try commenting out these lines:

Code: Select all

    PostMessage, 0x0203, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONDBLCLK
    PostMessage, 0x0202, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONUP
MonuKashyap
Posts: 112
Joined: 06 Jun 2016, 21:32

Re: Get or Set selected printer in Print Window

07 Dec 2021, 00:31

Xtra wrote:
06 Dec 2021, 22:11
MonuKashyap wrote:
06 Dec 2021, 21:28
I would like to know 2 things
1) How to select only [No doubble click, No print, only select]
2) Is Justme = X
in comment line of function ---> "; You can thank Justme for this function"
Its using double click so you don't have to select and then click the print button clearly easier? I don't see a point in selecting a printer and doing nothing else.
:arrow: @just me is the one you can thank.

Try commenting out these lines:

Code: Select all

    PostMessage, 0x0203, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONDBLCLK
    PostMessage, 0x0202, 0, POINT, , ahk_id %HLV% ; WM_LBUTTONUP
actually the situation is:
There are 2 printers
1) To pdf
2) To paper

and here "To pdf" is compulsary, and "To paper" is optional
so if we want to print to paper so we need only to press enter :lol:

and, your solution is working superb,,
Thanks a lot :superhappy:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, mmflume and 162 guests