Is there any resemblance of syntactic rules???

Get help with using AutoHotkey and its commands and hotkeys
RDCM
Posts: 1
Joined: 15 Feb 2019, 01:54

Is there any resemblance of syntactic rules???

15 Feb 2019, 02:26

I've spent like 2 hrs already trying to grasp this atrocious thing, but i can't seem to understand syntactic rules at effing all. R they even there in the first place???
Why the F in some places variables names should be wrapped with "%" while in others they should be typed as it is?

Code: Select all

tt := "Title"
MsgBox, 64, %tt%, blablabla ;<- without % it will assume "tt" is a string const
log := FileOpen(tt ".log", 0x101) ;<- here it'll work ONLY without %
Why the F some functions use traditional bracket syntax (e.g. "Func(param1, param2)") while others seem to work ONLY without brackets with commas:

Code: Select all

log := FileOpen(tt ".log", 0x101) ;<- seems legit, except variable without %
WinActivate(ahk_id %w%) ;<- surprise mf: "Call to nonexistent function" lol
WinActivate, ahk_id %w% ;<- works, but then again only with "%w%", not with "w"
Also i'm not sure wtf so much hassle with "ahk_id"/"ahk_class" bs, why not use classic window search once and then just passing returned handle? I mean ffs it's a system established in decades??

Code: Select all

hwnd := WndFind("Class","Title")
WndActivate(hwnd)
Oh, and also why the F single line If statement not working?

Code: Select all

	If(WinActive(ahk_id %w%)) return w ;<- screw u boi
;;;;;;;;;;;;;;;;;
	If(WinActive(ahk_id %w%)){ return w } ;<- still screw u
;;;;;;;;;;;;;;;;;
	If(WinActive(ahk_id %w%)){ return w ;<- naaah, not even close mf
	}
;;;;;;;;;;;;;;;;;
	If(WinActive(ahk_id %w%)){ ;<- yep, that's the ONLY way
		return w
	}
P.S. i've used at least couple dozens different programming and scripting languages in past 12 yrs or so, but i've never seen anything disgusting like that =\
User avatar
evilC
Posts: 4722
Joined: 27 Feb 2014, 12:30

Re: Is there any resemblance of syntactic rules???

15 Feb 2019, 07:00

Why the F some functions use traditional bracket syntax (e.g. "Func(param1, param2)") while others seem to work ONLY without brackets with commas:
ALL functions use IfWinActive(param, param) syntax. IfWinActive, param, param is a "Command", not a "Function".

This is because AHK is an amalgamation of two programming languages basically. The "old" syntax is Commands, the "new" syntax (AKA "Expression syntax") is functions.

Same deal with the variables. %var% is the old Command syntax, whereas var is new syntax.
If you want to force expression syntax for a command parameter, use % (There needs to be a space after the %), like so: Msgbox, % "MyVar = " myVar
To force expression syntax for an assignment, use := - myVar := "Foo"

Also note that there are two errors in this code: If(WinActive(ahk_id %w%)).

1) You are NOT using the contents of the variable w, you are using the contents of the variable whose name is stored in w.
Functions default to expression syntax - by using %var% when you are already in expression syntax you doing a "Double dereference" of the variable.
If w held "foo", and the variable foo held "Bar", then this statement is equivalent to If(WinActive(ahk_id "Bar")

2) ahk_id is a variable in your code, not a string - as mentioned above, functions default to expression syntax.

So the code should be written as: If(WinActive("ahk_id " w))
swagfag
Posts: 2778
Joined: 11 Jan 2017, 17:59

Re: Is there any resemblance of syntactic rules???

15 Feb 2019, 07:18

The reason is for historical reasons. A noble effort to retain backwards compatibility has led us to the cluster fuck that is the present day v1 syntax. If you wanna keep using v1, learn the handful of situations where only legacy syntax works and use expression syntax everywhere else
User avatar
jeeswg
Posts: 6568
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Is there any resemblance of syntactic rules???

15 Feb 2019, 09:04

- Thanks for this thread.

FUNCTIONS V. COMMANDS (AND %)
- 99% of the time, these are equivalent:

Code: Select all

Cmd, % VarName, % "LiteralString", ByRefVar, LiteralNum, % 1+1, % var1 var2
Func(VarName, "LiteralString", ByRefVar, LiteralNum, 1+1, var1 var2)
- The exception is mentioned here, the use of % %var% in commands:
'force an expression' and 'can be an expression' - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=14&t=37224
- In AHK v2 you can use functions for everything, and I backported these functions:
commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

AHK_ID/AHK_CLASS
- The 'hWnd' was deemed too hard for the casual user, but you can use it via "ahk_id " hWnd, and by itself in AHK v2.
- You can use DllCall to call Winapi functions.
- My general view is that AHK shines because it thinks about the casual user, yet also shines when it makes things straightforward for seasoned programmers.

SINGLE-LINE IF-STATEMENT
- I've said that this should be added in. The question is what syntax. Perhaps the smart detection of parentheses (but this could be buggy) and/or a 'then' keyword for a simpler safer implementation.
- Using the ternary operator with functions/expressions can achieve a lot, although this can't be used with return/break/continue.
- You could just have an operator added in, equivalent to a line separator (equivalent to carriage return/line feed), to bunch lines up.

FURTHER READING
Wish List 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=13&t=36789

CONCLUSION
- Before continuing, I should state that I'm just about ready to leave this forum.
- People rightly complain about the current AHK syntax, it can be 95% fixed just by backporting the AHK v2 functions and AHK v2 GUI objects, which I've done. Ultimately AHK v2 will fix everything.
- Once you've taken actions to negate the current problems, you realise that AHK actually has a lot of strengths compared to other languages, making it far easier to program with.
- What I find very difficult to understand is, as AHK v2 was taking longer and longer, why didn't people push for key, simple but important changes to be made to AHK v1. Maybe they thought that making small improvements to AHK v1 would delay the release of AHK v2, but this is naive, and in the end you get a flawed AHK v1 and a far-away-as-ever AHK v2, you get nothing.
- Btw I have managed to speed up getting various AHK v1/v2 problems fixed via my wish list, bug reports, encouraging people to add a '+1' for certain ideas and obviously the hard work of the dev, including features that people had argued for before but ineffectively. I.e. we've had a problem that people fail to state the need for a feature, or make the case/explain the idea poorly. E.g. force-local (absolutely crucial), A_ variables in #Include, ObjCount, new/backported A_ variables, Hotstring function, Send {Text}, see the changelogs for v.1.1.27 onwards:
Changes & New Features | AutoHotkey
https://autohotkey.com/docs/AHKL_ChangeLog.htm
- I've also written various bits of C++ code to fix things.
AutoHotkey C++ Powerhouse: Introduction - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=23&t=54394
- Some of the people here that like to comment on the AHK v2 development forum, are completely impractical, have no sense of forward-planning or prioritisation, have no sense of the user base, and just like to argue in an unnecessarily emotional/dramatic way but never do anything, and if they're so clever / so skilled in C++, why don't they ever propose any fixes? Worse, if people did act dramatically occasionally for *some* features, that would expedite their inclusion, but people have been dramatic about virtually everything.
- Another problem is users dropping poorly-explained random ideas all over the place, but not collecting the ideas in one place cf. my Wish List 2.0.
- Another problem is attacking and being condescending towards the main dev, I have one user in mind, (the situation is ludicrous when you consider the knowledge difference,) and if they don't start trying to 'play nice' with people, they could kill AHK v2, and may have already done so.
- So, I don't blame the dev, but I do blame many of the AHK v2 development forum participants for their inaction/counterproductive actions.
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”

Who is online

Users browsing this forum: aifritz, Dumitas, hasantr, ilhom, TheBeginner and 246 guests