How to avoid Infinite loop?

This is the macro I made to automatically copy. When I select texts, it will auto copy. But when it came to the image, I had to copy the image by right-clicking on it. so I use the system clipboard changes to trigger palette. this will result in an infinite loop, So how to avoid the infinite loop.

auto copy Macro (v10.1.1)

auto copy.kmmacros (4.0 KB)

No one has encountered this problem, there should be a condition that can only be executed in the forward direction, but not in the reverse direction

it may be a bug. when displayed clipboard, It display 7 same clipboard.

Hey there, one glaring issue is that you have a trigger for when the system clipboard changes... and the very first action is ⌘C to copy, which again changes the system clipboard, which is the trigger, which executes the macro again, and copies again, and triggers again and so forth.

Generally you do not want to use a copy action inside a macro that has this trigger because it will get stuck in an endless loop. I'll take a closer look at it tomorrow and see if I have any ideas for you.

Thank u.
This is simple example.
Is there a way to prevent triggering from within?

test.kmmacros (2.8 KB)

You could maybe use an if action in conjunction with the %TriggerBase% token to copy only if the trigger that executed the macro is not a clipboard change. See the screenshot for what I'm referring to.

If action with %TriggerBase% token screenshot (click to expand/collapse)

2 Likes

Thank u.
it still displays two same clipboard.

Just put a Semaphore Lock action with a short timeout at the start of your macro. This will prevent your macro from retriggering itself when it causes the clipboard to change.

Here's your original macro with the necessary modification:

auto copy v2.kmmacros (4.4 KB)

Keyboard Maestro Export

You should read the KM wiki page to understand why this happens/works. See https://wiki.keyboardmaestro.com/action/Semaphore_Lock

2 Likes

Thank you very much!
It can work, but I encountered two problems. Here is a simple test example.

  1. I need to wait a long time, about two seconds, before I can execute the macro again.
  2. When I copy the image by right-clicking, it always works the second time.
    test_1.kmmacros (2.9 KB)

The first thing I did when checking this latest macro you posted was to look at the semaphore settings. They are:

KM 0 2022-08-25_10-30-05

This does not correspond to what I told you to do in my previous post.

The semaphore settings must be as follows:

KM 1 2022-08-25_10-30-45

Make the change and then try again - if you'd read the wiki page and my post carefully about the semaphore settings we wouldn't be having this discussion.

1 Like

ok thank you.
I neglected to set the conditions. I find the answer to my question by comparing my macros with yours.
I'll go read the wiki page again. Since English is not my first language, it may be difficult to read, but I will try to understand it.

Well - do give it a try. If there are other questions feel free to ask :smile:

1 Like

The first thing to understand is that this isn't an "infinite loop". It looks like one, but what you were actually doing is repeatedly spawning new instances of the macro, one per clipboard change. That's why @tiffle's "Semaphore lock" works -- it says "if I am already running, do not create another instance of me". That's also why you have to wait until the macro completes before you can run it again.

I won't pretend to understand why you want to do what you are trying to do -- but, perhaps, another approach would be better?

It sounds like you either have:

  1. The pointer over some already selected text or
  2. The pointer over an image

...and you'd like to use a macro to copy either text or image then "do things". You can play on the fact that if no text is selected and you do ⌘C the clipboard doesn't change. But, rather than using a clipboard trigger, compare before and after CLIPBOARDSEED() values (that changes every time the clipboard changes).

If CLIPBOARDSEED() changes then you copied some selected text -- carry on. If it doesn't then you had no text selected, assume the pointer is over an image, do a "Copy Image", and carry on. Something like this (tested in Safari, change the trigger to suit):

Clipboard Test.kmmacros (4.7 KB)

Image

The gurus here will come up with better ways to do the "Copy Image" step, I'm sure -- this was quickly hacked together...

2 Likes

Thank you for your reply.
Perfect!
I didn't quite understand it at first, but it wasn't until I downloaded the macro to test it that I found it worked well.
Thank you all for giving me different opinions, I can learn a lot