Macro stop after a certain number or cycles

Hello everyone,

I was reading this forum which asked the question why his macro stops after 25 cycles. I have the same problem and I found out that Keyboard Maestro has a safety feature which will cancel all macros if you get to the point of having 50 sub-macros all executing at the same time.

I realized that if I have one function, then my macro runs forever. However, if my macro calls another macro, then it stops. For me, it's easier to make different functions (macros), since I split my work and it's more organized instead of putting all my macros into one function.

How do I adjust the settings so that KM does not stop my macros after 50 sub-macros? I cannot find it in the settings.

My Macro.kmlibrary (32.8 KB)

Thanks in advance.

@peternlewis, If you could help, that would be great.

So, to be clear, it only stops where there are 50 macros running simultaneously.
Sorry to be blunt, I doubt that you really need this, and that it is a design issue. But I'm willing to help.

If you would be willing to provide us with a reasonably detailed workflow that illustrates what it is that you need done, and show why you think you need all of these macros running at once, then I (and likely others) would be willing to take a look at it and maybe make some design suggestions.

Also, I don't really want to install your entire Macro library just to get a picture of it.
Could you please upload images of your Macro list, and the main driver Macro that is calling these 50+ sub-macros?

See post/upload your script/macro (files and/or images).

As @JMichaelTX ever so gently puts it, changing this setting is pretty much never the right answer.

It is there for a reason, it indicates you have lost control of your macros. And it is also a performance issue, each running macro takes up a significant amount of Keyboard Maestro Engine’s brain (CPU, Memory, etc) and just endlessly generating more is never good to be a good thing.

I looked at your macros (please post them using the Share to Keyboard Maestro Forum button at the top of the editor, or as exported macros, not as an exported macro library, which really is only there for cases where you want to be able to add something to the Keyboard Maestro Macro Library which you almost never do).

Every case you use Execute Macros in those, the Execute Macro is the very last executed action of the macro. Therefore, just set the Asynchronous setting on the Execute Macro - that will basically tell it to start but allow the current one to continue, which will promptly complete.

Sub-macros are a great resource for keeping your processes understandable. In this case, there is no need to use them for looping at all. You current structure is:

  • Request Review: Start:
    • Prompt
    • Execute Macro “Request Review: Loopy Loop”
  • Request Review: Loopy Loop:
    • Set Simulate Normal Keystroke Delay for This Macro
    • Copy
    • If condition
      • Cancel All Macros
      • Note that the Play Sound “Purr” will never execute because you canceled all macros including this one
    • Otherwise
      • Type stuff
      • Pause
      • Execute Macro “Request Review: Click Request Review”
  • Request Review: Click Request Review:
    • Do stuff
    • If condition
      • Do stuff
      • Execute Macro “Request Review: Loopy Loop”
    • Otherwise
      • Do stuff
      • Execute Macro “Request Review: Loopy Loop”

So “Request Review: Click Request Review” always continues from the start of Loopy Loop, and Loopy Loop always repeats unless it calls Cancel All Macros. So you can simplify the whole thing to:

  • Request Review: Start - unchanged
  • Request Review: Loopy Loop:
    • While calculation 1 (ie, always)
      • Set Simulate Normal Keystroke Delay for This Macro
      • Copy
      • If condition
        • Cancel All Macros
      • Otherwise
        • Type stuff
        • Pause
        • Execute Macro “Request Review: Click Request Review” (not asynchronously!!!)
  • Request Review: Click Request Review:
    • Do stuff
    • If condition
      • Do stuff
    • Otherwise
      • Do stuff

No need for recursive Execute Macros at all, no need for asynchronous Execute Macros either.

Hi Peter (@peternlewis),

Thanks for responding a thoughtful response.

I tested Asynchronous setting on the Execute Macro with my old script and it seems to be working just the way I wanted.

I have lots of scripts that are recursive and they had the same problem which was that they cancelled when 50 macros were running simultaneously. My solution to that problem is to turn asynchronous setting on since it would be more efficient instead of rewriting the whole code.

My question to you is will Asynchronous setting on the Execute Macro have performance issue? The reason why I ask is because I know each running macro takes up a significant amount of Keyboard Maestro Engine’s brain. Will Asynchronous macro take a chuck out of my CPU or will it have no performance issues whatsoever. In theory, I want to leave my laptop running overnight to work on some humdrum task.

Thanks in advance.

-Kenny

As log asn you ensure that the Execute Macro action is the last action of the macro (ie, that no other actions will execute after it in that macro, so you can have an If Then Else action with an Execute Macro action at the end of each Then and Else lists for example), then no, there will be no performance issue. The calling macro will complete immediately after and be removed from the running list, and the sub-macro will carry on.

I will try my best to always have the Execute Macro action as the last action of the macro. However, for some reason, if I am unable to do so and I must have Execute Macro action as not my last macro. What should I do?

Thanks in advance.

If you need to execute the submacro, wait for it to finish, and then proceed with more work, then you will not be able to use Execute Macro asynchronously. Also, any sub-macro will similarly not be able to use Execute Macro asynchronously.

So you're only solution at that point would be to rework the macros as originally suggested.

Hi @peternlewis ,

How do I ensure that my macro does not skip any steps? I have to leave my laptop running overnight to do the tedious manual work of requesting review. I have disabled screensaver to ensure KM would run smoothly.

Thanks in advance.

-Ken

The macro will never miss any steps.

The macro will be canceled if you have more than 50 simultaneous macros running as described previously - avoiding that is necessary.

Other than that, the macro will continue to execute the steps. However the system may process the steps more slowly, and if the system gets behind or goes to sleep, then the steps may not function correctly (eg keys may be lost).

To avoid that, you need to ensure you don't allow the system to sleep the screen, screen save the screen, or go to sleep, and you need to ensure the macro does not run sufficiently fast that the Mac falls behind (eventually the event queue will fill up, or the events will be processed by the wrong window).

Hi @peternlewis,

Thanks for your response. I greatly appreciate your advice since I'm using Keyboard Maestro to automate my mundane task at work and unfortunately learning Selenium/Scrapy is a huge learning curve for me.

I have two follow up questions if you don't mind me asking.

Question 1: You stated that in order to prevent a performance issue, I should either set my macro as asynchronous or do a while loop. What if I need to do recursion and I cannot have asynchronous setting on because I have to wait for the recursion macro to finish. From my understanding, asynchronous will run both macros at the same time. Unfortunately, I need one recursion macro to finish loading before it execute itself again. Is there a possible way without using a while loop?

Question 2: How do I ensure the macro does not run sufficiently fast that the Mac falls behind. Do I solve this by setting the "Set Simulate Normal Keystroke Delay" to 0.2 or is there another solution?

Thanks for your thoughtful input and thanks in advance.

-Ken

To add more input for question 2, I have caught KM skipping steps.

I created a macro that determines if a keyword exist on a webpage.

  • Macro: check if string "confirmed" is on webpage:
    • Set Simulate Normal Keystroke Delay for This Macro to 0.2
    • Simulate keystroke: ⌘L
    • Simulate keystroke: ⌘C // location bar is copied to clear the clipboard
    • Simulate keystroke: ⌘F
    • Inset Text "confirmed" by Typing: confirmed
    • Simulate keystroke: Escape
    • Simulate keystroke: ⌘C
    • If System Clipboard contains “confirmed”
      • Play Sound “Confirmed Located”
      • Otherwise:
        • Play Sound: “Error: Confirmed Not Located”

For the majority of the time, the macro works great. However, every now and then, my macro would play Sound: “Error: Confirmed Not Located” when there definitely was the word "confirmed" on the webpage.

Sorry my question is long and once again, thank you so much.

Hey Kenny,

Never use keystrokes when commensurate menu items are available.

Never use Cmd-C to mimic Copy. Use the Copy action instead. (It has a built-in pause to allow the clipboard to catch up.)

Keyboard Maestro tends to be faster than the system, so you often have to insert pauses or pause-until actions to let the system catch up.

All sorts of hang-ups are possible with the system and with various software, and sussing out all the pitfalls can be quite challenging at times.

-Chris

Hi ccstone,

I used the copy action but for some reason the copy action does not work.

I created a demonstration below. I have noticed that my macro works if the "if condition" is true, but my macro does not work if the "if condition" is false. For example if you set the Wikipedia page to "Trees" then my macro will prompt that they keyword "tree" is found. However, if you use a random article such as "Telephone", my macro will not prompt anything when it should prompt "No Trees Found". I included my video for more clarification.

Happy Holiday and thanks in advance!

-Ken

Recursive macros are fine - that is how they are intended to be used. Asynchronous execution should be by far the uncommon case, almost never needed.

The issue all along has been using Execute Macro to loop. Execute Macro is not intended as a looping mechanism. If you avoid doing that, you would not have any trouble.

By adding sufficient Pause or Pause Until actions as required to ensure the system is keeping up. The Set Action Delay action is a possibility, but it is a sledgehammer - it should generally not be used. Understand where you macro causes the system to take time to perform an action and deal with that delay.

This accomplishes nothing much since your Simulated Keystrokes are mostly all command keys, not “Normal Keystrokes”.

This is the action that changes the keyboard focus. So, as described above, this is a location where a Pause may be necessary. If the Find window comes up in a new window, use a Pause Until action with a Front Window condition to detect that the Find window has appeared. Alternatively, you could perhaps detect that the Copy menu is no longer active (since it should be after the Command-L). Or just a fixed Pause.

I don't know what this does or what happens after this, so it is hard to suggest much.

Thanks Peter! I will have of editing to do tonight for my macros.

Hey Ken,

When posting such things please post the macro in addition to the image, so people who are interested have something to test with. You'll get more help that way.

It's very helpful to see what you're actually doing. Words cannot take the place of images/videos for such things – however it's still desirable to be able to test locally.

If you haven't read this it's worth a couple of minutes of your time.

Tip: How Do I Get The Best Answer in the Shortest Time?

You're going about some things less efficiently than you could:

Working with Google Chrome.kmmacros (7.0 KB)

It's not necessary to use Chrome's Find command, as you can see in the above macro.

However in cases where this would be needed you should Insert Text by Pasting rather than Insert Text by Typing action, because each typed letter in Chrome is a separate find action and slows things down.

HTH.

-Chris

@ccstone

I have went through the tip and will be posting my macro with an attachment next time.

Thank you so much! I've been using KM for 2 years and the whole time I've been using Command F in order to determine if a certain text exist. Your macro is much more efficient. Can you point me to the right location where I can find documentation for using JavaScript with Keyboard Maestro? I would love to know what else I can do with Javascript and KM.

Once again, thank you ccstone, and thank you @peternlewis. You guys made my job substantially easier!

Happy New Year!

-Ken

Hey Ken,

You can use the Chrome-only action: Execute a JavaScript in Google Chrome action

Or the Front-Browser action: Execute a JavaScript in Front Browser action

I prefer the latter, because it works for both Safari and Google Chrome – and I can write one macro that will work in either browser.

This isn' always convenient or necessarily possible, but I like to do it when I can.

(There's a Execute a JavaScript in Safari action too of course.)

If you can run JavaScript from the Console in Safari or Chrome then it will probably run in a Keyboard Maestro Execute JavaScript action.

There are many very convenient things you can do with it, and it turns your web browser into a power tool.

-Chris

Hi Everyone,

I am supposed to leave my macro running overnight but for some reason my macro usually stops after 45 minutes. I did my due diligence and checked for every issue I could think of. I checked my timeout on my macro and it says 99 hours so I know that's not the issue. In the past my macro would recurse itself with "Asynchronous Setting" on. I recently changed my macro to use while loops since I've been told that "Execute Macro is not intended as a looping mechanism". After some test trial, the macro still suffers the same problem and sometimes stops after 45 minutes (sometime it goes on for over an hour). I am unable to figure out why.

Unfortunately I am unable to post pictures of my macros here since every time I do, it says "Multiple Sections". So instead, I used imgur.

KM Version 8.2.4
Work: Request Review v17 Macros.kmmacros (46.0 KB)

Thanks in advance.

-Ken