If Then Variable Condition - Check Multiple Variables

I have 43 questions each of which is set to Q1-Q43 variable and requires user's input (1-3).

How can I create a condition whereby if any of the questions has a repose of "0" a condition triggers?

Any way to do this without creating If Then condition 43 times?

Here's an untested idea, but an approach I have used before.

  1. Put your questions in a single KM variable (or a file), one question per line
  2. Use a KM For Each action on a lines collection in that variable
  3. Use a variable to track the count, and build the KM variable name for each answer dynamically (or append each answer to a single variable, one line per answer).
  4. Then you just need one IF/THEN in the For Each loop to test for your condition.


This is where dictionaries come in very useful:


Maybe in using the questions, but getting the questions into the Dictionary could be painful and verbose. Then, even more effort just to view/edit the questions. That's why I went with using a simple text file as the source for the questions. Easy to create and maintain.

That's valid. I guess, since I utilise dictionaries a lot, I've created a bunch of macros/AppleScripts to make creating, editing, viewing and deleting them easy/easier. I probably agree that your method is better for most people who haven't done this, but I don't think there's any harm in providing an alternative way of doing things.

But, yes, your method does get my vote.

This is easily done with the For Each action and the Lines In collection to iterate over the variable names, and the Filter action to get the “Value of Named Variable”. Something like this:

If the variables have consistent names (eg Q1, Q2, … Q43), then you can use the Number Range collection and set VarName to Q%Variable%Index%:

You can do as I suggested here:

But I also want to address a question you did not ask: How Do I Create and Maintain the LIst of Questions.

As far as I can tell, all of the above solutions pretty much ignore this question.
So I have built a solution that uses a plain text file for both the Questions and the Answers.
This makes it easy to enter, review, edit, and maintain. I don't like using a lot of KM Variables or Dictionary Keys for lists of items. KM just does not provide the tools to make entry/edit of these easy.
And, with a text file you can easily share with others for their review/input.

So here's my macro:

Example of Question in File (show in BBEdit)


(Note I am showing the "invisibles" in BBEdit so you can see TABS, SPACES, and LF)

Download the Question File:
Questions for KM.txt.zip (317 Bytes)

Example Question Prompt


Example Answers (not correct)


MACRO:   Prompt for Answers to List of Questions [Example]

~~~ VER: 1.0    2019-05-07 ~~~


Prompt for Answers to List of Questions [Example].kmmacros (14 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.




  • Prompt for Answers to List of Questions
    • Both Questions and Answers may be stored in files.


  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Trigger this macro.


  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
  1. Assign a Trigger to this maro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro.
  4. Create plain text file of questions in the same format as the Set Variable to Local__QuestionsList below
    • Then ENABLE these Actions:
      • Set to File of Questions
      • Set File for Answers
    (all shown in the magenta color)
    • DISABLE List of Questions for TESTING
      • OR, modify as you wish for testing.


  1. KM 8.2+
  2. macOS 10.11.6 (El Capitan)

TAGS: @Survey @Questions @RegEx


  • Any Action in magenta color is designed to be changed by end-user


  • To facilitate the reading, customizing, and maintenance of this macro,
    key Actions are colored as follows:
  • GREEN -- Key Comments designed to highlight main sections of macro
  • MAGENTA -- Actions designed to be customized by user
  • YELLOW -- Primary Actions (usually the main purpose of the macro)
  • ORANGE -- Actions that permanently destroy Variables or Clipboards,
    OR IF/THEN and PAUSE Actions


  • While I have given this a modest amout of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.


1 Like