Help setting default printer (picking the right one if it moves) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
whitenack
Posts: 3
Joined: 29 Feb 2020, 09:30

Help setting default printer (picking the right one if it moves)

29 Feb 2020, 09:56

Hello all...first post! I recently found AHK and have really been enjoying its functionality. I can do the basic things, but am starting to get more complicated and need some clarification on things.

I have the need to set a default printer at computer start. I have created a script that points the mouse to a place where the printer should be in the window, and then selects it. This works some of the time. The problem comes when the printer window gets resized...this causes the printer to move to a different position in the window, and the wrong printer is chosen. I need a step that finds the printer wherever it is in the window, then selects it.

Here is what I have so far...

Code: Select all

Run, control printers ; opens the Devices and Printers settings page where the printers are listed
WinWaitActive, Devices and Printers
sleep, 500
click, right, 389, 489 ; right clicks on the printer that I want to choose (depending on the sizing of the window)
sleep, 500
click, 423, 527 ; chooses "Set as default printer" 
Any help would be appreciated.

Thanks
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Help setting default printer (picking the right one if it moves)

29 Feb 2020, 12:53

Try to use the following functions, these should work more stable:

Code: Select all

	Printer_SetDefault(sPrinter){
		DefaultPrinter := Printer_GetDefault()
		If DefaultPrinter = %sPrinter% ; Checks if current printer is correct
		{	return
		}
		
		DllCall("system32\winspool.drv\SetDefaultPrinterW", "str", sPrinter)
		TrayTip, Printers and Faxes, %sPrinter% set as default printer., 10, 17
		
		; waits till default printer is correct
		while (DefaultPrinter != sPrinter ) {	
			DefaultPrinter := Printer_GetDefault()
		}
		DefaultPrinter = 
		
		;Printer_SetDefault("\\PS22\HP Designjet T790 PS") (example)
		return
	}
	
	Printer_GetDefault() {
		nSize := VarSetCapacity(gPrinter, 256)
		DllCall("system32\winspool.drv\GetDefaultPrinterW", "str", gPrinter, "UintP", nSize)
		if gPrinter not contains \\ ; if not a server printer
		{	;DllCall("winspool.drv\GetDefaultPrinterA", "str", gPrinter, "UintP", nSize)
			DllCall(A_WinDir . "\system\winspool.drv\GetDefaultPrinterA", "str", gPrinter, "UintP", nSize)
			
		}
		
		return gPrinter
	}
whitenack
Posts: 3
Joined: 29 Feb 2020, 09:30

Re: Help setting default printer (picking the right one if it moves)

01 Mar 2020, 07:49

Thanks for the reply.

This is way over my head...do I just copy and paste this into my script to replace what I had? Where, in your script, do I plug in which printer I want to be the default?
User avatar
DataLife
Posts: 460
Joined: 29 Sep 2013, 19:52

Re: Help setting default printer (picking the right one if it moves)  Topic is solved

01 Mar 2020, 08:48

@whitenack

Here you go. One line solution.
Just put in the EXACT name of your printer.

Put only this line in the autoexecute section of your script.

Code: Select all

RunWait, %WinDir%\System32\RUNDLL32.exe PRINTUI.DLL`,PrintUIEntry /y /n "Canon MG2500 series Printer"
Only tested on Windows 10, Ansi and Unicode Autohotkey.

Note:...........................
If you want to use ahk_users code instead of my 1 line then put this

Code: Select all

Printer_SetDefault("Canon MG2500 series Printer")
in the autoexecute section and ahk_users code after the autoexecute section. ahk_users code only works on Unicode. At least I tried it on Ansi and it did not work. I tried it on Unicode and it did work.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
DataLife
Posts: 460
Joined: 29 Sep 2013, 19:52

Re: Help setting default printer (picking the right one if it moves)

01 Mar 2020, 09:08

@AHK_user
I had to remove \\ in your example to get it to work.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
whitenack
Posts: 3
Joined: 29 Feb 2020, 09:30

Re: Help setting default printer (picking the right one if it moves)

01 Mar 2020, 14:08

DataLife wrote:
01 Mar 2020, 08:48
@whitenack

Here you go. One line solution.
Just put in the EXACT name of your printer.

Put only this line in the autoexecute section of your script.

Code: Select all

RunWait, %WinDir%\System32\RUNDLL32.exe PRINTUI.DLL`,PrintUIEntry /y /n "Canon MG2500 series Printer"
Awesome. This worked perfectly and cleanly. Behind the scenes.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest, Descolada, Perpendie and 329 guests