/en/_posts/2011-11-11-Working-with-conditions.markdown

https://github.com/maul-esel/ahkbook · Markdown · 77 lines · 61 code · 16 blank · 0 comment · 0 complexity · f7ca763e10aa53ea5667521c3278d80a MD5 · raw file

  1. ---
  2. title: Working with conditions
  3. layout: post
  4. permalink: /en/Working-with-conditions.html
  5. ---
  6. # Working with conditions
  7. ## Conditions
  8. You will notice, it is often needed to do things only *if something is like this or that* or if it is not.
  9. This is common to all programming and scripting languages, and most of them use the word `if` to do that.
  10. ## if
  11. So in AutoHotkey. As you got to know previously, AutoHotkey has a so-called expressional and a traditional mode. This also applies to `if`.
  12. {% highlight ahk linenos %}
  13. If (A_OSVersion == "WIN_98")
  14. MsgBox Oh, your computer is pretty old.
  15. {% endhighlight %}
  16. The above is an example for the expressional way: the actual comparison must be enclosed in braces.
  17. {% highlight ahk linenos %}
  18. If A_IsAdmin = 1
  19. MsgBox This script is executed as administrator.
  20. {% endhighlight %}
  21. Now this is the traditional way: no braces, the left side is **always** a variable, the right side is taken as literal string (or number).
  22. As this is only a comparison for `true`/`false` (`1`/`0` in AutoHotkey), you can leave out the second part: `if A_IsAdmin` is enough. This also applies when using expressional mode: if you want, include `A_IsAdmin` in braces. You could also do a comparison to AutoHotkey's "constants" **true** and **false**.
  23. It is often recommended to use expressional mode instead of traditional, because it is the same as in other languages, it is considered a <cite>better style</cite>, and traditional is removed in AutoHotkey v2 (and AutoHotkey\_H v2).
  24. ### comparison (==, =, !=, ...)
  25. Of course you may sometimes want to do different comparisons. For example, the `=` operator compares **case-insensitive** in AutoHotkey (it doesn't care about lower- / uppercase). To do *case-sensitive comparison*, use the `==` operator instead.
  26. As you might guess, `!=` (or also `<>`) checks whether the left and the right side are **not equal**. Also you might prefix a variable (or a condition enclosed in braces) by `!` or `not`.
  27. AutoHotkey also supports `>`, `<`, `>=` and [a lot more]() ([AutoHotkey\_L]()).
  28. ### blocks
  29. To do more than one line based on a condition, enclose those lines in curly braces:
  30. {% highlight ahk linenos %}
  31. if (!A_IsAdmin)
  32. {
  33. MsgBox This script must be run as administrator.
  34. ExitApp ; closes the script
  35. }
  36. {% endhighlight %}
  37. ### More `if`
  38. AutoHotkey classic, AutoHotkey\_L and AutoHotkey\_H also support several "if-like" statements such as `IfEqual`, `IfLess`, `IfGreater`, `IfLessOrEqual`, `IfWinActive`, `IfWinExist`, `IfWinNotActive`, `IfWinNotExist`, `IfExist`, `IfInString`, ... . Using those is discouraged today as they are often considered "bad style" and they're about to be removed in AutoHotkey v2, AutoHotkey\_H v2 and IronAHK. All of them can be worked around by using the "normal" `if` plus a comparison.
  39. A special case is `IfMsgBox`, which can't be worked around in AutoHotkey classic, AutoHotkey\_L and AutoHotkey\_H. Still it will be replaced by something else in AutoHotkey v2, IronAHK and AutoHotkey\_H v2. We'll look at this further in the next chapter when we deal with the specifics of the `MsgBox` command.
  40. ## else
  41. As all other languages, AutoHotkey also has an `else` statement. It can be put beneath any `if`-Block, and it can also be combined with an extra condition:
  42. {% highlight ahk linenos %}
  43. if (A_IsAdmin)
  44. {
  45. MsgBox This script is run as admin.
  46. }
  47. else if (A_OSVersion == "WIN_VISTA")
  48. {
  49. MsgBox This script is not run as admin. You might check your UAC settings.
  50. }
  51. else
  52. MsgBox This script is not run as admin.
  53. {% endhighlight %}
  54. You can also combine `else` with those special if-statements mentioned above.
  55. ## Combining conditions
  56. Also, you might sometimes want to check for multiple conditions: if both, at least one, or none of them applies.
  57. This is only supported in expression mode: combine the conditions with the word `and` (or `&&`) to check for both, and with `or` (or `||`) to check for at least one.
  58. {% highlight ahk linenos %}
  59. if (A_IsAdmin AND A_Year > 2011)
  60. MsgBox This script is run as administrator in 2012 or later.
  61. {% endhighlight %}