AutoHotkey: Popup menu for button bar or F4

From TotalcmdWiki
Revision as of 10:28, 13 December 2005 by Mbirth (talk | contribs) (initial version, something missing due to server error)
Jump to navigation Jump to search

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..., ClickMenu shows a popup menu with entries read from an INI file and launches the selected program upon click.`n`nMade 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
 

Detailed description

Back to AutoHotkey