Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Tutorial for Newbies


  • This topic is locked This topic is locked
1 reply to this topic
jaco0646
  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006
Tutorial for Newbies

As a person who started using AHK having absolutely zero programming experience (never taken a class or read a book on computers in my life), I will give you my 2 cents on learning it.

I think exploring the forum works great if you have even a little bit of background in this area. If, like me, you have none, I would learn it in the following order (this is how I would do it if I had to do it over again). :p My reasoning here is that for a person starting from scratch, you need to learn concepts before learning commands, and the forum is geared more towards commands.

First read the Tutorial, the Scripts page, and then the FAQ. Afterwards, I would recommend learning concepts in the following order:[*:3bu072xf]Hotstrings
[*:3bu072xf]Hotkeys
[*:3bu072xf]Mouse Clicks & Mouse HotKeys
[*:3bu072xf]Variables
[*:3bu072xf]Expressions
[*:3bu072xf]Loops - especially Parsing Loops
[*:3bu072xf]Strings - (e.g. StringMid, StringReplace)
[*:3bu072xf]If,Else statements
[*:3bu072xf]Arrays & Dynamic variables - (StringSplit)
[*:3bu072xf]GoSub & Functions
[*:3bu072xf]GUIs
[*:3bu072xf]Windows Messages
[*:3bu072xf]DllCallThe first 3 you will not necessarily use very often once you become proficient with the program; but they are the easiest to comprehend and implement. Everything from Variables through GUIs is what I would consider the "core" of the program that you will use repeatedly to write virtually all of your scripts with. Windows Messages and DLLCalls are the advanced techniques that are the most powerful but most complicated. The average user will probably never have the need and desire to master these two.
Of course this list is not set in stone, and you may be learning multiple items at once (for example you may want to scratch the surface of GUIs right away), but I think all of the items are important to learn about if you plan to use AHK more than a few times, and I tried to arrange them in an order that would be most logical and systematic.

When you're wondering how to accomplish a specific task in AHK, remember that most of the commands have a common naming scheme to identify what they do. For example:

Commands dealing with files start with File.
Commands dealing with windows start with Win.
Commands dealing with text start with String.

Similarly:

Commands that retrieve data from somewhere contain Get.
Commands that apply data to somewhere contain Set.

Combining the two previous structures, we have commands like:

FileGetSize - Retrieve the size of a file.
WinGetTitle - Retrieve the title of a window.
StringGetPos - Retrieve the position of a piece of text inside a larger piece (like a sentence in a paragraph).

FileSetTime - Apply a new Time Stamp to a file.
WinSetTitle - Apply a new title to a window.
SetTimer - Apply a new Timer, which is a command or group of commands that run at timed intervals.


Good Luck, and remember that nobody was born knowing any of this. It takes time.

jaco0646
  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006
Syntax for Assignment and Comparison in AutoHotkey

Commands vs Expressions


Assignment:

Command assignment =
Everything to the right of the = sign is considered literal text.
Variables to the right must be enclosed in percent signs. %MyVariable%
The = sign cannot perform calculations.

Command assignments are never necessary. They are merely convenient for assigning literal text because double quotes can then be omitted.

Expression assignment :=
Everything to the right of the := operator is considered an expression.
Literal text must be enclosed in double quotes. "literal text"
Expressions can perform calculations.

You may intersperse literal text with variables and calculations using the := operator.

For example:
nam := "Expression 1:" 
var := nam "two plus two equals" 2+2 "." 
MsgBox, %var%
The contents of %var% will be: Expression 1:two plus two equals4.

Note that since I did not add enough spaces into the literal text or use the A_Space variable in the expression, the output is not spaced correctly.

Note also that using the = sign to create the nam variable would've been easier since I could've omitted the double quotes then.


Comparison:

Command comparison If var = value
Everything to the right of the comparison sign is considered literal text.
Variables to the right must be enclosed in percent signs. %MyVariable%
Command If statements cannot perform calculations.

Command comparisons are never necessary. They are merely convenient for comparing literal text because double quotes can then be omitted.

For example:
If var = literal string
If var = 4
If var = %MyVariable%
If var = ;<--compare to blank value
If var ;<--boolean
Wrong: If var = 2+2

Expression comparison If (var = "value")
Everything to the right of If is considered an expression.
Literal text must be enclosed in double quotes. "literal text"
Expressions can perform calculations.

For example:
If (var = "literal string")
If (var = "4")
If (var = MyVariable)
If (var = "") ;<--compare to blank value
If (var) ;<--boolean
If (var = 2+2)

Note that numbers will not be mistaken for variables even if double quotes are omitted, so the following works fine:
If (var = 4)
For this reason, it is usually unwise to use numbers as variable names.