Printing with AutoHotkey Made Simple (AutoHotkey Tip)

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 microsoft_key+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 microsoft_key+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.

AddPrintMenuItemPrinting 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

RunDialogBoxMany 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,

jack

Understanding Label Names and Subroutines (Beginning AutoHotkey Tip)

Once You Know How AutoHotkey Label Names Work, You Can Add More Tricks to Your Bag of AutoHotkey Skills

cheeseburger-ingredientsWhile in the past I often used the terms AutoHotkey Label and subroutine interchangeably, that’s not exactly accurate—although the two terms maintain a close relationship. Misunderstanding how Label names work can be a source of errors for new AutoHotkey script writers. However, knowing the differences between what constitutes a Label and a subroutine opens up new scripting opportunities. The CheeseBurgerRecipe.ahk script uses little-known Label behavior to either walk through all the steps or jump to individual sections of the recipe for a “Jack Stuffed Cheeseburger.” Continue reading

Why AutoHotkey for Chefs and Dieticians?

cheeseburgerwhiteicon While You Don’t Often See Television Chefs Using Computers, AutoHotkey Offers Guidance When Cooking “Jack Stuffed Cheeseburgers” at Home

*          *          *

While I came up with a pretty cool barebones recipe script for this AutoHotkey blog, I’m not sure how well computers and cooking mix. From what I’ve seen, professional chefs don’t have much time to mess with technology—except for possibly writing cookbooks—in which case, I would refer them to “Why AutoHotkey for Writers, Bloggers, and Editors?.”

*          *          *

cheeseburgercartoon

Using a computer while cooking presents a challenging task. Although looking up a recipe on a smartphone works great, poking at the device with fingers covered in cookie dough proves impractical. Ideally, any kitchen machine is lightweight and stands upright on its own.

Although a little pricey, the Microsoft Surface Pro (or another less expensive Windows ultralight laptop computer) might do the job. Large enough to read the screen, yet easy to move around a cooking area, these computers help any budding chef or dietician who needs to check recipes—as long as no one dumps boiling water on the keyboard. For size and convenience, finding equipment more practical than a lightweight laptop proves difficult. Continue reading

Change Script Features on the Fly with the Windows System Tray Icon Context Menu (AutoHotkey Tip)

Dynamically Switch the Actions of a Running AutoHotkey Script with a Simple Click of a System Tray Right-Click Menu Item

From Rick Corbett:

Hi, Jack,

Talking of “Adding Actions to Windows System Tray Icon Menu“, perhaps you would consider writing about using—for example—ToggleCheck, MenuItemName to amend a running script dynamically, i.e. change something (like toggling logging to a file versus a MsgBox), then reload.

*          *          *

AutoHotkey Library Deal
AutoHotkey Library Deal

That’s an excellent question, Rick! Often, after loading a script,  we want to either turn features off and on or change how they function. Adding separate Hotkeys or rewriting scripts becomes wearisome. But, what if we could add a feature to the Windows System Tray Icon Context Menu (right-click) which either toggles an action on and off or completely changes how it works? AutoHotkey offers a straightforward way to get it done. Continue reading

Why AutoHotkey for Students?

Ten Reasons Why Every Student Should Use the Free AutoHotkey Windows Tools! AutoHotkey Helps You So Much Academically That You’ll Think You’re Cheating! Plus, It’s Delicious Brain Food and Helps Get You a Job!

Using AutoHotkey should be mandatory for every student from high school on through college! Not merely because learning AutoHotkey invigorates the brain cells (which it does!), but robotsexismit makes all your Windows tasks so much easier that it seems unsporting. AutoHotkey takes the grunt work out academia making learning fun…almost.

(If you know a student who uses a Windows computer, then do him or her a favor by sending him or her a link to this article. If he or she doesn’t already know about AutoHotkey, he or she will thank you profusely.)

For those unfamiliar with the free open-source scripting language, AutoHotkey offers simple, yet powerful, commands for adding enhancements to any Windows computer. In addition to their simplicity, the little apps work in any Windows program or on any Web page. For a brief overview, see this “Introduction to AutoHotkey: A Review and Guide for Beginners.” Continue reading

New Hotkey Book! (AutoHotkey Tips and Tricks)

AutoHotkey Techniques and Best Practices E-Book for Automating Your Windows Computers with Hotkey Combinations—Includes Something for Everyone!

Whether you’re a noobie to AutoHotkey scripts or an advanced programmer, of all my books, AutoHotkey Hotkeys may be the most important for new little-known tricks and useful ideas. It’s not that the other books don’t cover significant features of AutoHotkey, but this book includes some of the most practical tips for adding power to your scripts. I didn’t plan it that way. Continue reading

Adding Actions to Windows System Tray Icon Menu (Intermediate AutoHotkey Tip)

Adding Hard-to-Remember Hotkeys to the AutoHotkey System Tray Icon Makes Sense—Plus, How to Best Activate the Last Window

I’ve put my MousePrecise AutoHotkey script on my own list of favorites. I like the mouse cursor accuracy it adds to any of my graphic applications. I find it particularly useful when selecting a portion of an image for cropping or moving.

However, I recently ran into an issue where the script stopped working in Paint.NET (more details on the source of the problem later in this blog). I tried reloading the script, but to no avail. I decided to use the Hotkey I implemented for adding the window class to the group (GroupAdd command). Humm…I couldn’t recall the key combination. This lapse of memory forced me to open the script and dig through the code. Continue reading