Problem calling function instead of label Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Problem calling function instead of label

Post by ahketype » 30 Jan 2023, 09:09

Hi, I'm stuck trying to convert a menu item calling a function in v2. I've replaced the label name in the calling item with a function call, and replaced the subroutine with a function that just runs (used to run) another script.

Code: Select all

Tray := A_TrayMenu
Tray.Add("TrayClock", LaunchTrayClock())
;...
return ; end autoexec section
;...
LaunchTrayClock() {
	Run "C:\Users\JF\Documents\Programming\AHK Scripts\TrayIconTime\TrayClock.ahk"
}
The error report in the popup dialog says, Error: Parameter #3 of Menu.Prototype.Add requires an Object, but received an empty string.

Before this, I had a return 0, and the error said it received an integer instead of an object, so presumably it's whatever is being returned that should be an object, but I can't figure it out from the help pages on objects, menus, etc. I'm also confused as to the reference to Parameter #3, when the call has two parameters (and it seems it doesn't need three in Menu.Add(); the third is the Options).

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Problem calling function instead of label

Post by DuckingQuack » 30 Jan 2023, 09:20

Afaik, you don’t need the parentheses following the callback in tray.add, but that may not be the whole problem. Syntax isn’t my strong suit and I’m not currently able to test this but if this hasn’t been resolved before this evening, then I will test some things.
Best of Luck,
The Duck

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

Re: Problem calling function instead of label  Topic is solved

Post by mikeyww » 30 Jan 2023, 09:24

1. The second Add parameter is a function object.

2. Add:
The function should accept the following parameters:
FunctionName(ItemName, ItemPos, MyMenu)

Code: Select all

#Requires AutoHotkey v2.0
Persistent
script := A_ScriptDir "\test.ahk"
If !FileExist(script)
 MsgBox 'File not found.`n`n' script, 'Error', 48
tray := A_TrayMenu
tray.Add("TrayClock", launch)

launch(*) {
 Run script
}
Or:

Code: Select all

#Requires AutoHotkey v2.0
Persistent
script := A_ScriptDir "\test.ahk"
If !FileExist(script)
 MsgBox 'File not found.`n`n' script, 'Error', 48
tray := A_TrayMenu
tray.Add("TrayClock", (*) => Run(script))
The documentation helps you, because it contains an example that you can directly adapt for your script: https://www.autohotkey.com/docs/v2/lib/Menu.htm#ExBasic

iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: Problem calling function instead of label

Post by iseahound » 30 Jan 2023, 11:33

Hi I read your post about struggling with AHK v2. Generally, the v1 Gui functions were partirately bad.

Code: Select all

Tray := A_TrayMenu
Tray.Add("TrayClock", LaunchTrayClock) ; just pass a function object. 
;...
return ; end autoexec section
;...
LaunchTrayClock() {
	Run "C:\Users\JF\Documents\Programming\AHK Scripts\TrayIconTime\TrayClock.ahk"
}

So here's a brief intro:
1. Using a function object as an argument

Code: Select all

; https://www.autohotkey.com/docs/v2/lib/Func.htm
MsgBox LaunchTrayClock.Name

LaunchTrayClock() {
	Run "C:\Users\JF\Documents\Programming\AHK Scripts\TrayIconTime\TrayClock.ahk"
}
2. Executing a function

Code: Select all

LaunchTrayClock()
3. Executing a function (only works at the beginning of a line)

Code: Select all

LaunchTrayClock
4. Fat arrow syntax (In line functions)

Code: Select all

; https://www.autohotkey.com/docs/v2/lib/Func.htm
MsgBox LaunchTrayClock.Name

LaunchTrayClock() =>
	Run("C:\Users\JF\Documents\Programming\AHK Scripts\TrayIconTime\TrayClock.ahk")
5. Single lines

Code: Select all

Tray := A_TrayMenu
Tray.Add("TrayClock", LaunchTrayClock) ; just pass a function object. 
;...
return ; end autoexec section
;...
LaunchTrayClock() =>
	Run("C:\Users\JF\Documents\Programming\AHK Scripts\TrayIconTime\TrayClock.ahk")

Code: Select all

Tray := A_TrayMenu
Tray.Add("TrayClock", LaunchTrayClock) ; just pass a function object. 
;...
return ; end autoexec section
;...
LaunchTrayClock() => Run("C:\Users\JF\Documents\Programming\AHK Scripts\TrayIconTime\TrayClock.ahk")

Code: Select all

Tray := A_TrayMenu
Tray.Add("TrayClock", LaunchTrayClock() => Run("C:\Users\JF\Documents\Programming\AHK Scripts\TrayIconTime\TrayClock.ahk")) ; just pass a function object. 
;...
return ; end autoexec section
;...

Code: Select all

Tray := A_TrayMenu
Tray.Add("TrayClock", LaunchTrayClock() => Run("C:\Users\JF\Documents\Programming\AHK Scripts\TrayIconTime\TrayClock.ahk")) ; just pass a function object. 
;...
; autoexec section doesn't exist in v2
;...

ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: Problem calling function instead of label

Post by ahketype » 30 Jan 2023, 19:21

Thanks guys. I selected @mikeyww's as the solution, but those were all helpful. The example you linked to, mikey, did help me understand what the three parameters referred to were.

@DuckingQuack, you were right on that main issue - no parentheses should be there. And @iseahound, I do appreciate all those examples of different ways to use the feature. I'm still deep in the woods with objects, classes and so on, but that's one more bit I can get right now.

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Problem calling function instead of label

Post by DuckingQuack » 30 Jan 2023, 20:12

@ahketype Yay! Glad I could help at all!
mikeyww wrote:
29 Jan 2023, 21:04
You're an expert now! Time to teach others.
Mikeyww will be so proud!
Best of Luck,
The Duck

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

Re: Problem calling function instead of label

Post by mikeyww » 30 Jan 2023, 20:14

Indeed!

:)

Post Reply

Return to “Ask for Help (v2)”