AutoHotkey: Popup menu for button bar or F4
Overview
This script shows a popup menu with programs defined in a configuration file. Selecting an entry launches the program with the given parameters. Useful to assign different external programs to a single button in the button bar or to use it as an editor-selector for F4.
Source
#NoTrayIcon
;#ErrorStdOut
SplitPath A_ScriptFullPath, null, IniDir, null, IniFile, null
IniFile = %IniDir%\%IniFile%.ini
IfNotExist %IniFile%
{
MsgBox Could not find %IniFile%.
ExitApp
}
ProcessMenu("Menu")
Menu Menu, Show
Exit
SplitFirst(ByRef OutLeft, ByRef OutRight, InpText, InpSep)
{
StringGetPos SepPos, InpText, %InpSep%
If (SepPos >= 0)
{
StringLeft OutLeft, InpText, %SepPos%
RemChars := StrLen(InpText)-SepPos-1
StringRight OutRight, InpText, %RemChars%
}
Else
{
OutLeft := InpText
OutRight := ""
}
}
ProcessMenu(Menu)
{
global IniFile
IniRead MenuTitle, %IniFile%, %Menu%, Title
If (MenuTitle <> "ERROR")
{
Menu %Menu%, Add, %MenuTitle%, AboutBox
Menu %Menu%, Default, %MenuTitle%
; Menu %Menu%, Disable, %MenuTitle%
Menu %Menu%, Add
}
IniRead Count, %IniFile%, %Menu%, MaxItem, 0
Loop %Count%
{
IniRead, CurItem, %IniFile%, %Menu%, %A_Index%
If (CurItem = "-")
Menu %Menu%, Add
Else If (CurItem <> "ERROR")
{
SplitFirst(CurID, CurTitle, CurItem, "|")
If (CurTitle = "")
CurTitle := CurID
IniRead IsSub, %IniFile%, %CurID%, MaxItem, 0
If (IsSub = 0)
{
Menu %Menu%, Add, %CurTitle%, ProcessEvent
IniRead CurCmd, %IniFile%, %CurID%, Cmd
If (CurCmd = "ERROR")
Menu %Menu%, Disable, %CurTitle%
}
else
{
ProcessMenu(CurID)
Menu %Menu%, Add, %CurTitle%, :%CurID%
}
IniRead IsChecked, %IniFile%, %CurID%, Checked, 0
If (IsChecked = 1)
Menu %Menu%, Check, %CurTitle%
}
}
Return
}
ProcessEvent:
IniRead Count, %IniFile%, %A_ThisMenu%, MaxItem, 0
Loop %Count%
{
IniRead, CurItem, %IniFile%, %A_ThisMenu%, %A_Index%
If (CurItem <> "ERROR")
{
SplitFirst(CurID, CurTitle, CurItem, "|")
If (CurTitle = "")
CurTitle := CurID
If (CurTitle = A_ThisMenuItem)
{
IniRead Cmd, %IniFile%, %CurID%, Cmd
If (Cmd <> "ERROR")
{
empty := ""
IniRead TargDir, %IniFile%, %CurID%, StartPath, %empty%
IniRead StartMode, %IniFile%, %CurID%, StartMode, %empty%
Loop 9
{
CurPar := %A_Index%
StringReplace Cmd, Cmd, `%%A_Index%, %CurPar%
}
; Use RunWait for ex. Total Commander, where the starting app has to know
; whether the launched app is still running. Otherwise use Run.
RunWait %Cmd%, %TargDir%, %StartMode%, PID
}
}
}
}
Return
AboutBox:
MsgBox 4160, About ClickMenu...,
(Ltrim, Join
ClickMenu shows a popup menu with entries read from an INI file%a_space%
and launches the selected program upon click.`n`n
Made in 2005 by Markus Birth <mbirth at webwriters.de>
)
Return
Usage
Preparations
To make things simple, compile this script using ahk2exe (contained in the AutoHotkey download) first. Let's assume you named it ClickMenu.exe. Assign the ClickMenu.exe to a new button on the button bar and add all required parameters (up to nine) to the "Parameters" field of the button. You can refer to these later by using %1..%9 in the ClickMenu.ini. Alternatively you can assign the ClickMenu.exe to the F4 key in the Total Commander options.
Configuration
All configuration is done through an INI file which has to have the same basename as the script or compiled script. So in our case, the INI file has to be named ClickMenu.ini.
Sample configuration file
A sample INI file looks like this:
[Menu]
;Maximum number to parse, starting at 1
;Empty values are ignored.
MaxItem=99
Title=blafasel
1=TE1|Test entry
2=-
3=TES|Test sub
4=TE3|Test sub2
[TE1]
Cmd=notepad.exe %1
;Checked=1
; (omit following to start in current path)
StartPath=C:\TEMP
; StartMode=(Max|Min|Hide)
; (omit following to start normally)
StartMode=Max
[TES]
;Checked=1
MaxItem=1
1=TE2|Test2 entry
[TE2]
Cmd=explorer.exe
[TE3]
MaxItem=1
1=TE2|Test2 entry again
Detailed description
Menus
The section [Menu]
contains the main menu visible after clicking the button or pressing F4. Each menu section consists of one to many menu items defined by numerical keys (assuming the format Key=Value
) ranging from 1..n. The numbering must not be continuous but the value of MaxItem
has to be the number of the key of the last menu item, i.e. every menu item with a number higher than MaxItem
is ignored. Also be aware that all entries are displayed in the order of their number starting from the lowest going up to the highest.
Each menu may have a title defined by the key Title
. This is displayed in bold above the entries.
Each entry has the format x=ItemSection|ItemTitle
with x
being the numerical key, ItemSection
being the section in which the item is defined and ItemTitle
being the string displayed in the menu.
A separator is defined by x=-
.
Item- and submenu-sections can also contain Checked=1
to display a checkmark in front of the parent menu's entry.
Items
To define a program to launch, use the syntax Cmd=app.exe /parameters
. You can use the parameters defined in the button's Parameters field by setting something like Cmd=app.exe %3
which would start app.exe with the third parameter defined in the button's options.
To display a checkmark in front of the menu entry, you can use Checked=1
.
Additionally you can define StartPath=c:\WINDOWS
to launch the program with that starting path and StartMode=Max
to launch it maximized, StartMode=Min
to launch it minimized or StartMode=Hide
to launch the program hidden. If you omit StartPath
, the current directory will be used. If you omit StartMode
, the program will be launched normally.
Links
Back to AutoHotkey