Macro stop after a certain number or cycles

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

Is there any indication of the macro being aborted in the Engine.log file? Help ➤ Open Logs Folder.

Hi Peter,

I provided the log below. The error seems to be located here:

2019-01-04 10:42:27 Launch Task /bin/sh -c /usr/bin/osascript '/var/folders/hs/88m7zsvx3591hjv5fvjkxv6r0000gp/T/Keyboard-Maestro-Script-840D0E40-E90B-46D1-826C-9BF8B9CF8625' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

I did a quick Google search which lead me to this older post which had the same problem. I used the terminal and it revealed this.

I'm not sure what to do now. I have tried restarting my computer many times. Thank you.

-Ken

2019-01-04 09:40:13 Execute macro “main” from trigger The Hot Key ⇧⌘R is pressed
2019-01-04 09:40:25 Execute macro “disable main macros” from trigger The Hot Key ⌘Escape is pressed
2019-01-04 09:40:32 Execute macro “main” from trigger The Hot Key ⇧⌘R is pressed
2019-01-04 09:42:00 Execute macro “disable main macros” from trigger The Hot Key ⌘Escape is pressed
2019-01-04 09:42:31 Execute macro “main” from trigger The Hot Key ⇧⌘R is pressed
2019-01-04 09:58:09 Execute macro “disable main macros” from trigger The Hot Key ⌘Escape is pressed
2019-01-04 09:58:48 Execute macro “main” from trigger The Hot Key ⇧⌘R is pressed
2019-01-04 10:42:27 Launch Task /bin/sh -c /usr/bin/osascript '/var/folders/hs/88m7zsvx3591hjv5fvjkxv6r0000gp/T/Keyboard-Maestro-Script-840D0E40-E90B-46D1-826C-9BF8B9CF8625' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9
2019-01-04 10:42:27 Task failed with status -1
2019-01-04 10:42:30 Launch Task /Applications/Keyboard Maestro.app/Contents/MacOS/Keyboard Maestro Engine.app/Contents/MacOS/CompileAppleScript string failed
2019-01-04 10:42:30 Launch Task /Applications/Keyboard Maestro.app/Contents/MacOS/Keyboard Maestro Engine.app/Contents/MacOS/CompileAppleScript string failed
2019-01-04 10:42:31 Launch Task /Applications/Keyboard Maestro.app/Contents/MacOS/Keyboard Maestro Engine.app/Contents/MacOS/CompileAppleScript string failed
2019-01-04 10:42:31 Launch Task /Applications/Keyboard Maestro.app/Contents/MacOS/Keyboard Maestro Engine.app/Contents/MacOS/CompileAppleScript string failed
2019-01-04 10:42:31 Launch Task /Applications/Keyboard Maestro.app/Contents/MacOS/Keyboard Maestro Engine.app/Contents/MacOS/CompileAppleScript string failed

The error you show in the terminal looks like curly quotes instead of plain quotes, I doubt that is related.

The error “Failed to set posix_spawn_file_actions for fd -1 ” looks like the system failing to spawn a posix file. Probably this is caused by overwhelming the system process table. You may need to reconfigure your system to allow more processes, or alternatively you may need to allow a larger pause in the middle of the operation to allow the system to clean up after itself.

Hi Peter,

Thanks for getting back to me. I added a 3 second pause and it seems to be pausing after an hour. Instead of adding a larger pause, it would be more efficient to reconfigure my system to allow more processes. I did a Google search and was unable to find any documentation on reconfiguring my system to allow more processes. Could you link me to any guide if you know any?

Thanks in advance.

-Ken

I don't know the details I'm afraid. I don't even know which particular settings my be appropriate, sorry.

Hi Peter,

No worries. I greatly appreciate all the help that you gave me. Thank you so much.

-Kenny