Adding additional parameters to Menu Item's callback function?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
MitchyBeMitchin
Posts: 2
Joined: 31 May 2023, 16:04

Adding additional parameters to Menu Item's callback function?

Post by MitchyBeMitchin » 31 May 2023, 16:46

Hi All,

I'm currently working on a small AHK v2 script that will let me quickly launch various windows applications from a system tray icon.

Right now, I've just implemented the CMD window. As part of the functionality, I want to have the ability to run this exe with or without Administrative access.

I've been able to implement this by using a Submenu (the "CMD" menu item splits out into "Run" and "Run as Admin" options). Each of these two submenu options has its own corresponding callback function which it executes when selected which are being set using the second parameter of the Menu's Add() function. You can see this working implementation below:

Code: Select all

#Requires AutoHotkey v2.0
Persistent					; Keep the script running until the user exits it.
#SingleInstance				; Only one instance of this script can be running at one time

/*
 * Global Variables
 */
AdminVerb := "*RunAs " 	; Corresponds to the "Run as administrator" system verb action that falls under a file's right-click Explorer menu

/*
 * Global Functions
 */
LaunchCmd(*) {
	Run(A_ComSpec)
}

LaunchCmdAdmin(*) {
	Run(AdminVerb . A_ComSpec)
}

ConfigureSystemTray() {
	SysTray := A_TrayMenu
	SysTray.Delete()		; Remove all pre-existing items in the script's system tray icon menu

	; Add CMD to the system tray menu
	CmdSubMenu := Menu()
	CmdSubMenu.Add("Run", LaunchCmd)
	CmdSubMenu.Add("Run as Admin", LaunchCmdAdmin)
	SysTray.Add("CMD", CmdSubMenu)
}


/*
 * Begin main code execution
 */
ConfigureSystemTray()

After getting this implementation working, I realized I could condense my code a bit by combining these callback functions into one LaunchCmd() function which takes a parameter indicating wether to launch the exe as Admin or not. However, I'm not exactly sure if it is possible for me to implement this since the function parameter of the Add() function specifically designates that the function must take three parameters:
Add
Adds or modifies a menu item.
MyMenu.Add(MenuItemName, Function-or-Submenu, Options)
MenuItemName
Type: String
The text to display on the menu item, or the position& of an existing item to modify. See MenuItemName.
Function-or-Submenu
Type: Function Object or Menu
A function object to call as a new thread when the menu item is selected, or a reference to a Menu object to use as a submenu.
This parameter is required when creating a new item, but optional when updating the Options of an existing item.
The function should accept the following parameters:
FunctionName(ItemName, ItemPos, MyMenu)
— from Menu Object documentation page


Is there any way I could go about getting the behavior that I am wanting here?

For visual reference, the following illustrates the idea of what I'm wanting to accomplish (obviously not working code):

Code: Select all

LaunchCmd(ItemName, ItemPos, MyMenu, AsAdmin:=False) {
	AsAdmin ? Run(AdminVerb . A_ComSpec) : Run(A_ComSpec)
}

ConfigureSystemTray() {
	SysTray := A_TrayMenu
	SysTray.Delete()		; Remove all pre-existing items in the script's system tray icon menu

	; Add CMD to the system tray menu
	CmdSubMenu := Menu()
	CmdSubMenu.Add("Run", LaunchCmd)
	CmdSubMenu.Add("Run as Admin", LaunchCmd(*, True))
	SysTray.Add("CMD", CmdSubMenu)
}

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Adding additional parameters to Menu Item's callback function?

Post by teadrinker » 31 May 2023, 18:20

Code: Select all

MyMenu := Menu()
MyMenu.Add('Item without params', MenuHandler)
MyMenu.Add('Item with params', MenuHandler.Bind(,,, 'MyParam1'))
MyMenu.Show()

MenuHandler(ItemName, ItemPos, MyMenu, Param1 := '') {
    MsgBox 'ItemName: ' . ItemName . '`nParam1: ' . Param1
}

MitchyBeMitchin
Posts: 2
Joined: 31 May 2023, 16:04

Re: Adding additional parameters to Menu Item's callback function?

Post by MitchyBeMitchin » 31 May 2023, 21:28

Using Bind() did the trick; thanks for the help!

Post Reply

Return to “Ask for Help (v2)”