From Variable in Array To Its Index


I've know the syntax IndexNumber that gives "the index of the array value within the Variable Array ", so that IndexNumber returns the corresponding Variable in an array

But how can I do to have my Variable in an array to return its IndexNumber ?

For example below

I would like when user select 10 to return its index 1, 20 to return its index 2 ...

I've made some research but have to clue about how to work this way

Take a look at the "Prompt With List" action, particularly the How to Store a Different Value than Displayed -- a much more user-friendly way of doing what you want.

1 Like

You can also use that technique in the Prompt for User Input action.

1 Like

One way you could do this with a list of numbers that might change is to first have your list of numbers as a Variable with each number on a new line. Then convert that list into a form that can be used in your prompt (10|20|30|45 etc).

After the choice of number is made, you can use a For Each Action to count down through the lines until a match is found (the chosen number matched to one of the original numbers in the list). The number of lines counted until the match will be the "index" number you are after.

The good thing about this approach is that you can change the numbers in the list at any time, the list can be of any length and the Macro will still return the index number.

EXAMPLE Return Index Number of Chosen Number v3.00.kmmacros (7.4 KB)

Click to Show Image of Macro


And a contrariwise version on the same theme -- number your list in the "friendly values" way linked to earlier, i.e.


...then feed that into your prompt.

This will auto-number an easily edited list and provide it in a prompt for you:

Return Index Number of Chosen Number (Friendly Values Version).kmmacros (5.1 KB)


1 Like

And since I seem to be on a roll... Since our (pseudo) array is just text, we can chop off the text after our chosen item, then get the length of the remaining array. (Note -- you need to add |s to the start and end of the text so you can search for a completely-delimited number, hence the subtraction after getting the matched index.)

Return Index Number of Chosen Number (Array Version).kmmacros (6.2 KB)


My gut says this should be quicker than iterating through an array to find a match, especially on longer lists.

1 Like

I've been dealing with the below prompt action to select 'Best Folder' to move a file into.

It is getting wildly long in length and hard to edit.

You just showed me how to do it much easier.

Thanks very much!

My Way!

Your Way!

1 Like

Hey Steve,

Another option would be to use a Prompt With List action.

You can arbitrarily name path strings like this:

~/__Home Folder
~/Pictures/__Pictures Folder

You can use a text file for the prompt list to make maintenance easier. (I use BBEdit for such things Keep in mind that BBEdit reverts to a still very powerful lite version after a set demo period.)

I have a move to folder macro set up this way.


Just be aware that to use a Variable for the dropdown list you need to put an extra | in front of it to tell Keyboard Maestro to treat it as a dropdown list.


1 Like

Thanks. That would probably have given me trouble. (and caused me to mentally yell at the so-and-so who gave me the tip) :grinning:

Another good idea. Thanks.

Thanks a lot @Zabobon @Nige_S @ccstone

1 Like