AutoHotkey Switch-Case

Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
EE FELLOW 2017 — first ever recipient of Fellow award
MVE 2015,2016,2018
CERTIFIED GOLD EXPERT
DISTINGUISHED EXPERT
Published:
Updated:
Edited by: Andrew Leniart
Many programming languages have a Switch–Case capability. Since its first beta in 2003, AutoHotkey has not had one. Finally, in the recent 28-Sep-2019 release of Version 1.1.31.00, it now has Switch–Case. This article discusses it and shows a sample usage as applied to a previous post here at EE.


First, let me start by saying that this is not an article to learn about the concept of Switch–Case in programming. This article assumes that you have programming/scripting experience and are a user of the AutoHotkey language or, at the very least, interested in it. If you are new to AutoHotkey, my Experts Exchange article will get you started with it:

AutoHotkey - Getting Started

Whether you are new to AutoHotkey or not, this article presumes that you have some knowledge of Switch–Case in other programming/scripting languages, such as C# and Java:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch
https://www.w3schools.com/java/java_switch.asp

If those links ever go bad, a web search for "switch case c#" and "switch case java" will surely find them, along with similar searches for other languages.


As mentioned in the article summary above, AutoHotkey has been around for 15+ years (following its fork from 
AutoIt), but has not had a Switch–Case capability until the v1.1.31.00 release a few days ago. The implementation is similar to other languages and is well-documented at the  online documentation for it (and in the CHM help file that comes with the product). Any programmer should feel very comfortable with its syntax and semantics, although it does have its idiosyncrasies:


• An already existing AutoHotkey command, StringCaseSense, controls the case-sensitivity of string comparisons performed by the new Switch command.


• Each Case statement is limited to 20 values. In practical terms, this is not a serious limitation.


• There is no EndCase statement, no Break statement, and no implicit fall-through. Each set of Case statements ends implicitly at the next Case (or at the Default statement or the closing brace of the Switch itself).

• The Default statement is not required to be last in the sequence.

For more details on its operation, see the  online doc at the AutoHotkey site . But before ending this article, I'm going to show a sample usage based on this previous post here at Experts Exchange:


https://www.experts-exchange.com/questions/29157696/AutoHotkey-send-block-comment-based-on-activewindows-process-name.html#a42939541

Following is a rewrite of the If–Else portion of that code using the new Switch–Case statements:


WinGet,Process,ProcessName,A
Switch Process
{
  Case "vbsedit.exe":
    CommentBlock:="' *************************************************************************`n"
    . "' Author:`n"
    . "' Creation date:`n"
    . "' Description:`n"
    . "' ***************************************************************************`n"
  Case "notepad++.exe":
    CommentBlock:="Rem ======================================================================`n"
    . "Rem Author:`n"
    . "Rem Creation date:`n"
    . "Rem Description:`n"
    . "Rem ======================================================================`n"
  Case "powershell_ise.exe":
    CommentBlock:="# ===========================================================================`n"
    . "# Author:`n"
    . "# Creation date:`n"
    . "# Description:`n"
    . "# ===========================================================================`n"
  Case "scite.exe":
    CommentBlock:="; ===========================================================================`n"
    . "; Author:`n"
    . "; Creation date:`n"
    . "; Description:`n"
    . "; ===========================================================================`n"
  Case "ssms.exe":
    CommentBlock:="-- ===========================================================================`n"
    . "-- Author:`n"
    . "-- Creation date:`n"
    . "-- Description:`n"
    . "-- ===========================================================================`n"
  Default:
    MsgBox,4144,Error,Active window is not a supported program
    Return
}
I've heard some folks in the AutoHotkey community refer to the new Switch–Case as "syntax sugar" — a somewhat disparaging, albeit accurate characterization, in that the same logic may be achieved with an If–Else series and a fall-through Else. But as syntax sugar goes, I find it to be a sweet improvement. :) 

Article Update 13-Oct-2019: As stated in the article, Version 1.1.31.00 was the first release with the Switch–Case capability. As often happens with software, initial versions have bugs, and this occurred with Switch–Case in v1.1.31.00, as described here:

https://www.autohotkey.com/boards/viewtopic.php?p=296401#p296401
 
Thus, if you downloaded and installed v1.1.31.00 after reading this article, please download and install v1.1.31.01, which has these fixes for Switch–Case (copied here under "Fair Use"):
 
    Fixed Switch %v%, Case %v% and Throw %v%.
    Fixed Case 2,,: to show an error message rather than crash.
 
As always, the latest version may be downloaded at the AutoHotkey website.
End Article Update 
 

Obviously, this is not a "deep" technical article. My purpose in writing it is simply to make my fellow EE members and AutoHotkey coders aware of this new feature, one that has been a decade and a half in the making.


If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe

0
6,092 Views
Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
EE FELLOW 2017 — first ever recipient of Fellow award
MVE 2015,2016,2018
CERTIFIED GOLD EXPERT
DISTINGUISHED EXPERT

Comments (2)

Luis DiazIT consultant

Commented:
Very useful article! Glad to know that an alternative to if-then through switch-case statement is available in AutoHotkey!
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
Hi Luis,
Thanks for the compliment and the article endorsement...both very much appreciated! Regards, Joe

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.