parse json into submenu and sub-sub menus in v2 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sanneedshelp
Posts: 18
Joined: 10 Mar 2024, 19:52

parse json into submenu and sub-sub menus in v2

29 Mar 2024, 12:46

i want to accomplish the same code but instead of hardcoding i want to read it from a json structure. please help ?

working Code

Code: Select all


m := Menu()
mainmenu :=  Menu()
submenu1 :=  Menu()
submenu2 :=  Menu()
submenu3 :=  Menu()


m.Add("mainmenu", mainmenu)

mainmenu.Add("What is the Queston 1", submenu1)
submenu1.Add("The answer can be anything -it --rm -ssh --especially blah <> --image", MenuHandler)

mainmenu.Add("can you type this", submenu2)
submenu2.Add("yes i can type 'anything' you want me to", MenuHandler)
submenu2.Add("i can also write anything that is gibberish", MenuHandler)

mainmenu.Add("Good Morning", submenu3)
submenu3.Add("morning is ok -- with so many ideans like , :' and also anything ", MenuHandler)
submenu3.Add("very good morining", MenuHandler)
submenu3.Add("Thank you", MenuHandler)



MenuHandler(Item, *){
    A_Clipboard := Item
	Send('^v')
}


#n::m.Show()  ; i.e. press the Win-Z hotkey to show the menu.


here is the json structure

{
"Mainmenu1": {
"What is the Queston 1": [
"The answer can be anything -it --rm aks-ssh --especially blah <> --image"
],
"can you type this": [
"yes i can type \"anything\" you want me to",
"i can also write anything that is gibberish"
],
"Good Morning": [
"very good morning",
"morning is ok -- with so many ideas like , :' and also anything ",
"Thank you"
]
},
"Mainmenu2": {
"Random Query": [
"Some random command -it --rm --image=randomimage:latest"
],
"Lorem Ipsum": [
"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
"Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
],
"Gibberish Text": [
"Flibber flabber blibber blabber",
"Blah blah blah -- with symbols like @#$%&*()!",
"Thank you for using gibberish"
]
}
}


Non working code, Some thing like below


Code: Select all


; Read the JSON file
jsonContent := FileRead("C:\key\myjson.txt")

; Parse the JSON content
json := JSON.Load(jsonContent)

; Create the main menu
m := Menu()

; Loop through each main menu item in the JSON
for mainMenuKey, mainMenuValue in json
{
    ; Create a new submenu
    subMenu := Menu()

    ; Loop through each submenu item
    for subMenuKey, subMenuValue in mainMenuValue
    {
        ; Add the submenu items
        for index, item in subMenuValue
        {
            subMenu.Add(item, "MenuHandler")
        }
    }

    ; Add the main menu items
    m.Add(mainMenuKey, subMenu)
}

; Define the menu handler
MenuHandler(Item, *)
{
    A_Clipboard := Item
    Send('^v')
}

; Hotkey to show the menu
#n::m.Show()  ; i.e. press the Win-N hotkey to show the menu.






ntepa
Posts: 429
Joined: 19 Oct 2022, 20:52

Re: parse json into submenu and sub-sub menus in v2

29 Mar 2024, 14:20

Try this:

Code: Select all

; Read the JSON file
jsonContent := FileRead("C:\key\myjson.txt")

; Parse the JSON content
jsonObj := JSON.Load(jsonContent)

m := Menu()

objToMenu(jsonObj, m)

objToMenu(obj, menuObj?) {
    if !IsSet(menuObj)
        menuObj := Menu()
    for itemName, menuItem in (obj is Array ? obj : obj.OwnProps()) {
        if IsObject(menuItem) {
            menuObj.Add(itemName, objToMenu(menuItem))
        } else {
            menuObj.Add(menuItem, MenuHandler)
        }
    }
    return menuObj

    MenuHandler(item, *) {
        A_Clipboard := item
        Send('^v')
    }
}

; Hotkey to show the menu
#n::m.Show()  ; i.e. press the Win-N hotkey to show the menu.
sanneedshelp
Posts: 18
Joined: 10 Mar 2024, 19:52

Re: parse json into submenu and sub-sub menus in v2

30 Mar 2024, 07:36

#include C:\hotkey\json.ahk


This is the one i have
https://github.com/cocobelgica/AutoHotkey-JSON/blob/master/JSON.ahk#L44


This is compiling through v1 but error when using v2 got below error
image.png
image.png (30.24 KiB) Viewed 148 times
Also when i compiled it through v1 got below error
image.png
image.png (18 KiB) Viewed 148 times
ntepa
Posts: 429
Joined: 19 Oct 2022, 20:52

Re: parse json into submenu and sub-sub menus in v2

30 Mar 2024, 13:01

You can't run v1 and v2 code in the same script. That json library is written for v1.
Try this library instead: [Library] cJson.ahk for AHKv2 (version 2.0.0)
sanneedshelp
Posts: 18
Joined: 10 Mar 2024, 19:52

Re: parse json into submenu and sub-sub menus in v2

30 Mar 2024, 17:37

Thank you!! i was now able to compile successfully after using the new library that you gave and then win + n menu doesn't show up at all.

attached Code , json.ahk and actual json

Code: Select all


#include C:\hotkey\json.ahk

; Read the JSON file
jsonContent := FileRead("C:\hotkey\myjson1.json")

; Parse the JSON content
jsonObj := JSON.Load(jsonContent)

m := Menu()

objToMenu(jsonObj, m)

objToMenu(obj, menuObj?) {
    if !IsSet(menuObj)
        menuObj := Menu()
    for itemName, menuItem in (obj is Array ? obj : obj.OwnProps()) {
        if IsObject(menuItem) {
            menuObj.Add(itemName, objToMenu(menuItem))
        } else {
            menuObj.Add(menuItem, MenuHandler)
        }
    }
    return menuObj

    MenuHandler(item, *) {
        A_Clipboard := item
        Send('^v')
    }
}

; Hotkey to show the menu
#n::m.Show()  ; i.e. press the Win-N hotkey to show the menu.

image.png
image.png (95.9 KiB) Viewed 122 times
image.png
image.png (192.89 KiB) Viewed 122 times


{
"Mainmenu1": {
"What is the Queston 1": [
"The answer can be anything -it --rm aks-ssh --especially blah <> --image"
],
"can you type this": [
"yes i can type \"anything\" you want me to",
"i can also write anything that is gibberish"
],
"Good Morning": [
"very good morning",
"morning is ok -- with so many ideas like , :' and also anything ",
"Thank you"
]
},
"Mainmenu2": {
"Random Query": [
"Some random command -it --rm --image=randomimage:latest"
],
"Lorem Ipsum": [
"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
"Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
],
"Gibberish Text": [
"Flibber flabber blibber blabber",
"Blah blah blah -- with symbols like @#$%&*()!",
"Thank you for using gibberish"
]
}
}
ntepa
Posts: 429
Joined: 19 Oct 2022, 20:52

Re: parse json into submenu and sub-sub menus in v2  Topic is solved

30 Mar 2024, 18:42

Try this:

Code: Select all

#include C:\hotkey\json.ahk

; Read the JSON file
jsonContent := FileRead("C:\hotkey\myjson1.json")

; Parse the JSON content
jsonObj := JSON.Load(jsonContent)

m := Menu()

objToMenu(jsonObj, m)

objToMenu(obj, menuObj?) {
    if !IsSet(menuObj)
        menuObj := Menu()
    try enum := obj.__Enum(2)
    catch
        enum := obj.OwnProps()
    for itemName, menuItem in enum {
        if IsObject(menuItem) {
            menuObj.Add(itemName, objToMenu(menuItem))
        } else {
            menuObj.Add(menuItem, MenuHandler)
        }
    }
    return menuObj

    MenuHandler(item, *) {
        A_Clipboard := item
        Send('^v')
    }
}

; Hotkey to show the menu
#n::m.Show()  ; i.e. press the Win-N hotkey to show the menu.
sanneedshelp
Posts: 18
Joined: 10 Mar 2024, 19:52

Re: parse json into submenu and sub-sub menus in v2

30 Mar 2024, 19:45

@ntepa Thank you so much. that worked. :)
sanneedshelp
Posts: 18
Joined: 10 Mar 2024, 19:52

Re: parse json into submenu and sub-sub menus in v2

31 Mar 2024, 22:56

@ntepa Could you help me with one other issue that im running into, i can log new post but this is just continuation of the existing logic.

Issue: when ever the json file has a large value , it is throwing an error menu is too large. in order to fix the issue i implemented below logic and added shortValue to menuhandler

Code: Select all

        } else {
            ; Truncate the string to 90 characters and add ellipsis if it's longer
            shortValue := SubStr(menuItem, 1, 45)
            if (StrLen(menuItem) > 45) {
                shortValue .= "..."
            }
            ; Add the truncated string as a menu item
            menuObj.Add(shortValue, MenuItemHandler)

This is all good and working and resolved my menu too large issue but when i want to send text i don't want to send shortvalue but would like to send full text from the json which is menuitem.
could you help me direction how i can achieve this.

Code: Select all


MenuItemHandler(selectedItem, *) {
    A_Clipboard := selectedItem
    if (SubStr(selectedItem, 1, 4) = "http") {
        Run selectedItem
    } else {
        Send('^v')
    }


full script

Code: Select all

#include C:\hotkey\json.ahk

; Read the JSON file
jsonContent := FileRead("C:\hotkey\myjson1.json")

; Parse the JSON content
jsonObj := JSON.Load(jsonContent)

; Create a new menu
m := Menu()

; Convert the JSON object to a menu
objToMenu(jsonObj, m)

; Function to convert an object to a menu
objToMenu(obj, menuObj?) {
    if !IsSet(menuObj)
        menuObj := Menu()
    try enum := obj.__Enum(2)
    catch
        enum := obj.OwnProps()
    for itemName, menuItem in enum {
        if IsObject(menuItem) {
            ; Create a submenu for the object
            submenu := Menu()
            objToMenu(menuItem, submenu)
            menuObj.Add(itemName, submenu)
        } else {
            ; Truncate the string to 90 characters and add ellipsis if it's longer
            shortValue := SubStr(menuItem, 1, 45)
            if (StrLen(menuItem) > 45) {
                shortValue .= "..."
            }
            ; Add the truncated string as a menu item
            menuObj.Add(shortValue, MenuItemHandler)
        }
    }
    return menuObj
}

; Function to handle menu item selection
MenuItemHandler(selectedItem, *) {
    A_Clipboard := selectedItem
    if (SubStr(selectedItem, 1, 4) = "http") {
        Run selectedItem
    } else {
        Send('^v')
    }
}

; Hotkey to show the menu
#n::m.Show()  ; Press the Win-N hotkey to show the menu.

ntepa
Posts: 429
Joined: 19 Oct 2022, 20:52

Re: parse json into submenu and sub-sub menus in v2

31 Mar 2024, 23:34

Try this:

Code: Select all

#include C:\hotkey\json.ahk

; Read the JSON file
jsonContent := FileRead("C:\hotkey\myjson1.json")

; Parse the JSON content
jsonObj := JSON.Load(jsonContent)

m := Menu()

objToMenu(jsonObj, m)

objToMenu(obj, menuObj?) {
    if !IsSet(menuObj)
        menuObj := Menu()
    try enum := obj.__Enum(2)
    catch
        enum := obj.OwnProps()
    for itemName, menuItem in enum {
        if IsObject(menuItem) {
            menuObj.Add(itemName, objToMenu(menuItem))
        } else {
            if StrLen(menuItem) > 45 {
                shortValue := SubStr(menuItem, 1, 45) "..."
            } else shortValue := menuItem
            menuObj.Add(shortValue, MenuHandler.Bind(menuItem))
        }
    }
    return menuObj

    MenuHandler(selectedItem, *) {
        if SubStr(selectedItem, 1, 4) = "http" {
            Run(selectedItem)
        } else {
            A_Clipboard := selectedItem
            Send('^v')
        }
    }
}

; Hotkey to show the menu
#n::m.Show()  ; i.e. press the Win-N hotkey to show the menu.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot], magicmore, mikeyww and 42 guests