Clarifications on comments in Scripts

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nigelle
Posts: 10
Joined: 23 Oct 2013, 09:58
Location: France

Clarifications on comments in Scripts

28 Jul 2017, 11:13

In the help I have found :

1) "Scripts can be commented by using a semicolon at the beginning of a line. For example:
; This entire line is a comment. "
When the line is indented does "the beginning of a line" mean the first non blank character not taking into account tab or space ?

2) "Comments may also be added to the end of a command, in which case the semicolon must have at least one space or tab to its left. For example:
Run Notepad ; This is a comment on the same line as a command. "
What is the definition of "command" ? Does this mean any "entry" in "Alphabetical Command and Function Index" of the help ? this is broader and include true commands, functions, #strings and block object array delimiters ?
e.g. for a block
{ ; comment
...
} ; comment

3) Storing values in variables are missing in the list
e.g.
CopyOfVar = %Var% ; With the = operator, percent signs are required to retrieve a variable's contents.

4) "In addition, the /* and */ symbols can be used to comment out an entire section, but only if the symbols appear at the beginning of a line as in this example:
/*
MsgBox, This line is commented out (disabled).
MsgBox, This one too.
*/ "
Same question as in 1)
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Clarifications on comments in Scripts

28 Jul 2017, 11:38

When the line is indented does "the beginning of a line" mean the first non blank character not taking into account tab or space ?
Yes. AHK will, under most circumstances (I don't feel confident in saying all circumstances), I think there are some exceptions) ignore the whitespace at the beginning or end of a line - AHK leading whitespace or trailing whitespace.
What is the definition of "command" ?
While command would be something like Run, WinWait, MsgBox, etc. per the documentation, you can add comments on lines that are doing maths like var:=8+2. Whether assigning a variable a value is technically a command or not may be a matter of semantics. My experience with writing comments in AHK is if you type ; (note the preceeding whitespace) then anything after that on any line is going to be ignored by AHK.

I'm not sure if your point/question 3 is really a question to be answered.
/* ...
Yes, you can have whitespace preceding these characters and they will still function. Like in 1, that leading whitespace is just ignored by AHK.
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Re: Clarifications on comments in Scripts

28 Jul 2017, 18:51

Using sCiTe4AutoHotkey can be helpful in visually understanding the comments.

See an example I made;

https://gyazo.com/9601dac59956dd36a9cce49fce22aaaa
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Clarifications on comments in Scripts

29 Jul 2017, 03:29

/* will work for single lines too */ ... beside the fact that you can set your own comment flag: #CommentFlag
Last edited by BoBo on 29 Jul 2017, 08:05, edited 1 time in total.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Clarifications on comments in Scripts

29 Jul 2017, 04:21

@BoBo

That doesn't work for single lines.

Code: Select all

!3::
/* Testing */
MsgBox Hi
*/
MsgBox Bye
return
If it did, then MsgBox Hi would be executed. But it is actually treated as part of the comment.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Clarifications on comments in Scripts

29 Jul 2017, 06:04

To explain semicolons and comments, I would say this:
- if the line starts with a semicolon, the whole line is a comment
- if in a line a space/tab is followed by a semicolon, everything from that point in the line onwards is a comment
- this applies everywhere except for continuation sections (see example code below)

- the simple logic of this makes it very easy to remove comments from a script, or to distinguish between code and comments if you want to do some parsing (e.g. converting a script from AHK v1 to AHK v2)

Code: Select all

#IfWinActive, ahk_class Notepad ;COMMENT
q:: ;COMMENT you can comment everywhere
;COMMENT
 ;COMMENT
MsgBox, % "hello world" ;COMMENT
vText := StrReplace("hello", "l", "", vCount) ;COMMENT
MsgBox, % vText " " vCount ;COMMENT
Loop, 3 ;COMMENT
{ ;COMMENT
	var++ ;COMMENT
} ;COMMENT
var += 5 ;COMMENT
MsgBox, % var ;COMMENT
vText := "abcde" ;COMMENT
return ;COMMENT
#IfWinActive ;COMMENT

;==================================================

w:: ;continuation sections and comments
vText := "
(
abcde ;this is not comment
abcde ;this is not comment
abcde ;this is not comment
)"
MsgBox, % "[" vText "]"

vText := "
(Comments
abcde ;this is a comment
abcde ;this is a comment
abcde ;this is a comment
)"
MsgBox, % "[" vText "]"
return
==================================================

Some other points:
- to have a literal space/tab and a semicolon, escape the semicolon with a backtick
- to have a semicolon hotkey, escape the semicolon with a backtick

Code: Select all

q:: ;escaping semicolons, to have a literal space/tab and a semicolon
;the line below would be invalid, 'Missing close-quote' error:
;vText := "a ; b ; c"

;the line is OK if we escape the semicolons:
vText := "a `; b `; c"

MsgBox, % vText
return

;==================================================

;because a semicolon at the start of a line
;is interpreted as a comment,
;to do a semicolon hotkey, we escape the semicolon:

`;:: ;a semicolon hotkey
MsgBox, % A_ThisHotkey
return
In this post is everything I think is worth saying about semicolon comments in AutoHotkey, unless someone can find anything I've missed!? Cheers.

==================================================

'if 0' and '#If 0' are handy for temporarily commenting things out:

Code: Select all

#If 0
q::
MsgBox, % A_ThisHotkey
return
#If

;==================================================

w::
if 0
{
	MsgBox, % "a"
	MsgBox, % "b"
	MsgBox, % "c"
}
MsgBox, % "d"
return
Well, that's it for my comments on comments.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AdsBot [Google], Bing [Bot], Google [Bot], Spawnova and 98 guests