Execute Macros in the Background (and Meanwhile Use the Computer)

I think this isn’t possible right now, and perhaps will never be possible due to the nature of KBM. Still, the question:

Is there, or will there ever be, a way of having KBM execute macros “in the background”, i.e. on some virtual screen perhaps, while I, as the human, can still use my Mac without interfering with the macro execution?

Example:

I’ve got a macro that opens a browser window, goes to a URL, clicks some buttons there, copies text to the clipboard, closes the window, etc…

Right now, while that macro is running, I can’t use my Mac. I can’t have other applications in the foreground, and if I even were to move my mouse or do something on the keyboard, then I’d by interfering with KBM, and KBM would be interfering with me. So it’s either me using the Mac, or KBM using the Mac – but never both of us at the same time.

Some time ago, I actually used an old iMac I had just for executing KBM macros, while I was working on my own Mac.

Is there any way of doing this virtually? I.e. having KBM execute its macros in some virtual desktop environment, that does run on my computer’s CPU and uses its applications and files, but that does not interfere with the desktop and the keyboard and mouse that I am using as the user?

That being said, I love KBM – and even if it does “interrupt” my work, I know that it is saving me tons of time, doing like 10x the stuff in the short “interruption” than I ever could. It’s an awesome piece of software - perhaps even the most awesome piece of software I have on my Mac.

Hey Leonard,

You must understand that much of what Keyboard Maestro does is accomplished by “driving” the user-interface.

When Keyboard Maestro has taken the wheel you cannot take it back without causing a wreck.

But…

I believe Keyboard Maestro 8 supports some background events, so a few things can be done to an app in the background.

There are also a number of ways and means to do many tasks that don’t require Keyboard Maestro to “drive”.

For instance — it’s possible that your browser task could be accomplished with Safari (or Chrome) and AppleScript.

If a web page can be directly access it is possible to get it’s text with AppleScript, Curl, Lynx, or other means and parse it.

You’re never going to get the perfect environment where you can use all of Keyboard Maestro’s tools without letting it “drive” some of the time, because Apple and app developers will never expose all features to scripting.

-Chris

1 Like

As @ccstone says, KM8 does indeed now allow for keystrokes to be sent to apps in the background: https://wiki.keyboardmaestro.com/action/Type_a_Keystroke
But as the page says, since most apps don't expect to receive input in the background, how well it works in practice could vary drastically.

The only way to truly ensure that your macros don't interfere with your own ability to use your Mac is to write them in such a way that they don't depend on simulated UI interactions like keystrokes and mouse clicks, which usually means resorting to scripting. Depending on how familiar you are with scripting this can feel like a tall hurdle to overcome, but as someone new to it myself, I can tell you that even a little knowledge can go a long way towards the kind of automation you describe.

As an example, here's an old version of a macro I used to use frequently that would manually copy text from BBEdit, paste it into Excel, and switch back to BBEdit:

This macro saved me a lot of time and manual typing, but it was also prone to errors, usually when Excel would be slow to activate and throw off the rest of the macro, and even when it worked correctly it prevented me from working in BBEdit while it was running. I imagine this is not unlike the scenarios you've been running into yourself.

By comparison, here's the macro I use now:

As you can see, the initial steps are unchanged, but the latter half has been entirely replaced by an AppleScript that is faster and more precise then the old macro ever was, and it doesn't interfere with my own use of my Mac at all beyond the initial copy and forward delete actions (which are so quick that I couldn't interfere with them even if I wanted to anyway) thus letting me continue to use BBEdit even as the data in the Excel document changes. While this version admittedly took a lot more reading and experimenting to set up properly than the old one, it has also more than paid that effort off in how much better it works.

Hopefully this gives you an idea of how scripting can help you to better automate your Mac like the way you describe. If, like me, you've also never dabbled much in AppleScript before and aren't sure where to begin, I would point you towards @ccstone's own References for Learning & Using AppleScript and encourage you to ask questions here, as there are several experienced scripters here whose expertise far surpasses my own and have been incredibly helpful in providing assistance and in-depth examples (such as @JMichaelTX, who just recently demonstrated how to use AppleScript to safely delete a sequence of macro actions).

For the sake of completeness, I would be remiss in not mentioning JavaScript for Automation, or JXA, another method of scripting Mac apps that can be used natively in KM just like AppleScript. A lot of more experienced coders seem to prefer it for numerous reasons, and if you're starting from scratch with scripting you may be better served by focusing your efforts there. Personally, as someone new to scripting with zero prior programming experience, I find it much more intimidating and far harder to read than I did AppleScript, and I've been more than happy enough with what I've learned of the latter so far that I don't feel a need for it, but as I said, I would be remiss in not at least mentioning it.

1 Like

This sort of thing is clearly not going to work well in the background. For one thing, it will mess with your clipboard, and there is only one of them, so even if you could manage to do the rest in the background, that would be a problem.

If you can drive the whole lot with the Safari Browser Actions, then you could probably do it in the background.

Otherwise, you could possibly create another account and Fast User Switch and do it there, but I suspect you can;'t do any UI actions in the other account when it is not active (just like you can't when the screen saver is running).

Alternatively, get yourself an old Mac and use that like you did, that could work.

So there are ways to work around it, but not ways to just have Keyboard Maestro operate with UI but in the background generally.

1 Like

I thank you all for the great answers.

What I hear is that I can achieve my goal as long as I circumvent mouse activity, keyboard activity, clipboard activity and reading images off the screen. Ways to do this are the Browser Actions (just Safari, or Chrome as well?). And, of course, Apple Script.

So far, I have not used Apple Script at all, because I’m not familiar with the “language”. But @gglick’s example encourages me to dip my toes into it.

Otherwise, the easiest option is to just use a second Mac for KBM.

(They call something a “Killer App” if it makes you buy a device just so you can use that app. So how shall we call KBM, if it makes me get a SECOND device exclusively just for it? :slight_smile: )

1 Like

I'm glad to hear my example was encouraging! Good luck with dipping your toes in the AppleScript waters, and again, feel free to ask questions here if and when you run up against a wall. For my part, while I may not have nearly the expertise as some of the other scripters here, I'd be happy to offer what help I can from the perspective of a fellow AS beginner :slightly_smiling_face:

KM does indeed have actions for Chrome as well as Safari (as well as a new set in KM8 that will act on both depending on which one is in the foreground) and they can go a long way towards improving automation involving websites.

One last tip: another way to circumvent clipboard activity is to use variables instead of the clipboard whenever possible. While it's best if you can get the data you want through a script or browser action, even a single initial copy operation can work with minimal interference if you then set the copied data to a variable, process it there, and only involve the clipboard again at the end when you paste the result. Depending on your task you can also improve things by copying a large set of data and processing it entirely in variables, rather than copying multiple data points in succession. This kind of automation is entirely doable in KM without any scripting knowledge whatsoever, and can also be a big step up in reducing macros' potential to interfere with the UI.

:laughing::+1:t2:

It’s not KM that makes you get something for it. It’s rather the conditions as imposed by the OS, I think. Anyway, I would call it “Mega Killer App”, for obvious reasons :wink:

1 Like