Trying AHK2 for the first time Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Trying AHK2 for the first time

Post by WalkerOfTheDay » 08 Mar 2023, 09:45

So I decided to finally try out AHK v2 after using v1 for years.
I tried to convert an existing script, and am getting the following error:

Error: This variable has not been assigned a value.

Specifically: local Monitor (same name as a global)

011: If GetKeyState("Control")
012: {
▶ 013: If (Monitor = "gaming")
014: {
015: run("c:\controlmymonitor\ControlMyMonitor.exe /SetValue Primary 60 17")


I noticed by adding Global above the line: if GetKeyState("Control"), it executes
the script, but the [else] is never executed. Any ideas why ??

Code: Select all

#Requires AutoHotkey v2
#MaxThreadsPerHotkey 2
Persistent
Monitor := "gaming"

^g::
~LButton & RButton::
{
    if GetKeyState("Control")
        {
            if (Monitor = "gaming")
                {
                    run "c:\controlmymonitor\ControlMyMonitor.exe /SetValue Primary 60 17"	; switch to work pc
                    Monitor := "work"
                }
        }
        else
            {
                run "c:\controlmymonitor\ControlMyMonitor.exe /SetValue Primary 60  1"		; switch to gaming pc
                Monitor := "gaming"   
            }
    return
}

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time  Topic is solved

Post by boiler » 08 Mar 2023, 10:06

Hotkey routines are functions in v2, so you need to be concerned with variable scope. If you want to refer to the global variable, you need to declare it as global. Otherwise, it thinks you mean to use it as a local variable, in which case you should initialize its value, but that’s not what you’re trying to do here. If you only were referring to the variable and not assigning it a value in your function, it probably would have automatically determined you were referring to the global variable, but not since you are assigning it a value.

See Local and Global Variables for further explanation.

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Trying AHK2 for the first time

Post by mikeyww » 08 Mar 2023, 10:31

Also: instead of Run, display a MsgBox for each condition. You can just display the value of the condition itself. If the Else condition is not met, then the If condition is met. You can then backtrack & determine why.

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 09 Mar 2023, 09:07

@boiler Thanks ! That makes sense.
@mikeyww Thanks, the run was not for troubleshooting though, otherwise I would have added the MsgBox for sure, thanks.

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 10 Mar 2023, 06:54

Okay, with another script I'm getting a similar error. What is the correct way in AHKv2 to send a variable ?
The last line

Code: Select all

SendRaw Artno
is throwing below error, even though I have assigned a value to the variable afaik

Error:
Warning: This variable appears to never be assigned a value.

Specifically: local SendRaw

025: Sleep(4000)
026: Send("^f")
▶ 027: SendRaw(Artno)
028: }
029: Exit

For more details, read the documentation for #Warn.

Code: Select all

#Requires Autohotkey v2.0

^1::
{
    A_Clipboard := ""                                                                   ; empty clipboard
    WinActivate("Cadiz + 2.5% - Excel")
    Sleep(1000)
    Send "^c"                                                                           ; copy articlenumber
    Sleep(1000)
    ClipWait
    Artno := StrReplace(A_Clipboard, "`r`n")
    A_Clipboard := ""                                                                   ; empty clipboard
    Send "{Right 5}"
    Sleep(1000)
    Send "^c"                                                                           ; copy listprice
    ClipWait
    Sleep (1000)
    ListPrice := StrReplace(A_Clipboard, "`r`n")
    ToolTip(Artno . " " . ListPrice)
    WinActivate("Session - c19708up.cloudiax.com - Verbinding met extern bureaublad")
    Sleep(1000)
    Send "!na"
    Sleep(4000)
    Send "{F6}"                                                                         ; open article data
    Sleep(4000)
    Send "^f"                                                                           ; find
    SendRaw Artno
}

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Trying AHK2 for the first time

Post by mikeyww » 10 Mar 2023, 06:59

Hello,

SendRaw is not a v2 function.

Explained: Raw mode

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 10 Mar 2023, 07:04

And note for the next time you encounter something similar: The error message tells you that it’s SendRaw that it has an issue with, not Artno.

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 10 Mar 2023, 07:04

mikeyww wrote:
10 Mar 2023, 06:59
Hello,

SendRaw is not a v2 function.

Explained: Raw mode
Ah, my bad :headwall:

I guess it's gonna take some time for me to adapt to the new version :D

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 10 Mar 2023, 07:08

It would help to get familiar with the v2 Function List, or at least reference it often.

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 10 Mar 2023, 07:17

boiler wrote:
10 Mar 2023, 07:08
It would help to get familiar with the v2 Function List, or at least reference it often.
Thanks @boiler :thumbup:

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: Trying AHK2 for the first time

Post by RaptorX » 10 Mar 2023, 07:27

If you already know v1, I would definitely suggest reading the Changes from v1.1 to v2.0
section of the help file, it will save you hours of frustration.
Projects:
AHK-ToolKit

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 16 Mar 2023, 10:02

I'm not sure if I should open a new topic for this, but I guess I could also post it here.
It's a question regarding ImageSearch, specifically the variations option.

I have this working piece of an imagesearch:

Code: Select all

if ImageSearch(&OVarX, &OVarY, 0, 0, A_ScreenWidth, A_ScreenHeight, "*5 C:\AHK-Studio-master\Projects\AHKv2\Bol_Images\selecteer.png")
This works fine, but I would rather not use the full path, so I tried:

Code: Select all

if ImageSearch(&OVarX, &OVarY, 0, 0, A_ScreenWidth, A_ScreenHeight, "*5" A_WorkingDir "\Bol_Images\selecteer.png")
This throws an error about parameter 7 being invalid.

I tried it without the quotes around *5, but that also doesn't work.

If I remove the "*5" al together it works, but I really would keep the variations option in there.

What is the correct syntax ?

Full script:

Code: Select all

#Requires Autohotkey v2.0
SetWorkingDir "C:\AHK-Studio-master\Projects\AHKv2\"
^1::
{
    CoordMode "Pixel"
    ;if ImageSearch(&OVarX, &OVarY, 0, 0, A_ScreenWidth, A_ScreenHeight, "*5 C:\AHK-Studio-master\Projects\AHKv2\Bol_Images\selecteer.png")
    if ImageSearch(&OVarX, &OVarY, 0, 0, A_ScreenWidth, A_ScreenHeight, "*5" A_WorkingDir "\Bol_Images\selecteer.png")
    {
        Click OVarX, OVarY
        Sleep("250")
        MouseClick "Left", 0, 160, 1, 10,, "R"
        
    }
    else
        MsgBox "Icon could not be found on the screen."
}

ESC::ExitApp

gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Trying AHK2 for the first time

Post by gregster » 16 Mar 2023, 10:08

I would guess: put a space behind "*5": "*5 " - that means, actually include it in the expression.
Imagesearch docs wrote: Options: Zero or more of the following strings may be also be present immediately before the name of the file. Separate each option from the next with a single space or tab.
And yes, I would suggest to create a separate topic, next time - or if you have more questions about this.

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 16 Mar 2023, 10:11

@gregster Yes ! That's it. That simple... Like I said before, it's gonna take some time getting used to v2 :mrgreen:

gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Trying AHK2 for the first time

Post by gregster » 16 Mar 2023, 10:12

Sure, but expressions worked the same in v1 (so there are similarities to discover). ;) Of course, they are the default now.

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 16 Mar 2023, 10:54

To that point, just envision (or use a MsgBox to display) exactly what your concatenated string should look like, and then you would see that the *5 would have been butted up right against your file path. As gregster pointed out, this is exactly the same as if you use an expression to build a parameter string in v1.

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 16 Mar 2023, 14:32

Fair enough,

But in my defense, in AHKv1 the *5 wouldn't have quotation marks around it, would it? So I wouldn't have placed it right against the path in the first place :P

Disclaimer:
(I don't have access to a pc right now so I can't test if what I'm claiming is actually true.) :dance:

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Trying AHK2 for the first time

Post by mikeyww » 16 Mar 2023, 14:37

If you were using a forced expression, the result would have been the same. Game over!

:)

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 16 Mar 2023, 17:09

WalkerOfTheDay wrote: But in my defense, in AHKv1 the *5 wouldn't have quotation marks around it, would it?
Yes, it would have. You’re missing the point of what gregster and I and also mikeyww pointed out. Expressions are the same in both v1 and v2, and you can use them in both.
boiler wrote: …this is exactly the same as if you use an expression to build a parameter string in v1.

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 24 Mar 2023, 06:37

Okay, I'm back at it again. This time I thought I try to create a gui in AHKv2, and again failing at it :crazy:
I'm trying to do a simple click on a button and then fire a MsgBox

Sorry for my ignorance in advance.

In V1, I would have just created used a gLabel, but that doesn't work anymore.

Code: Select all

#Requires Autohotkey v2.0

; MyGui := Gui([Options, Title, EventObj])

AHKV2Gui := Gui("AlwaysOnTop", "my title")
AHKV2Gui.Add("Button", "vMyButton", "ButtonText")
AHKV2Gui.OnEvent("Click", AHKV2Gui_Click)
AHKV2Gui_Click {
    MsgBox("blabla")
}
AHKV2Gui.Show("w400 h300")

/* example from manual
myGui := Gui()
myGui.AddText("", "Press Alt+F4 or the X button in the title bar.")
myGui.OnEvent("Close", myGui_Close)
myGui_Close(thisGui) {  ; Declaring this parameter is optional.
    if MsgBox("Are you sure you want to close the GUI?",, "y/n") = "No"
        return true  ; true = 1
}
myGui.Show
*/
Edit

I seem to have found something in the docs that works :mrgreen:

Code: Select all

#Requires Autohotkey v2.0
AHKV2Gui := Gui("AlwaysOnTop", "my title")
AHKV2Gui.Add("Button", "vMyButton", "ButtonText").OnEvent("Click", ClickTheButton)
AHKV2Gui.Show("w400 h300")

ClickTheButton(*)
{
    MsgBox
}

Post Reply

Return to “Ask for Help (v2)”