Autohotkey "Immediate Window"

Get help with using AutoHotkey and its commands and hotkeys
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Autohotkey "Immediate Window"

30 Dec 2016, 12:57

One of the great benefactors of working with Languages like VBA is the Immediate Window. For those who don't know, the VBA Immediate Window is like a console window but for VBA code. For instance, below is a snapshot of the VBA IDE:

Image

In the Immediate Window I can test procedures and functions. For example if I typed the following into the immediate window:

Code: Select all

?CustomRound(0.49)
The value returned by the CustomRound() function (0 in this case) will be printed to the Immediate Window after the command is executed. As a second example take the following code:

Code: Select all

a=2
b=3
c=" cats "
Msgbox a & c & "+ " & b & c & "= " & (a+b) & c
Execute each line at a time (press {Enter} while the cursor is on the line). When executing the final line a message box will pop up with the message "2 cats + 3 cats = 5 cats ". This is important because it shows the importance of "local variables" in the immediate window.

The immediate window is great for prototyping code and testing concepts before you commit to writing a full script. Recently I've been thinking about making one of these for AHK, because I am sure it will help me out with writing AHK code and I'm certain it will help others also. The question is - What would be the best way to implement it?

The window

I'm not too experienced with making good GUIs in AHK and I'm not even sure if such a immediate window is possible (and extendable) in a default AHK GUI. For this reason I plan to avoid AHK's default GUIs and instead use HTML to build the GUI. I will use Ace Editor for the ability to write commands and Exo.AHK. Such an editor will be hackable and extendable which suites my own needs and the needs of the community, I believe.

Command Evaluation

This is where it gets complicated... I'm not entirely sure how this is going to work but here's the general overview that I've invented so far:

Saving Variables

As shown in VBA example 2, local variables are important and should be saved. However, as any basic user of AHK knows, all variables in AHK are temporary. However we want to emulate "permanent variable declaration". To accomplish this, for every command string executed from the immediate window we will use a Regular Expression, such as "^\s*(VARIABLE):{0,1}=.*", to detect where variables have been set.

If a variable has been set we will push the code to a collection as follows: Constants.Push([Match1,Match])

Evaluating Code

If a variable has not been set, determined by the RegEx, then:
  • 1. A new GUID is created (used below where {GUID} is found).
    2. Includes are added (see next section).
    3. All variable declarations will be written to a file named Code_{GUID}.AHK.
    4. The code to be evaluated is then also written to Code_{GUID}.AHK.
    5. Extra-code added to return variable information to Main console window (see next sections).
    6. File instructed to self-delete when finished executing followed by ExitApp.
    7. File executed with Autohotkey.exe.
Includes

Inclusions are important as they allow the declaration of modules and often users will want to test functions of modules rather than single lines of code. To include inclusions into the immediate window we will use a similar method to variable declarations. We will first use RegEx to detect an inclusion: ^\s*(Include\s*.*) and if a match is found we will push the code to a collection as follows: Includes.Push(Match1)

ByRef Functions

A common technique in AHK programming is to use ByRef function parameters. For example:

Code: Select all

SayHello(ByRef Greeting, name){
  Greeting := "Hello " . name . "!"
}
In the immediate window, if we executed:

Code: Select all

Name=Karl
Greeting= ;Required to add Greeting to variable collection.
SayHello(Greeting,Name)
MsgBox, %Greeting%
We would expect a Message box saying "Hello Karl!". However this wouldn't be the case because the Greeting variable will never be set using the outcome of the SayHello Function. There are 2 work arounds that I can think of for this:
  • 1. A section is added to the end of every execution checking for a change in variable value. If a change is found the change is passed back to the original script
    2. When a file is included with #Include it can be searched for functions with ByRef arguments. Then if such a function is executed a callback can be added to the script, or the executed code could be added to the variables collection (as it directly effects another variable).
COM Objects

If I execute XL := comObjCreate("excel.application") a new instance of Excel will be created. However in later uses of XL I will need to use the active COM object rather than creating new ones. For this reason, when creating COM Objects the variable declaration needs to be changed from ^\s*(VARIABLE):=comObjCreate(.*) to "%Match1% := ComObjActive%Match2%". However I am unsure how to get the correct COM Object using comObjActive() since, iirc, the function will pick the first available COM Object... It would be better to get it using some reference to it...

On the flip side I'm not sure if COM Objects such as "VBScript.RegExp" stick around after they are created... Something I will have to look into later.

Am I overthinking this?

Or is this what you would do also? When I start writing code, I will add it to a GITHub repo, so anyone can fork it / develop it further.

Also, a function such as Eval() Might be useful for this project, however I'm not sure whether Finc's library still works in the latest versions of AHK.
User avatar
evilC
Posts: 4787
Joined: 27 Feb 2014, 12:30

Re: Autohotkey "Immediate Window"

30 Dec 2016, 15:12

What editor do you use for AHK? ACE? By not using an editor with a debugger, you are seriously missing out ;)
I don't know of any immediate window support in any IDEs/Editors for AHK, but the latest Lexikos fork of SciTe4Autohotkey (See my post in the Scite4AHK thread) has a rather excellent variable browser.

Also, I know that for example with AHK_H supports dynamic code execution via the AHKExec command (Which is basically an eval() command). I would imagine this may be possible using vanilla AHK, but I would not know how. I would imagine that something like this would be required to implement an immediate window. AhkExec also executes in the scope of the current script, so includes etc would not be an issue.

Hope this helps
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Autohotkey "Immediate Window"

30 Dec 2016, 19:28

evilC wrote:What editor do you use for AHK? ACE?
I use Atom because it's multi-OS friendly and I use Mac OS X mostly. Soon my windows PC will be delivered though and I can start testing the stuff I've made :) Syntax highlighting is all I really need to write code. It's just testing it which becomes a problem.
I don't know of any immediate window support in any IDEs/Editors for AHK.
This exactly why I planned to make my own.
but the latest Lexikos fork of SciTe4Autohotkey has a rather excellent variable browser.
Variable browsing is something an immediate window would allow (to an extent), a variable browser doesn't allow dynamic userfriendly evaluation for testing coding concepts, the main motivation for the Immediate Window. Variable browsing is just a side effect of the Immediate window.
AhkExec also executes in the scope of the current script, so includes etc would not be an issue.
Interesting! That would solve... most issues I think. Does that include dynamic includes? As in can you inject "Include ..." into AHK_H?

The only potential issue may be rewriting exo for AHK_H... Though I'm not sure what that'd entail if anything...
guest3456
Posts: 3150
Joined: 09 Oct 2013, 10:31

Re: Autohotkey "Immediate Window"

30 Dec 2016, 23:28

sounds like you're asking for a REPL
https://en.wikipedia.org/wiki/Read%E2%8 ... print_loop

Pulover has a more recent Eval() func for AHK, you may check that instead of fincs

you could also look at the ExecScript() example at the bottom of the Run help page:
https://autohotkey.com/docs/commands/Run.htm

otherwise use AHK_H

lexikos
Posts: 7088
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Autohotkey "Immediate Window"

01 Jan 2017, 17:50

sancarn wrote:I'm not even sure if such a immediate window is possible (and extendable) in a default AHK GUI.
What does the VBA Immediate window do that you can't do with a standard Edit control?

The power of the Immediate window comes entirely from the ability to eval code, not anything special that the GUI does.
However, as any basic user of AHK knows, all variables in AHK are temporary.
Everything in this world is temporary.

All variables in AutoHotkey exist from the moment they are created (the moment the script starts, unless they are dynamic variables) to the moment the script exits. It can't get any more permanent than that without existing outside the script's process.

Local variables are "permanent" as well, and that is why you can (but shouldn't!) use them even without calling the function.

Code: Select all

#Persistent
SetTimer MyLbl, -500
MyFn() {
    MyLbl:
    localvar := 123
    ListVars ; Doesn't show localvar, because it's local.
    MsgBox % localvar
    ExitApp
}
They are just freed if and when the function returns, in the same sense as localvar := "".

Anyway, you are overthinking it. Why don't you just use global variables?

In the VBA "IDE", I think you can stop at a breakpoint (or step into the code) and use the Immediate window to execute code in the context of the current line. You won't be able to achieve this with AutoHotkey, but at least the Immediate window could use the script's actual global variables.
guest3456 wrote:you could also look at the ExecScript() example at the bottom of the Run help page
I think that the Immediate window would be much less useful if it executed in the context of a newly started process (as for ExecScript), rather than in the context of a script which was already running.
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Autohotkey "Immediate Window"

02 Jan 2017, 17:26

lexikos wrote:
sancarn wrote:I'm not even sure if such a immediate window is possible (and extendable) in a default AHK GUI.
What does the VBA Immediate window do that you can't do with a standard Edit control?
The power of the Immediate window comes entirely from the ability to eval code, not anything special that the GUI does.
This is likely mostly due to my ignorance. The immediate window is a multiline edit control, such a thing can be created with AHK, however the line that is executed relies on the position of the cursor/caret.
I know you can get the text contained in the control fairly easily, but can you get the cursor position in the control?

One of the pros of using IE (HTML) though is the extensability aspect. Ability to add buttons (via external applications), code snippets, syntax highlighting etc. Most of which would be more difficult in AHK, I'd assume.
Everything in this world is temporary.
Temporary was the wrong word there :P
All variables in AutoHotkey exist from the moment they are created (the moment the script starts, unless they are dynamic variables) to the moment the script exits. It can't get any more permanent than that without existing outside the script's process.
Indeed. That's rather what I meant.
Local variables are "permanent" as well, and that is why you can (but shouldn't!) use them even without calling the function.

Code: Select all

#Persistent
SetTimer MyLbl, -500
MyFn() {
    MyLbl:
    localvar := 123
    ListVars ; Doesn't show localvar, because it's local.
    MsgBox % localvar
    ExitApp
}
They are just freed if and when the function returns, in the same sense as localvar := "".
Never knew you could do that O_o Not entirely sure why you would ever want to do that though...
Anyway, you are overthinking it. Why don't you just use global variables?
How does one use a global variable which can be shared by multiple instances of AHK? As in, with the following script executed from the immediate window:

Code: Select all

a:=3
Msgbox, %a%
Assuming I will be running the 2 lines in 2 seperate ahk scripts, how does one make sure that script 2 knows about variable a?
In the VBA "IDE", I think you can stop at a breakpoint (or step into the code) and use the Immediate window to execute code in the context of the current line. You won't be able to achieve this with AutoHotkey, but at least the Immediate window could use the script's actual global variables.
Indeed. It is a shame you can't go through an AHK script step by step, but... What can you do? Use msgbox I guess... :P Using your variable explorer with msgbox will do the trick there :)
guest3456 wrote:you could also look at the ExecScript() example at the bottom of the Run help page
I think that the Immediate window would be much less useful if it executed in the context of a newly started process (as for ExecScript), rather than in the context of a script which was already running.
Okay that's probably the confusion with the above also. How does one execute code in the context of the current script? AHK_H?
guest3456
Posts: 3150
Joined: 09 Oct 2013, 10:31

Re: Autohotkey "Immediate Window"

02 Jan 2017, 19:36

sancarn wrote:How does one execute code in the context of the current script? AHK_H?

here you go, there's lot of ways to do this in AHK_H, HotKeyIt could tell you if it could be done better

but this works:

Code: Select all

ahkdllthread := AhkThread()

Gui, Margin, 20, 20
Gui, Font, s11, Courier New
Gui, Add, Edit, r20 w240 section vCode, input code here
Gui, Font, s11, Verdana
Gui, Add, Button,, Execute code
Gui, Font, s11, Courier New
Gui, Add, Edit, r20 w300 ys readonly vREPL,
Gui, Font, s11, Verdana
Gui, Add, Text, w200 vStatus, Ready...
Gui, Show, w600 h420, AHK REPL
return

ButtonExecuteCode:
   Gui, Submit, NoHide
   if ahkdllthread.addScript(code, 2)
   {
      GuiControl,, REPL, % REPL "`n" code
      GuiControl,, Status, Code added.
      SetTimer, ClearStatus, -1500
      GuiControl,, Code,
   }
   else
      GuiControl,, Status, Error adding code`,retry
return

ClearStatus:
   GuiControl,, Status, Ready...
return
and here are your examples:

https://imgur.com/I9GyfFj

Helgef
Posts: 4467
Joined: 17 Jul 2016, 01:02
Contact:

Re: Autohotkey "Immediate Window"

03 Jan 2017, 03:21

guest3456 wrote: but this works:
:clap: Very nice indeed!
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Autohotkey "Immediate Window"

05 Jan 2017, 08:15

guest3456 wrote:
sancarn wrote:How does one execute code in the context of the current script? AHK_H?
here you go, there's lot of ways to do this in AHK_H,
Yeah so AHK_H :P
and here are your examples:

https://imgur.com/I9GyfFj
Jeez and it's already working to an extent! That's brilliant!
This will be an excellent basis!

Return to “Ask For Help”

Who is online

Users browsing this forum: dokimi, Google [Bot], RayB, starionx, TAC109 and 34 guests