Help in scripting subMenus Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
leo007
Posts: 50
Joined: 26 Oct 2019, 04:56

Help in scripting subMenus

07 Dec 2019, 02:58

Hi Everyone,

I have script that I found on the web that close to my need.
I need help in scripting to modify this code so it will have simple submenus

Code: Select all

::nu::
  v = 1=a,"2=b"
  TextMenu(v)
  return

TextMenu(TextOptions) {
    arrVal := []
    fn := Func("MenuAction").Bind( arrVal )

    Loop, Parse, TextOptions, CSV
    {
      arr := StrSplit(A_LoopField, "=", " ")
      arrVal.push(arr[2])
      Menu, MyMenu, Add, % arr[1], % fn
    }

    Menu, MyMenu, Show
    Menu, MyMenu, DeleteAll
}

MenuAction(arrVal) {
  SendInput % "{Text}" . arrVal[A_ThisMenuItemPos]
}

Example for sub menu structure:

|Menu:
|--subMenu1
|------item1
|------item2
|--subMenu2
|------item3
|------item4

Thanks in advance for your help :)
leo007
Posts: 50
Joined: 26 Oct 2019, 04:56

Re: Help in scripting subMenus

07 Dec 2019, 04:08

tmplinshi Thanks for your Reply
the script is a bit difficult to me so I didn't understood it.
I guess I make something wrong but copied the script from best choosed answer by hitting F1 script do nothing.
tmplinshi
Posts: 1561
Joined: 01 Oct 2013, 14:57

Re: Help in scripting subMenus  Topic is solved

07 Dec 2019, 06:47

Maybe try to change the hotkey F1 to another key.

Code: Select all

menuData =
(
subMenu1
	item1 = a
	item2 = b
subMenu2
	item3 = c
	item4 = d
)

menu1 := new textMenu(menuData, "OnMenuSelect")
return

:X:nu::menu1.show()

OnMenuSelect(Command) {
	SendInput, {Text}%Command%
}

class textMenu
{
	__New(ByRef VariableOrFileName, FunctionName := "") {
		if !IsFunc(FunctionName)
			throw FunctionName " is not a valid function name"

		this.dat := this.dataToArray(VariableOrFileName)
		this.fn := FunctionName
		this.menuName := &this

		for i, line in this.dat
		{
			if tabCount := this.isSubMenu(i)
				this.begin_subMenu(i, tabCount)
			else
			{
				line := LTrim(line, "`t")

				this.addMenu(line)

				if (SubStr(line, 1, 1) = ".")
					Menu, % this.menuName, Disable, % SubStr(line, 2)

				if this.isEndOfSubMenu(i)
					this.add_subMenu()
			}
		}

		this.dat := this.fn := this.subMenu := ""
	}

	__Delete() {
		Menu, % this.menuName, Delete
	}

	addMenu(ByRef line) {
		if (line ~= "^-{3,}")
			Menu, % this.menuName, Add
		else
		{
			if !RegExMatch(line, "^(.*?)\s*=\s*(.*)$", m) {
				m1 := line
			}
			m1 := LTrim(m1, ".")
			fn := Func(this.fn).Bind(m2)
			Menu, % this.menuName, Add, % m1, % fn
		}
	}

	nextLineTabCount(i) {
		if ( i+1 <= this.dat.MaxIndex() )
		&& ( RegExMatch(this.dat[i+1], "P)^\t+", tabCount) )
			return tabCount
	}

	isEndOfSubMenu(i) {
		return this.nextLineTabCount(i) < this.subMenu.tabCount
	}

	isSubMenu(i) {
		tabs := this.nextLineTabCount(i)
		return (tabs > this.subMenu.tabCount) ? tabs : 0
	}

	begin_subMenu(i, tabCount) {
		this.subMenu := { name: this.menuName
		                , text: LTrim(this.dat[i], "`t")
		                , tabCount: tabCount
		                , pre_subMenu: this.subMenu }
		this.menuName := &this "_" i
	}

	add_subMenu() {
		Menu, % this.subMenu.name, Add, % this.subMenu.text, % ":" this.menuName
		this.menuName := this.subMenu.name
		this.subMenu := this.subMenu.pre_subMenu
	}

	dataToArray(ByRef VariableOrFileName) {
		if FileExist(VariableOrFileName)
			content := FileOpen(VariableOrFileName, "r").Read()
		else
			content := VariableOrFileName
		
		content := Trim(content, "`r`n")
		content := RegExReplace(content, "\R+", "`n")
		return StrSplit(content, "`n", "`r")
	}

	show() {
		Menu, % this.menuName, Show
	}
}
leo007
Posts: 50
Joined: 26 Oct 2019, 04:56

Re: Help in scripting subMenus

07 Dec 2019, 07:59

tmplinshi your script works great. Thank you very much :)

just one question
I found another short and very simple alternative way do make submenu using GUI:

Code: Select all

F1::
  Menu, S_Menu, Add, Text3, LAB_1
  Menu, S_Menu, Add, Text4, LAB_2
  Menu, M_Menu, Add, --SUB MENU-- , :S_Menu

  Menu, M_Menu, Show
  return

  LAB_1:
      send, 1
    Return

  LAB_2:
      send, 2
    Return

  Esc:: ExitApp
so I'm very curious to know you opinion, and are there any drawbacks to create and manage menus/submenus with this simple GUI over your related script suggestion?
tmplinshi
Posts: 1561
Joined: 01 Oct 2013, 14:57

Re: Help in scripting subMenus

07 Dec 2019, 08:22

The simple code you provided is better. The textMenu function is just more convenient to configure the menu, without writting the Menu codes.
leo007
Posts: 50
Joined: 26 Oct 2019, 04:56

Re: Help in scripting subMenus

07 Dec 2019, 08:32

great!
thanks once again)

Return to “Ask For Help”

Who is online

Users browsing this forum: lexikos, rifqii_, Smile_ and 56 guests