While Other Techniques Exist for Printing Directly from AutoHotkey, Make It Easy by Using Print Drivers from Other Programs
When I first considered printing from the cheeseburger recipe script, I didn’t realize how complicated printing from AutoHotkey can get. With any printing, a number of factors come into play. First, each printer model uses particular print drivers. Second, the format of the document affects the output and drivers. Third, you may need to choose from multiple printers.
While I found a number of solutions, they tended to involve more advanced techniques using DllCall(), print spoolers, and other enigmatic code. This plays well for anyone comfortable with Windows’ inner workings and hidden mechanisms but gets too complicated for the average AutoHotkey user. I always look for the simplest (and sometimes the most effective) method to get things done.
The problems mentioned above (print drivers, file formats, and multiple printers) never go away. AutoHotkey scripts need tailoring to specific setups and circumstance. So why reinvent the wheel? Most Windows programs solved these printing problems long ago. By default, CTRL+P opens the print dialog in Windows applications, but if you only want to print directly to the default printer without the dialog, then the Windows Command Prompt or the Run dialog box (WIN+R or +R) may be your best bet. In most situations, you can take advantage of those prepackaged software solutions.
Using Windows Program Command Line Capabilities
Windows programs allow launching from the Command Prompt window. As long as the right parameters exist and you include them, you can take advantage of the apps printing capability. Since most software matches a particular file type (e.g. TXT for Notepad, JPG or GIF for Windows Paint, PDF for Foxit Reader), you can match your printing routines to the file type through the external software. Thus, the application does all the print formatting work for you.
In AutoHotkey, the AutoHotkey Run command serves the same purpose as the Run (WIN+R or +R) dialog box in Windows.
Using Windows Notepad for Printing Text
One of the simplest methods for printing text in AutoHotkey uses the ubiquitous Windows Notepad app. As a no-frills text processor, it can format and print any text file. Best of all, Notepad can print the text file through the AutoHotkey Run command.
Open a text file in Notepad through AutoHotkey with simply:
Run, Notepad.exe FileName.txt
To silently print (no print dialog), add the print switch (/p):
Run, Notepad.exe /p FileName.txt
Using the /p switch, AutoHotkey launches Notepad running the built-in print drivers to format and send the text file to the default printer. (A Notepad window opens, loads the text file, sends it to the printer, then immediately closes—possibly so fast that you miss it.) For example, AutoHotkey can use Notepad to print the “Jack Stuffed Cheeseburger” recipe.
Printing the Cheeseburger Recipe
Not everyone keeps a computer in their kitchen. Even then, a recipe printed on a piece a paper offers easy access to ingredients and instructions without putting a machine in the kitchen—at risk to spills and other culinary disasters. Plus, carrying a printed list of ingredients to the grocery store often proves advantageous—even over a smartphone.
To add the print feature to the CheeseBurgerRecipe.ahk script, I first use the Menu command to insert a new Print All menu item—as shown at the right:
Menu, tray, add, Print All, Print
then, I write the accompanying Label subroutine Print:
Print: FileDelete, RecipePrint.txt FileAppend , % "Jack Stuffed Cheeseburgers`r`r" . Ingredients . "`r`r" . Mix . "`r`r" . Cook . "`r`r" . Serve, RecipePrint.txt Run, Notepad.exe -p RecipePrint.txt Return
(For display purposes, the FileAppend line of code uses the force expression operator (%) and line continuation techniques to wrap the single line of code into multiple lines.)
First, just in case a residue file from a previous print operation exists, the FileDelete command executes. (This FileDelete could just as easily appear after the Run command line.) Next, the FileAppend command adds the text to a temporary file (RecipePrint.txt), then, using the Run command, prints the file through Windows Notepad.
Note: I also added an option to print individual sections of the recipe (e.g. only the list of ingredients) to each MsgBox section of the recipe using the built-in Help button and a PrintItem() function. A more complex approach, I implemented techniques discussed in the “Tips for Optimizing the Standard AutoHotkey Message Box (MsgBox) Command” blog. I’ll leave this discussion for another time.
Windows Program Switches
Many programs include parameters or switches to perform specific functions either through the Windows Command Prompt window or the Run dialog box—shown at right. Those commands and switches work the same way with the AutoHotkey Run command. Not all programs offer command line switches, but enough free programs exist to allow the printing of almost any type of file via AutoHotkey. A little investigation answers any questions.
A search of the Web for Windows program switches (normally a slash / or dash – followed by one or more characters) usually yields the parameters needed for printing (e.g. /p or -p) in conjunction with the Run command. Some programs also offer switches for selecting the target printer (e.g. /t or /pt). However, many applications don’t offer any command line print options at all. Therefore, you must write your scripts to use the software application which works best for your purposes.
Notepad Versus Wordpad for Printing
While Notepad works great for text, Windows Wordpad offers similar results. Notepad might respond a little quicker, but Wordpad prints more file formats:
Run, write /p "c:\AutoHotkey\Spicy Beef Minestrone.docx"
In the above line of code, AutoHotkey instructs Wordpad to load, print the Microsoft Word DOCX file, then close. (Tip: The term “Wordpad” works in place of “write” as the program source.) Generally, AutoHotkey prints any format available for reading and saving files within a specific software package. In the case of Wordpad, AutoHotkey can also send to a printer the free OpenOffice (LibreOffice) document format (ODT), as well as, the Rich Text Format (RTF).
Note: Not all programs accept command line switches. Therefore, your favorite software might not work for printing directly from AutoHotkey.
* * *
For more information about LibreOffice, get Rob Spahitz’s book Getting Started with OpenOffice (LibreOffice): The Free Replacement for Microsoft Office at Amazon.com.
* * *
Printing Graphics Files
Suppose you want to print any graphic file such as BMP, JPG, PNG, GIF, or TIF? You might find a number of options, but you need to test them with the AutoHotkey Run command.
I tested the Microsoft Paint program, but, for me, regardless of efforts to change default settings, the printer always used the landscape print mode and expanded the image to fill the page. I moved over to my favorite free image viewer, IrfanView, achieving much better results. But even then I needed to tinker with the print setup in the program.
Tip: For most programs, the AutoHotkey Run command with a print switch uses the default print setup for the program. If you don’t get what you want (e.g. landscape versus portrait mode), open the file in the program, change the print setup, and print. Then close the program. I found in a number of Windows programs that this resets the default printing mode for AutoHotkey. Generally, the last print setup used in a Windows program gets saved as the default.
I found a list of the IrfanView print parameters on the Web. For IrfanView, be sure to include the complete path to the program:
Run, C:\Program Files (x86)\IrfanView\i_view32.exe "C:\AutoHotkey\Cheeseburger Cook.png" /print
If any spaces occur in the print file’s name or path, be sure to enclose it in quotation marks. The Run command does not require double quotes around the command program path and filename—even if spaces occur within it.
Notice that for IrfanView the print switch includes the entire word /print. IrfanView should work for most graphic file formats but test it to be sure.
Printing More File Formats
While many Windows programs don’t include print run parameters, I found this list of common command line switches for popular software. You may find your favorite programs here.
For PDF files, I use the free Foxit Reader. It turns out that FoxitReader also does a great job for printing PDF files through AutoHotkey. From the above list of common command line switches:
Print a PDF file silently to the default printer:
“FoxitReader.exe” /p PdfFile
Print a PDF file silently to an alternative printer:
“FoxitReader.exe” /t PdfFile PrinterName
I simply added the following to an AutoHotkey script:
Run, FoxitReader.exe /p C:\AutoHotkey\PortableDoc.pdf
AutoHotkey prints the PDF file through the Foxit Reader program.
Notice that the command works without including the complete path to the file Foxit Reader.exe:
C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe
That means that during installation either the program was added to the system or user path where Windows automatically searches or a shortcut was added to the Windows Registry, such as:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\foxitreader.exe
In many cases, a shortened program name won’t work with Run command. For example, IrfanView requires the full path in the Run command. That means IrfanView did not get added to a system or user path nor the Registry during installation.
As mentioned in the tip above, I found that changing and saving the print settings in Foxit Reader fixed any formatting problems when printing from AutoHotkey.
Expanding AutoHotkey Print Opportunities
Many other file formats exist which demand different print drivers. Rather than finding a program for each format, turning to one free software program with drivers for multiple formats makes things easier. The free LibreOffice suite of programs offers print drivers for numerous file formats including Microsoft Word, Excel, and PowerPoint, plus, all the OpenOffice formats (ODT, ODS, etc.). (For an introduction to OpenOffice/LibreOffice, see Rob Spahitz book Getting Started with OpenOffice (LibreOffice): The Free Replacement for Microsoft Office.)
* * *
For more information about LibreOffice, get Rob Spahitz’s book Getting Started with OpenOffice (LibreOffice): The Free Replacement for Microsoft Office at Amazon.com.
* * *
Although different command EXE files exist for each package in the suite, LibreOffice allows one main path for all supported print formats with one soffice.exe command file:
Run, C:\Program Files (x86)\LibreOffice 4\program\soffice.exe -p "c:\AutoHotkey\Spicy Beef Minestrone.docx"
This AutoHotkey Run command prints a Microsoft Word file. (Note that the LibreOffice print switch uses a dash rather than a forward slash.)
I found soffice.exe in the App Paths section of the Windows Registry discussed above. That means we can shorten the Run command line by eliminating the program path:
Run, soffice.exe -p ExcelFile.xls
This line prints a Microsoft Excel file using LibreOffice print drivers.
If you need to print other file formats, find software which both supports the file type and allows printing from the Windows Run dialog box.
While other methods for printing from AutoHotkey exist, I found it easy to use the print drivers in other (usually free) software packages.
* * *
Like anybody else, I have expenses and a need to make ends meet. As Jack’s AutoHotkey Blog increases in popularity, coding the test scripts and writing the blogs takes up more of my time. That means I’ve less time to pursue other income earning opportunities. I don’t plan to ever move Jack’s AutoHotkey Blog behind a paywall, but if you think my efforts are worth a bit of your hard-earned cash, then you can offer a token of your appreciation by purchasing one or two of my AutoHotkey books. You may not need the references yourself, but you might know someone who can benefit from one or two of them.
Thank you,
Thank you very much for this! I needed it!
LikeLike
Hi, Jack.
My admiration and respect for your knowledge, and my thanks for your valuable contributions. Congratulations and happy new year.
LikeLike