Stuffing More into AutoHotkey Pop-up Menus (AutoHotkey Tip)

Rather Than Increasing the Length of a System Tray Menu, Add Submenus—Plus, How to Use Menu Names (A_ThisMenu) for Conditional Actions

cheeseburgerrecipeWhen I decided to add two more recipes to the cheeseburgerwhiteicon Jack Stuffed Cheeseburger script, I ran into the problem of adding too many items (one for each step in each recipe) to the System Tray right-click menu. In the original, Cheeseburger.ahk script, I only included four steps in the menu (Ingredients, Prepare, Cook, and Serve). However, after inserting the Animal-Style Cheeseburger and the Jack Stuffed Cheeseburger (Animal Style) into the script, the number of recipe increments jumped from four to 18—plus an additional Print Recipe option for each burger. Also, since many of the steps use the same (or similar names), I needed a method for identifying each step with the proper recipe. When increasing the number of AutoHotkey menu options, cumbersome lists of items commonly crop up. You can fix these bloated menus by using submenus. Continue reading

New “Why AutoHotkey?” Book Available Free on Amazon

For One Day, Monday, May 1, 2017, the Just Published E-Book Is Available for the Kindle and Kindle Apps on Amazon.com

CoverWhy200I’ve made the new e-book Why AutoHotkey? available exclusively on Amazon and you can get the book free. It’s not that you need the book since most of its contents can be found right here on the Why AutoHotkey? page. I’ve produced this book for people who don’t know about or use AutoHotkey. As you are already accessing this blog, you’re likely well aware of AutoHotkey. My goal with this new book is to reach the AutoHotkey unaware. I’m guessing the Amazon is loaded with those types of Windows users.

I plan to make the book free more times, but Amazon only allows me to give it aways five times in a three-month period. (I would always make it free if they would let me.) Don’t worry if you miss this one. I’ll announce in this blog whenever I schedule another free book day. (Sign-up to follow the blog at the upper right of this page if you want notification whenever a new blog comes out.)

I know…I’ve expressed my disdain for the way Amazon treats independent writers, but they have such a huge reach, it would be silly for me to completely ignore them. (I still prefer people buy from ComputorEdge E-Books, but everyone must have an option.) This new compilation book should help the uninitiated to understand how much power AutoHotkey can bring to their Windows PC.

 

Create a Universal MsgBox Print Function with ControlGetText (AutoHotkey Tip)

When Nothing Else Works for Copying Text, Try the ControlGetText Command and Create a Global MsgBox Print Function

In a previous blog, I highlighted the Control, EditPaste command. The command helped me solve a particular problem where the standard Send, ^v (Windows paste shortcut) responded too slowly. I’ve since discovered that the complementary ControlGetText command resolves some sticky MsgBox printing troubles. It not only works quicker than the Windows copy shortcut (Send, ^c) but it greatly reduces code. Continue reading

Why AutoHotkey for Teachers and Educators?

If You and Your Students Have Access to Windows Computers, Then You’re Set! If Not, Well…

The varied computer situations of educators present a particular problem for AutoHotkey use. AutoHotkey works only on Windows computers. Fortunately, 90% of the desktop and laptop computers currently in use run Microsoft Windows. On the downside for teachers, they may or may not RobotWordProcessorCartoonhave access to Windows machines in the classroom. Plus, even if students have a computer available at home, not all of them are Windows PC. This makes it difficult to create consistent computer-dependent lesson plans covering everyone in the class.

Noting the difficulties with applying a Windows-based program to schooling, I forge on with the possibilities for those instructors in a position to take advantage of AutoHotkey.

AutoCorrect That Teaches

AutoHotkey Library Deal
AutoHotkey Library Deal

One of the most common ways to use AutoHotkey involves the implementation of the AutoCorrect.ahk script which fixes misspelled words on the fly. The slightly modified AutoCorrect.ahk script which came from the AutoHotkey Download Web site instantly corrects commonly misspelled English words while typing in any Windows word processor or Web editing page. See “Add AutoCorrection to All Your Windows PC Programs with AutoHotkey.”

The innovative educator can change up the AutoCorrect script to include those annoying words students regularly interject into something more appropriate and/or educational. For example, suppose a teacher notices that students overuse the word “fat” in their papers. Add an alternative Hotstring to a special AutoCorrect script which might include an automatic replacement:

::fat::stout

overusedwordsOf course, numerous other options exist (i.e. corpulent, fleshy, beefy, paunchy, plump, full, rotund, tubby, pudgy, chubby, chunky, burly, bulky, elephantine). Other possible AutoHotkey approaches similar to the OverusedWords.ahk script might work better. This Hotstring app pops up a menu of replacements for a list of overused words—as determined by English teachers when grading papers.  (See image at right which offers substitutes for the work angry.) While primarily for students, this script works for anyone hoping to improve their writing. Chapter Eleven of Beginning AutoHotkey Hotstrings discusses this script.

Another creative AutoHotkey script might use these same techniques found in the OverusedWords.ahk script to pop-up educational windows (MsgBox command rather than a menu of replacement words) whenever a student types specific words on their windows computer. For example, suppose you want to reinforce the idea that Columbus set sail for the new world in 1492? Include the following code in any AutoHotkey script:

:b0:1492::
  MsgBox, Columbus sailed the ocean blue!
Return
:b0:Columbus::
  MsgBox, Columbus set sail for the new world in 1492!
Return

After loading the two Hotstrings (1492 and Columbus), whenever typing either string followed by the space key or punctuation, the respective MsgBox windows pop up with the additional information.

The Web as a Reference

AutoHotkey has the capability to make any Web lookup easier. The short beginning AutoHotkey strip SynonymLookup.ahk demonstrates how to use the Windows Clipboard to quickly access reference sites on the Web. In this case, after hitting the Hotkey combination CTRL+ALT+L, Thesaurus.com opens in the default browser using the highlighted word in any text to find synonyms. (Referenced in the blog “Why AutoHotkey for Writers, Bloggers, and Editors?“) The ZIP file includes both the AHK script and the compiled EXE file which runs on any Windows PC—even without AutoHotkey installed.

More Word Choice

rhymepopupmenuSpecifically written for the poet inside us all, the Rhymes Pop-up script MenuRhymeMenu.ahk uses both Web lookup and the sample menu from the OverusedWords.ahk script to find rhymes. Simply highlight the target word (house in the example shown at the right), then use the CTRL+ALT+R Hotkey combination. AutoHotkey accesses the http://www.rhymer.com Web site and parses the list of soundalike words for addition to the pop-up menu.

The PoeticWords.ahk script includes Hotstrings which instantly change five cent words into more exotic 50¢ permutations. The PoeticWords.ahk script contains over 500 Hotstrings for replacing common words with more pretentious counterparts.

These two scripts illustrate examples of how AutoHotkey might help both teachers and students in their quest for beautiful words (PoeticWords.ahk) and/or rhymes (RhymeMenu.ahk)—first introduced in “Why AutoHotkey for Poets?

Early Childhood Education

CowSplashAutoHotkey offers creative educational opportunities far beyond the manipulation of the English language. Simple multimedia scripts enhance learning for the younger student. As an example, the NumbersSpeak.ahk script includes the cow-skating.jpg (image file) and cow-madcow.wav (audio file) putting together an entertaining expression of sight and sound. Discussed in the blog “AutoHotkey Scan Codes, Speech, Sound, and Splash Images in Children’s Apps.”

The NumbersSpeak.ahk script uses the speaking SayWhat.ahk script as a basis for exploring techniques in writing children’s educational software. The script employs the AutoHotkey SplashImage and SoundPlay commands, plus the ComObject function ComObjCreate(“SAPI.SpVoice”).Speak().

Press the letter “C”, and the script displays the SplashImage, reads the words, then spells “cow”, followed by the sound of a laughing mad cow mooing.

While the script is far from complete, it features some of the AutoHotkey tools available for building cute children’s scripts. It is an apt companion for the educational TalkingText.ahk script which voices the letters of the keyboard. then makes animal sounds when certain animal names are spelled (e.g. cat, dog, bear, and more).

A Talking Keyboard

In the blog “Why AutoHotkey for Grandparents?“, I introduce a one-line AutoHotkey script which makes the keyboard voice the previously pressed key by pressing the SPACEBAR:

~Space::ComObjCreate("SAPI.SpVoice").Speak(A_PriorKey)

Load this script and let the little ones press keys, then the SPACEBAR to hear the key names spoken.

More Multimedia Presentations

The PhoneRing.ahk script (included in Chapter Six of the AutoHotkey Applications e-book) combines a graphic file, an audio file, and the computer voice to display a multimedia message. Activated with ALT+P, it demonstrates the use of a splash image with audio as a message, compiling all the files into one executable package which not only runs on any Windows computer but cleans up after itself.

Quiz Timing and Classroom Reminders

If you happen to keep a Windows computer on your classroom desk, other administrative tasks become easier. The EggTimer.ahk app script opens a window (after loading, press CTRL+F12) where you set the desired test time interval, then click Start. The time counts down to zero while a progress bar works its way from left to right. Once the countdown increments to zero, a bell rings, the computer voice says “Your eggs are ready!”, and a window pops up with the same message. Now you have the perfect soft boiled egg (or, with a simple modification, maybe “Put your pencils down!”). Included in the AutoHotkey Applications e-book.

The Reminder.ahk script has gone through many iterations and has more to come—although I’m not sure when. I use it to tell me to take out the trash on Wednesday and for other quick temporary reminders. It is called up with the hotkey combination CRTL+WIN+R plus the most recent version has a Set Reminder option in the System Tray icon right-click menu.

In the classroom, it can remind the teacher when specific events must occur—such as a pop-quiz before the end of the period. The various versions of the Reminder files (some compiled into EXE files) have been included for people who are reading the book Digging Deeper into AutoHotkey. You can find those versions of the script in the Reminder.zip file.

Explanations of the Appointment Reminder script are incorporated in more detail in one section of the e-book Digging Deeper Into AutoHotkey and extended in Chapters Thirty-two and Thirty-three in the e-book AutoHotkey Applications. I encourage anyone to tailor the script for personal use—possibly adding multiple reminder capabilities and a repetitive reminder feature. I plan to do that myself when the time is right. (Maybe I should set up a reminder?)

Teach Programming with AutoHotkey

If you teach people to program, I would argue which language you use has less relevance than the techniques you teach. Learning to program is about learning how to think in a logical way while working with the tools common to most computer languages. For that reason, if I were instructing a class of budding system analysts, I would use AutoHotkey.

AutoHotkey includes all the tools (loops, if conditionals, etc.) that a programmer needs to use in any of the many programming languages. In fact, once you learn one language, you can quickly pick up another. It’s merely a matter of learning the syntax and technical differences. AutoHotkey includes virtually everything that you find in other languages, plus some simple extra tools (Hotkeys and Hotstrings) offering immediate results for students. In addition, the easy construction of GUIs (Graphical User Interfaces) makes building powerful pop-up apps easy.

An AutoHotkey student gets instant useful results. The importance of immediate outcomes can’t be overemphasized. Without them, demotivation soon follows. Most programming lessons start with the infamous “Hello, World!” routine. After that, the lessons become esoteric and metaphysical. Many may give up the journey while condemning programming as a boring occupation. However, when a student gets quick, practical results, the motivation level stays high.

Many more possibilities for AutoHotkey exist in education. Those mentioned here only represents my own minor endeavors. I have no doubt that creative teachers and professors will find numerous other ways to enrich the minds of their students with AutoHotkey on Windows computers.

jack

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

Why AutoHotkey for Poets?

Erstwhile Multifarious Poets Optated for Quill and Parchment. Forthwith, AutoHotkey Propounds the Furtherance of Lyrical Ruminations on Windows Computers.

Okay…I’m not a poet. My mind doesn’t work that way. But that doesn’t mean I can’t see how AutoHotkey might be useful to people who craft the English (or any other) language. Even so, I robotpoetryoccasionally enjoy writing a short rhyming couplet. (I know…constructing rhyming poems has become cliché—at least for real poets.)

In this blog, I offer a couple of AutoHotkey scripts for assisting and inspiring(?) budding wordsmiths. The first includes a set of over 500 Hotstrings for inserting “the most beautiful words in the English language.” The second script draws upon the Web to create a pop-up menu of rhymes. Even if you never intend to write a poem, you might find these AutoHotkey techniques interesting and/or useful. Continue reading

Why AutoHotkey for Engineers and Scientists?

While Writing AutoHotkey Scripts Should Be No Problem for Most Engineers and Scientist, Many Might Be Surprised by How Much the Free Language Offers in Windows Tools

I’m not sure how many people with technical backgrounds are familiar with AutoHotkey. My guess is that quite a few have never heard of the free open source robotpicartoonlanguage. Without a personal referral or ubiquitous marketing, free software such as AutoHotkey often goes overlooked for a long period of time. It’s not until individuals realize how much AutoHotkey can do for them that they start to explore the possibilities.

No software package does everything you want. That’s why adding little extras makes any program better. The beauty of AutoHotkey is that in addition to automating individual Windows programs, it can cross boundaries and add more features to any Windows software. Plus, it has the capability to create special pop-up apps for specific usages. The Windows utility building features in AutoHotkey can be especially helpful for anyone working in a technical field. Continue reading