Trigger on Application that is running, but in the background

Hi, I’m trying to run an action on Skype, that starts up, runs in the background and then opens a window in the background, but does not activate that window. I would basically like to minimise this window as soon as it pops up. I’ve tried the ‘launches’, ‘activates’, ‘deactivates’ and ‘while application is active’ triggers, and these only work once the Skype window is select i.e. when the window is activated.
Any ideas on how do to this other than creating a macro that’ll loop continiously in the background?


Unfortunately, I don’t have an answer, but I do have a similar question. I have an application, Papers, running on one computer. I switch to another computer somewhere else, and I want to make sure that Papers is closed. Sometimes I remember to close it, sometimes I don’t. So basically I need a macro that asks:

  • is Papers running
  • has it been inactive for more than 15 minutes
  • then close the application Papers

I can do the third line easily with Quit Specific Application and I think for the first I use a trigger of this application is running. What I can’t figure out is how to test for whether it has been inactive for 15 minutes. Any help would be most appreciated. Larry

Imhinman, I am unclear from your question whether you physically go elsewhere to another computer or use some networked app. If the former, then perhaps you could test for system idle time as suggested here.

I’m talking about being physically away from the computer. I might forget to close the program when I am using it at home and then attempt to open it on my laptop at the office, which can have seriously bad results.

Keyboard Maestro has an IDLE() function that tells you how long the Mac has been idle (ie, no user input via mouse or keyboard).

If your Mac goes to sleep, then Keyboard Maestro will not be able to quit anything, so you need to ensure that your Mac stays awake long enough to tell that the Mac is idle enough to quit.

Assuming you want to wait until the Mac is idle 15 minutes, set the Mac to not sleep for at least say 30 minutes, and then set a periodic trigger to run every ten minutes and if the calculation IDLE() > 15*60 is true, then quit Papers.

The CPU/battery usage of such a slow periodic trigger will be negligible, although if you want bonus points, have another pair of macros:

Trigger on Papers Launches, action Enable Periodic Macro
Trigger on Papers Quits, action Disable Periodic Macro

If you want to consider Papers idle if it has not been in the foreground for 15 minutes, even if the Mac is not idle as such, you can add another macro:

Trigger on Papers launches, activates or deactivates, action Set variable Papers Last Used Time to NOW()

And then your periodic macro can first test if Papers is at the front and if so also update Papers Last Used Time, and then test the calculationNOW() - Papers Last Used Time > 15*60 and quit Papers if that is true.

Thanks, Peter. This is incredibly helpful and I really appreciate your taking the time to explain this.

Hinman Lawrence M.

Thanks Peter for the idea. I tried to put create a macro with the insight. But have failed to make a working macro.

Imhinman - were you able to make the macros? Would you be able to share the actual macros, or the logic and variables used?


I worked some more on this problem. Still unsuccessful.

I have two macros.
Macro 1 launches when TaskPaper is launched. It works when the computer is idle. The part that is not working is the second if statement.
Macro 2 is supposed to set the variable which is used to assess whether TaskPaper has been deactivated. And this macro works.

I am going to share the macros below. Would be grateful if somebody could identify the mistake that I am making. I think I am going wrong where I do the calculation
'NOW() - %Variable%TPDeactivated% > 120


Macro 1:

Macro 2:

Ok, I think I have it working now. Pasting a version of the macro.

I wonder if someone has been able to answer the original question: How do you send commands to a background window with Keyboard Maestro?

1 Like

You cannot send commands to any non-foreground application with Keyboard Maestro, except:

  • Using the Select Menu Item action, which will pull the app to the front, select the menu, and then pull the previous application back to the front
  • Using AppleScript/Shell Script, etc

Edit: As of Keyboard Maestro 8, you can send key strokes (Type a Keystroke or Insert Text by Typing actions) to non-foreground applications, though applications may or may not be willing to accept keystrokes when they are in the background.


Is there a way to send the keystroke or menu command to %Application%2%?

Yes. Select the "Send To" item in the Action Gear Menu:

Sorry JM, I may not be getting it right. But when I do that I still get list menu of running/recent/etc applications to send the keystroke to.

I want to send to %Application%2%, no matter what it might be.

No, you can send to specific applications, but not the previous/last application, or an application selected by any other means except the front application.

Not without creating the action XML and executing it anyway.

Hmm.. that sounds like another lost weekend to me, but okay! Thanks both.