Wednesday, January 4, 2012

AutoHotKey - changing default printers

This particular post relates to Windows, specifically Windows 7, Windows XP, and using AutoHotKey to automate some daily tasks / annoyances...

The problem:
I sometimes need to print to one printer in one part of the building, and then shortly thereafter print to another printer in another part of the building, both on the network but in two different physical locations.  What to do?  This is where scripting a very rapid change of default printer would speed things up with a lot less clicking.  By setting up a hotkey to change printers, this can enable you to change printers quickly basically from the command line.  In this case, I want to use the printer in "reception", so I made the hotkey the letter "r" to make it easy for me to remember: (note: I added spaces around the hotkeys so they would display more clearly here, you should eliminate them)



+ # r :: 
     {
          Run, rundll32 printui.dll`, PrintUIEntry /y /q /n \\servername\printername 
          MsgBox Front Office Printer Enabled
          return
     }


Let's break it down:
Brackets: I'm not sure these are actually needed, but I like them so I use them to stay organized.
Run, : this command basically begins the command string in AutoHotKey.  I initially tried this using %comspec% but this was unwieldy and opened a terminal window and I think was too "crufty".  This method just runs the program directly for speed and efficiency.
The important potential source of error here is to escape the comma following printui.dll, because failing to do so will cause the action to fail.  It's a tiny little thing that you might have overlooked at first, but make sure you note it.
The switches are:
     /y - set printer as default
     /q - quiet mode, do not display error messages
     /n - printer name
I experimented with IP address and printer naming, but the name that was most consistent was to use the servername and then the printername found under printer properties / sharing.
The MsgBox is optional - once you prove to yourself that the printer is switching back and forth, you could eliminate the msgbox to reduce another keystroke - it all depends on your goals.

Hopefully this helps someone else as it has helped me.

1 comment: