Create a menu from an array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Create a menu from an array

04 May 2021, 13:14

Instead of creating menu items manually -

Code: Select all

    Item1 := Func("Func").Bind(1, 2, "abcdefg")
    Menu, MyMenu, Add, Item1, % Item1

Func(One, Two, Three) {
    Run, prog.exe %One% %Two% %Three%
    return
}
- I have found this example that apparently creates a menu from an array:

Code: Select all

Array := []
Loop, Read, tmp.txt
    Array.Push(A_LoopReadLine)
sendElement(ItemName, ItemPos) {
    Clipboard := ""
    Clipboard := RegExReplace(ItemName, "^" ItemPos " ")
    Clipwait, 3
    Send ^{v}
}
for index, element in Array
    Menu, MyMenu, Add, %index% %element%, sendElement
return

Menu, MyMenu, Show
However, I am unclear how it works: do I need to use the clipboard; should tmp.txt be space-delimited; etc?
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create a menu from an array

04 May 2021, 13:34

The file is delimited by newlines (i.e., each element on its own line). And it doesn't use the clipboard for creating the menu. The menu items execute the function that pastes text when the user selects a menu item, which is where the clipboard is used. So you can remove the function and replace what each menu item does with your own routine(s). The function is somewhat oddly stuck in the middle of the code, and the line that shows the menu would never be executed where it is placed. The following replaces that function with one that just displays the info in a MsgBox, and you press F1 to call up the menu:

Code: Select all

Array := ["Red", "Green", "Blue"] ; in place of reading from file for demo purposes
for index, element in Array
    Menu, MyMenu, Add, %index% %element%, ShowElement
return

F1::Menu, MyMenu, Show

ShowElement(ItemName, ItemPos) {
    MsgBox, % "Item name: " ItemName "`nPosition: " ItemPos
}
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Create a menu from an array

04 May 2021, 15:46

Even with #Persistent, I cannot get the script to do anything but report 004: Exit.

Instead of having each element on its own line, can I set up the file thus:

Code: Select all

1, 2, abcdefg
2, 3, bcdefgh
...
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create a menu from an array

04 May 2021, 15:55

Ecimeric wrote: Even with #Persistent, I cannot get the script to do anything but report 004: Exit.
You ran the above script and pressed the F1 key on your keyboard, and it didn't present a menu with three options?

Ecimeric wrote: Instead of having each element on its own line, can I set up the file thus:

Code: Select all

1, 2, abcdefg
2, 3, bcdefgh
...
You can set it up however you want, but I don't understand what 1, 2 and abcdefg would be. Can you explain what the various items on each line are?
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create a menu from an array

04 May 2021, 16:03

You should run the script that I posted exactly as shown to verify that you got the expected results. Remember to press the F1 hotkey. I assume when you say it did nothing, that's after you modified it to try to get it to read the file, so there must be some error there. Please post the script as you ran it.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Create a menu from an array

04 May 2021, 16:22

boiler wrote:
04 May 2021, 15:55
You ran the above script and pressed the F1 key on your keyboard, and it didn't present a menu with three options?
Correct, except for #Persistent in order to press F1.
boiler wrote:
04 May 2021, 15:55
You can set it up however you want, but I don't understand what 1, 2 and abcdefg would be. Can you explain what the various items on each line are?
They are start, stop (in seconds) and ID; ie. sets of three elements.
boiler wrote:
04 May 2021, 16:03
I assume when you say it did nothing, that's after you modified it to try to get it to read the file, so there must be some error there. Please post the script as you ran it.
As above.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create a menu from an array

04 May 2021, 16:35

Ecimeric wrote: Correct, except for #Persistent in order to press F1.
That wouldn't be necessary. You never need to add #Persistent to a script that has a hotkey defined. It is automatically persistent as described in the documentation for #Persistent:
A script is persistent if any of the following conditions are true:
  • At least one hotkey or hotstring has been defined in the script or created by the Hotkey command or Hotstring function, even if it is not enabled.
  • ...

When the script is running and you press F1, the menu should appear at the location of your mouse pointer, wherever that is.

Do you have any other scripts running that use F1 as a hotkey? You might try changing F1 to something else.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Create a menu from an array

04 May 2021, 16:45

boiler wrote:
04 May 2021, 16:35
Do you have any other scripts running that use F1 as a hotkey? You might try changing F1 to something else.
I tried other keys, but the script just does not stay open in order to press any.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create a menu from an array

04 May 2021, 16:48

Then you don't really have a hotkey defined. A hotkey must have two colons (::) after it, not just one. If it still does doesn't stay open, then post your script because something is fundamentally wrong. AHK does not work the way you are describing it.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Create a menu from an array

04 May 2021, 20:50

I have it working: I had */ at the end of a comment; whereas AHK needs it to be on a separate line.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create a menu from an array

04 May 2021, 21:07

That's why I asked if you were running the script exactly as I posted, and asked you to post what you were actually running. That issue would have been spotted immediately. That would save a lot of posts back and forth, testing the code, reviewing it for what could be wrong, etc.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Create a menu from an array

04 May 2021, 21:26

:oops:

Looking at the Arrays documentation, I cannot see how to get from -

Code: Select all

Red
Green
Blue
- to passing arguments behind each of those elements to my function; ie. how can I pass 1, 2, abcdefg when I select Red?
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create a menu from an array

04 May 2021, 21:52

Well, I wouldn't think you'd want to build the menu with Red, Green, and Blue as in my example. I would have each menu item be a line from your file (broken into array elements) like this:

Code: Select all

Array := []
Loop, Read, tmp.txt
    Array.Push(StrSplit(A_LoopReadLine, ","))
for index, element in Array
    Menu, MyMenu, Add, % element.1 " " element.2 " " element.3, ShowElement
return

F1::Menu, MyMenu, Show

ShowElement(ItemName, ItemPos) {
    MsgBox, % "Item name: " ItemName "`nPosition: " ItemPos
}

Your file should have the data in each line separated by commas without extraneous spaces, like this:

Code: Select all

1,2,abcdefg
2,3,bcdefgh
...
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create a menu from an array  Topic is solved

04 May 2021, 22:03

What you really should operate with in the function is the ItemPos, which tells you which item of the Array to use. So the function should either have that array bound as a parameter or (easier for now) just make Array global. This is a tested, working script (assuming tmp.txt is populated correctly):

Code: Select all

Array := []
Loop, Read, tmp.txt
    Array.Push(StrSplit(A_LoopReadLine, ","))
for index, element in Array
    Menu, MyMenu, Add, % element.1 " " element.2 " " element.3, ShowElement
return

F1::Menu, MyMenu, Show

ShowElement(ItemName, ItemPos) {
    global Array
    MsgBox, % "Start: " Array[ItemPos].1 "`nStop: " Array[ItemPos].2 "`nID: " Array[ItemPos].3
}
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Create a menu from an array

04 May 2021, 22:35

Thank you; now I just need to work out the syntax for this part of my function: https://doma.in/Array[ItemPos].4. (I made element.1 the MenuItemName).
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create a menu from an array

04 May 2021, 22:48

Are you trying to open the site that would be defined by that URL? You should be able to open it in the default browser like this:

Code: Select all

Run, % "https://doma.in/" . Array[ItemPos].4
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Create a menu from an array

04 May 2021, 22:58

I am trying Run, vlc.exe "https://doma.in/" . Array[ItemPos].4 but VLC doesn't like the quotes.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create a menu from an array

05 May 2021, 04:14

You used expression syntax in a place that expects legacy syntax (actually, you mixed both, which you can’t do anywhere). You have to force an expression, then use expression syntax throughout:

Code: Select all

Run, % "vlc.exe https://doma.in/" . Array[ItemPos].4
You could alternatively use legacy syntax, but you’d have to first assign the array element to a simple variable:

Code: Select all

UrlEnd := Array[ItemPos].4
Run, vlc.exe https://doma.in/%UrlEnd%
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Create a menu from an array

05 May 2021, 12:53

Thank you; I have it working, and am chuffed with the results :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Holarctic, mapcarter, robnicholson, Rohwedder and 333 guests