Add an Action to Every Macro in My Library

I would like to add a semaphore lock to every macro in my library. Is this possible, without having to go in to every macro and edit it? Thanks KM Community!

The last time we spoke you wanted to stop two DIFFERENT macros from executing simultaneously. That can't be what you mean this time because if we stopped that, then you could never have one macro calling another. So the only way I can make sense of this question is that you want two copies of the SAME macro from running simultaneously. Is that correct? We might as well clarify the question before we give an answer, otherwise we may give the wrong answer.

I have a detailed answer for one of these two scenarios, but I'm legitimately not sure what your question is yet.

You're right. There are times when I have macro's triggering each other. Usually with the action 'Execute a macro'. It therefore would not make sense to do this. I will just keep adding locks to macro's I know I want locked. Thanks again. My impetus here was to be able to hit two or three triggers, one after the next, and have them execute in turn. But if a macro has macros nested within, I believe the lock would break the nested macro?

I think we're on the same wavelength here. Although I've not tested it, I'm 99% sure if you use the same lock in two macros, in the way you've described, the second one will freeze execution. Yes, making decisions about concurrent execution limitations is something that does force programmers to think.

Your original question was about how to modify all your macros in some way. I wish that question was still valid, because I have some experience with using KM to modify other KM macros. The whole idea of self-modifying code is hilarious and risky. I've done it because KM doesn't support variables for images. But you don't need it so we don't have to open up that can of gremlins.

But even though this case is closed, you should, for educational and entertainment purposes, read this action and see if you can figure out what it does:

If you can figure it out, it will give you some insight into how Semaphores work.

I would advise future readers to try to figure this out before scrolling down to read the answers.

A semaphore lock in a macro that already holds that lock would not stop the macro - it already owns the lock so it would just carry on.

You could add a Semaphore Lock to the front of the action list of every macro using AppleScript. Frankly, this really isn't a particularly good idea tho, it is almost universally unnecessary. But if you really want to, it would be relatively straight forward to write the AppleScript to do it (I'm afraid it's not AppleScript I want to test out on my Mac tho!).

I guess that would be entirely dependent on the skill and experience of the person writing the code.
Making changes to a KM Macro using a script is not something I would advise the novice AppleScripter to do, especially not a script that is making changes to ALL Macros.

I would classify it as a medium to difficult scripting task, and would not undertake it myself without first doing this:

  • Extensive testing on a few TEST macros
  • Making a backup of ALL of my KM macros

What would probably be more useful is a script that would insert a Semaphore Lock Action with the same Semaphore Name in a block of selected Macros. Again, I would not do this without extensive testing.

Hey @Ihventura_Hutch,

Yes – this can be done, as @peternlewis mentions above.

And – as @JMichaelTX mentions it is not a simple undertaking, unless you already know how.

This macro restricts adding the given action to a single macro group (by name), but it would be easy enough to expand that to work with all macro groups.

Mass Adding Actions to Macros in a Named Macro Group

-Chris

2 Likes