Use a variable in a „Prompt for user input“ for the number of loops in a "Repeat Macro"

Hi,

I have to upload files to a website. Every upload consist of a series of several file (.mp4).
In the upload-form, every video of a series has to be named (not the file per se, but the „description“ of the files on the website). The names of the .mp4 should be numbered, like
„Record 01.mp4; Record 02.mp4; Record 03.mp4…“, the rest of the name should be equal (for example „Record“).

Now I would like to use a „Prompt for user input“ to determine for every upload series the quantity of the .mp4-files. This number should be used for the repetitions in a loop.
If one have 5 videos, then the loop should

  • be used for jump 4x (5-1: the cursor is in already in the first row) from one row to the next in the web-form
  • be used to named the files as „Record 01, Record 02. The number/quantity 5 specifies the number in the names (01; 02).

So, is there a possibility to use a variable („number/quantity“) in a „Prompt for user input“ to define the number of repeats in a „Repeat Macro“ and use them for naming?

Hi,

Not sure if I understand correctly what you are looking for, but maybe something like this:


[Test] Repeat.kmmacros (4.0 KB)

Thank you very much, that’s exactly what I need: I have tried yesterday with %Variable%BlaBla%" in the field “Execute Actions Number of repeats”, but it’s your solution to put the variable without the %Variable-Stuff that’s work perfectly!

The thing with KM is that

  1. sometimes things you’d expect to work don’t work, and
  2. often things you’d never expect to work do work :yum:

PS: #1) is not meant as an offense to KM, since most of these things turn out to be logical, after thinking about it.

For me, things work as expected 99% of the time. Most of the remaining 1% is (my) user ignorance/error. The only thing that is hard for me to grasp is the use of functions in the Filter Variable/Clipboard Action.

Read the wiki article Text Fields.

Keyboard Maestro has two very distinct types of fields: text fields and numeric fields. Most text fields are text token fields. Most numeric fields are calculation fields.

They are generally easy to tell apart by their context - if they expect a number, they are calculation fields and otherwise they are text fields.

This is a fairly crucial thing you need to know to use Keyboard Maestro well.

2 Likes

The Repeat action needs a built-in way to ask for a user-specified number of repeats. At the least, the ‘times’ field should support a positive integer varialbe. I’m new to KM and was astonished that the number of repeats field didn’t accept variables. (My workaround uses Applescript.)* I believe there should be a medium-skilled KM user to bridge between super KM experts and typical users. Super experts can figure out something as non-intuitive as the steps above, but few users could. What does clicking the mouse 71 pixels down from its current (unknown-to-me) cursor location do? What is being highlighted in the 100 pixel square? How is that getting ‘n’ into the Repeat action ‘times’ field? In QuicKeys, the option for user-specified repeats is built-in. In Applescript, in one minute, I can write the code that allows a user to enter the repeat number. No mouse movement required. No window visible for 0.3 seconds required.

*Applescript workaround
Start with KM actions, if needed.
Execute an Applescript
Make a repeat - end repeat loop in Applescript (with user entering repeat number, if needed.)
Tell Applescript to call your (separate) KM macro that performs loop tasks.
For actions after exiting the loop, either add more Applscript code or use KM actions.

An advantage of this method is that you can add a delay between loop repeats. You can get that delay from the user, if needed. Short delays can use Applescript’s ‘delay numberOfSeconds’ command. Longer delays need a comparison between the time the loop should end and the current time, with the loop continuing when current time is greater than loop-should-end time. I used this method because I couldn’t get delays to work in a KM repeat with a Pause action.

But it does support a Variable:

From post above:

You may want to review the wiki help page for Repeat action (KM Wiki), and this Macro:
Variable Repeat .

You can do the same with KM. Just insert a Pause action (KM Wiki). If you want, add a Variable "DelayBetweenRepeats" in the Prompt for User Input.

The Move and Click Action was just an example of an Action that you can put in a repeat loop.

You can do it even quicker in KM. Again, no mouse movement is required, that was just an example Action.

Perhaps a simpler Macro will help your understanding of how to use a Repeat action (KM Wiki) with a Pause action (KM Wiki).

Please note the following KM Variables are used:

  • NumOfTimesToRepeat
  • PauseTime

##Macro Library   Repeat Loop with Pause and Prompt


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/c/cbe8009eac71bff56c319801e136b0e001cbd83f.kmmacros">Repeat Loop with Pause and Prompt.kmmacros</a> (4.9 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---


<img src="/uploads/default/original/2X/a/a0feb895b92a35163e276052bd5170b6495c1e44.png" width="510" height="1004">
1 Like

My strong suggestion is to have a check boxes or something similar in the
Repeat action setup window for asking whether the user can the number of
repeats and/or choose the delay for each repeat. Repeat loops are one of the
most important actions within macros. They should be easy to make. I would
never have thought to start with a user input action instead of the Repeat
action. I never would have thought to use the action “Repeat Actions
NumOfTimesToRepeat Times,” because I don’t have that action. Is it something
I’m missing, or was the action built outside of the main program?

I really wanted to make Repeat work. I searched the entire Keyboard Maestro
Wiki: manual:Single Page Version
https://wiki.keyboardmaestro.com/Home_Page that I downloaded. There were
few entries for ‘repeat.’ There was no “Repeat Action” entry. There was no
“Variable Repeat” entry. Is KM Wiki different from the Wiki manual?

I used Pause within a Repeat loop in my macro. The Repeat action totally
ingnored the Pause action. I put Pause in various places within the Repeat
loop, but it never worked. Pause has worked for me in every other situation,
so I cannot figure out why it didn’t work for Repeat loops. Perhaps it was
the solar eclipse.

Sincerely,
Gregory Tetrault

I get this sentiment most often from people coming from QuicKeys or another macro program with a different mindset to Keyboard Maestro.

As described elsewhere, Keyboard Maestro's facilities are designed to be orthogonal to each other. That is, each part is like a lego block (traditional lego, not modern stuff), with each piece doing its task and not combining the features of other tasks. The down side of this is that trivial tasks like "ask me the count and then repeat that many times" are slightly more challenging, but the up side is that the pieces can be combined in any way you want "read a file and repeat that number of times", "prompt me for a number and then repeat three times that many times", etc.

This same policy applies to the larger components of Keyboard Maestro. Macro Groups, macros, triggers, actions, are all independent of each other, as much as possible each has its own responsibilities which don't overlap.

That is the manual for Keyboard Maestro itself (for the application). The wiki is much larger than that. You can search the wiki in the search field at the top right of every wiki page. Also, on each Keyboard Maestro action, you can select Help from the gear menu (top right). Also, you can hold the option key down while selecting from the Insert Action (or other) menus to get help on any action. In version 8, you will be able to use the system Help to search for "Repeat" and a link to the wiki Repeat action documentation is available there too. This would take you straight to the help on the Repeat action:

https://wiki.keyboardmaestro.com/action/Repeat

Which states in part:

The repeat count can be a simple number, or you can use a variable or other calculation in the field (as with most all numeric fields in Keyboard Maestro, simply start typing and the field will expand to make room for more than a number).

And also has a link to the Variable Repeat topic.

As stated, pretty much ever numerical field in Keyboard Maestro can contain an expression, which in turn can be the result of a prompt for information, or can have the value come from some other source. This is part of where Keyboard Maestro gets its power from, this ability to take each independent part of plug them together in whatever way matches your needs - not by me trying to guess how you might want to use Keyboard Maestro.

Post your macro (see How to Post/Upload Your Macro to the Forum) so we can see what you are doing and explain what might be the issue.

1 Like

Now that you know how to use KM Repeat loops, you can create a "template", maybe like my above Macro, and save multiple Actions as one "Favorite Action" using @DanThomas' great KMFAM system:
MACRO: [KMFAM] Favorite Actions and Macros

Then, whenever you need your custom Repeat template, just press F12, type "repeat", and press RETURN. Your Repeat template is inserted into your current Macro, at the current location.

I use this system many times a day, and it makes building KM macros a breeze. Let us know if you have any questions about this.

I never would have thought to use the action ³Repeat Actions
NumOfTimesToRepeat Times,² because I don¹t have that action. Is it something
I¹m missing, or was the action built outside of the main program?

The above question has not been answered.

Greg T

Your example isn’t accurate. Most Lego pieces are identical. Construction is
simple because nearly all pieces connect in the same way. A better example
is an Erector Set. (I don’t know if you had those.) There are (sort of) flat
metal pieces of many different lengths and shapes. The pieces have multiple
holes for small bolts. You can join pieces at different lengths and choose
different angles. If you don’t put the right pieces in the right places,
chose the right holes for the bolts, and get the correct angles for the
cross braces, then the structure is weak or lopsided and can’t support the
winch you want at the top. You can get 90% through a project and realize
it’s not right, and you may have to redo almost every step. You find that
the list of parts was not included. You don’t know if the kit has all the
parts you need. The logic of how to place and orient the parts is not what
you are used to. There is no start to finish instruction set with diagrams.
The manual is for an older set and is incomplete manual. The information
you need is printed in small bits on small cards that are in a large box
that contains thousands of cards with information that mostly is unwanted or
confusing.

That’s one analogy for Keyboard Maestro. I have never used a program like
KM. I do not like going online to get the simplest questions answered. I
don’t like hunting through a big Wiki organized in a way that doesn’t make
sense to me, in order to grab little snippets of info that I may be able to
combine with other snippets to solve a problem or answer a question. I don’t
like searching through blogs to maybe find an answer that works for me. I
don’t like hunting for templates and examples that should have been in the
download accompanied by instructions and explanations and the list of
Actions in each. I no longer like being a beta tester—KM feels like a GitHub
project that has a few primary programmers, some closely associated helpers,
a few dozen users/beta testers, and a few complainers (such as me) who think
to project needs rethinking. This type of GitHub project often stays in beta
for years.

Here is another example of why I find KM dfficult. There is an action called
‘Group’ that I wish to know more about. I select it in the Actions window. I
control-click Group: nothing happens. I go to the KM Editor Help menu and
search for ‘Group.’ I get a list of menu items containing the word ‘Group.’
I search the manual. I get macro groups and smart groups, but no action
called Group. Macros are comprised of actions. So why is it that the manual
has no chapter or section about Actions? Why is it that the manual barely
refers to Actions? Aren’t Actions the Lego blocks? You and others will say,
go to the Wiki. To learn a little about a standard Action? Your manual, or a
thorough Help file, should list every Action and explain what it does. Bonus
points for showing how it typically is used. Double bonus points for
indicating which examples and templates the Action appears in.

I did find out that if I put Group into a macro, I can activate the pop-up
menu in the upper right and select the Help menu item. Of course, the help
is the online Wiki. (What happens when someone needs to work offline and use
unfamiliar actions? He’s out of luck.) I admit that the Wiki entry for Group
is good. I still would like to learn about an Action before I put it into a
macro. A control-click link on the item in the Actions window would be
useful, even if it goes to the Wiki.

Here’s a contrast to KM. Click a cell in an Excel spreadsheet and select
menu item Function… from the Insert menu. A small window appears that lists
every function in Excel. When you select one, you get a brief description of
the function. To get more info, you click a link that takes you to the
proper page of the full Excel Help file that has much information on the
function. The small window can help you set up the function on your sheet
and will display the results when you enter parameters in the small window.
I would be pleased if Actions were treated half as well as Excel functions.

Greg T

Clearly, every complex program could be better, and Keyboard Maestro is no different. And clearly each user is going to struggle with different things.

This sounds more or less exactly like the Insert Action by Name facility:

With the Help button taking you to the wiki.

It would certainly be nice to have some help text in the window directly. Everything is a compromise - which other feature should I have dropped to make that happen? And certainly it would be good to have contextual menus in the Action selector, although I admit I've put more effort in to the Insert Action by Name system, the action selector is more a legacy system, although it is useful for browsing.

Can I ask what you used before that you found easier than Keyboard Maestro?

The Repeat action is a built in action, in the Control Flow category.

OK, let me try to answer your question.

The initial Repeat action (KM Wiki) looks like this when you insert it:

Let's start with a NEW Macro:

Now, in the "No Action" block (which means there has NOT been an Action inserted there), I'll insert a Repeat Action from the Actions panel, by double-click, or drag/drop:

So now, my Macro looks like this:

and it defaults to a Repeat of "1" times. You can change that to a different number, OR, you can enter a KM Variable into the number field. Granted, how would you know that you could do that? Well, ALL Actions have a Help menu item in the Gear menu:

When I click on the Gear menu, I see the above, and then if I click on the "Help" menu item, I get the KM Wiki Help article:

From that simple Help article, you learn that you can use a variable in most numeric fields.

Does this now make sense?

So, now, I can enter a KM Variable in the Repeat Action:

I used "myKMVarRepeatTimes", but you could use any variable name you want, including spaces.
But wait! It says "invalid"??? Since you have some programming experience, can you guess why?
.
.
Yep, it is because "myKMVarRepeatTimes" is undefined at this point, so the Action would fail.
How can we fix this?
.
.
Well, I'm sure you've guessed that we need to define and set this variable BEFORE the Repeat Action.
There are a number of ways to do this, but the two most common are:

  1. Set Variable to Calculation action (KM Wiki)
    .

    .

So now, after I run/trigger my macro, the "invalid" is removed:

Does it now make sense how to use Repeat and Prompt Actions together to use a variable in the Repeat loop?

Earlier you mentioned how you found AppleScript easier to use. So, let's examine the corresponding AppleScript statements.

We have 3 AppleScript commands that are all independent of one another, but can be used together, just like in KM:

--- First, we have a  Repeat Loop ---
repeat with i from 1 to 10
end repeat

--- We have a set variable command ---
set myRepeatTimes to 10

-- We have a Prompt command ---
set oAns to display dialog "Enter the Repeat Times" default answer "10"
set myRepeatTimes to (text returned of oAns) as integer

--- If we have either of the above commands that set "myRepeatTimes",
--   then we can use the Repeat like this:
repeat with i from 1 to myRepeatTimes
end repeat

Does this make sense?
Do you see the analogy between AppleScript and KM?
Note that the AppleScript Repeat does NOT have a parameter to prompt for input. You have to do that in a separate statement.

I hope this answers your question, and at least some of your concerns about KM.
If you have more questions, please let us know.

3 Likes

“Well, I’m sure you’ve guessed that we need to define and set this variable
BEFORE the Repeat Action.”

Your technique for doing anything complex reminds me of Reverse Polish
Notation where all numbers are entered before the operators. 3 (enter) 5
(enter) plus (press) equals (press) and 8 appears on the display.

“Note that the AppleScript Repeat does NOT have a parameter to prompt for
input. You have to do that in a separate statement.”

Thatt’s true if you start from scratch. But, you can save the code below.
You paste it into a new script, and the only code you add are the “do
whatever” steps. This is far easier than what you describe below. I can use
this within a KM macro and add more Applescript or call a different KM macro
for the “do whatever” tasks.

display dialog “Enter number of repeats.” default answer “2”

set numRepeats to text returned of the result

display dialog “Enter delay in seconds.” default answer “0”

set delaySecs to text returned of the result

repeat numRepeats times

– do whatever

delay delaySecs
end repeat

The more I look at KM, the more I realize that, for me, anything complex is
best done with Applescript with KM adding some features and providing the
ways to launch the Applescript-containing macro (hot key, status menu, macro
palette, etc.) that aren’t easily done with Applescript. I expect to pick up
more knowledge of KM as I go along, and I will rely more on KM actions. I am
not learning KM as quickly as I wish. As I said before, I’m a book and
manual person, and, for software, I like an approach that is task-focused.

Because of my approach, I won’t get as frustrated if I cannot figure out an
action or how to string actions together. Therefore, I won’t bother
JMichaelTX and Peter N Lewis as much. (Yay! you say.)

Greg T

I have to disagree. KM is one of the best, if not the best, Mac automation tools available. KM is designed to allow people without any programming experience to easily automate workflows, from the very simple to very complex. Like any app, or programming language, you have to learn the terminology and the basics to be productive. I believe KM is much easier to learn than AppleScript. AppleScript offers none of the built-in help features you mentioned earlier.

I can see that you are very frustrated with KM, but I urge you to put your frustration aside, and try to learn KM. It is very powerful and flexible, and you will be well-rewarded for your efforts.

I'm curious, have you been through the KM Getting Started topics?

Getting Started with Keyboard Maestro

  • Read the Quick Start.
    • This is essential to become familiar with KM terminology
  • Do the tutorial (Help ➤ Tutorial) in the KM Editor.
    • Gives you a live walkthrough of creating a macro in the KM Editor
  • Review/Browse the Available Macro Actions
  • For Help with an Action in your Macro, click on the gear icon at the top right of the Action, and select "Help"
  • Search the Keyboard Maestro Wiki for detailed help.
  • Start small, and grow your macros organically.
  • Be prepared for some trial and error in the beginning.
  • Make good use of this Keyboard Maestro Forum
    • Search for existing macros
    • Post your questions/issues if you get stuck

Good luck.

1 Like

If you are a confident AppleScripter, this may well be a good solution (or starting point anyway) for you. We all look at problems with the lens of the solutions we have used in the past, so if you can easily and comfortably write AppleScripts, then that fine, let Keyboard Maestro do the bits that are difficult in AppleScript. I have a fair amount of AppleScript commands in Keyboard Maestro that are designed to expose the Keyboard Maestro functionality to AppleScripts (for example, the process tokens command gives you full access to all the information Keyboard Maestro has with Tokens).

Not getting frustrated is always good, but it's no bother (to me, and I doubt to @JMichaelTX either) - the only frustration we have is in our inability to help you get the most out of Keyboard Maestro.

You absolutely have some valid points about Keyboard Maestro's UI - some of them I simply haven't got to improving, some of them are very difficult, some of them are historical, and some of them have a flip side that explains their motivations. But none of that removes the value in the observations.

Regardless of how you use Keyboard Maestro, I hope you continue to get value out of it. And as an AppleScripter, I hope you stick around on the forum because there are often questions on how to do things in AppleScript.

3 Likes