Executing macro multiple times with AppleScript?

Hello everyone,

I have written an AppleScript with a repeat function, which pretty much contains the following lines:

repeat x times
   tell application "Keyboard Maestro Engine"
      setvariable "MailSender" to MailAddress
      setvariable "MailBody" to MailContent
      do script "MyMacroName"
   end tell
end repeat

My question is: If this macro is executed multiple times in quick succession due to the repeat loop, will the variables remain the same for the remainder of the started macro, or will the AppleScript change the variables and mess with the other macro instances which are being executed?

If so, how can I prevent this? Is this just a problem with global variables, and can it be prevented with local variables?

Setting a KM Variable is, in concept, the same as setting a variable in any language, with the exception that KM global variables persist outside of the macro or script that sets the variables.

It is hard to offer meaningful help because your script does not make any sense -- a tight loop where the source AppleScript variable never changes.

IAC, as soon as you set the KM variable, it changes to the new value just set.

If you'd like to post a real-world script/macro that actually works, or fails at a specific point, and ask a specific question, perhaps we could be of more help.

No and yes.

No, these variables are global to Keyboard Maestro, and so each executing macro would see the same variables, however yes, because the do script action will wait for the macro to complete (I believe, I haven't tried this in a long time, but I'm pretty sure that is how it is coded). So the loop will not execute "in quick succession", it will execute only as fast as the macro takes to run. In which case you should not have a problem.

An alternative would be to use the with parameter option of the do script command, because that is not shared with our macros, so something like:

tell application "Keyboard Maestro Engine"
    do script "MyMacroName" with parameter MailAddress & ":" & MailContent
end tell

The parameter comes in to the macro as the %TriggerValue% which you'll have to break apart into the two variables, and at that point you will want to use Instance variables so that they are separate for each macro.

But the macros still wont run simultaneously unless you do more work.

1 Like

Thanks for the hints and clarification.

I will post the full AppleScript (and what it does together with the macro) later, but I can say that "most of the time" the procedure works as intended:

  • an incoming mail triggers the macro and passes on variables to process
  • if two mails are arriving at the same time, the script makes sure that each mail is processed only once.

I had to create it this way since I discovered that Apple Mail.app rules don't fire reliably on every mail. I mean, if I create a rule: "Mails matching those criteria: Run Apple Script", I noticed that the script is not run on every incoming mail reliably. Perhaps AppleScript needs some time to process one mail, and the Mail.app rule fails to wait for the next execution?

There seem to be some hiccups when I post 10 mails at once to process, but I will work those out later. It was important to get this working on a few mails first.

Thanks, and write to you soon :wink: