If/then triggers based on which app was used most recently

Hi i’m trying to figure out if there is there any way to set if/then triggers based on which app was opened/used/in the front most recently. Could you please help?

I have a midi-triggered scroll macro set to trigger with an if/then condition based on whether evernote in at the front or not. I set it up like this because i sometimes use the scroll macro with Scrivener instead of evernote. It works great, but sometimes get confusing if i was working in evernote but then go to change a song in iTunes on my other monitor then i hit the button and it opens Scrivener instead bc evernote technically wasn’t in the front anymore- iTunes was.

So I’m wondering- Is there any way to set if/then triggers based on which app was opened/used/in the front most recently? That would solve all of the confusion.

heres a screenshot of the macro and the options i see :

You can test for the previous app (i.e. the app that was frontmost before the currently frontmost app) with the %Application%2% token:

[test] Previous App Condition.kmmacros (2.6 KB)

1 Like

The usual approach is to put the macro in a Group that is activated ONLY when a specific app is frontmost.

You can use the same trigger in macros in different groups.
So, have different Macro Groups for Evernote and Scrivener. Then the midi-trigger would trigger one macro when Evernote is frontmost, and a different macro when Scrivener is frontmost.

Make sense?

If not, please feel free to ask more questions.

2 Likes

I think he was asking for the previous app, unless I misunderstood.

Edit

Yes, after re-reading the OP I understand what you mean. And that’s probably the better approach.

2 Likes

@danklim wants the macro to work even when the application is not at the front, but wants it to operate on the most-recently front of the target applications (Evernote / Scrivener).

The way to do this is to keep track of which of the target applications was last at the front.

In the Global Macro Group, add one macro for each target application, like:

Then in your macros, use the TargetApp variable to decide which action to perform.

1 Like

Thank you all!

Peter, could you please explain what you mean by "Then in your macros, use the TargetApp variable to decide which action to perform."?

I've made the track target application macro for both evernote and Scrivener as you've described:

But not sure how to implement it in my macro. Do i trigger the if/then macro with "The Macro" instead of "The application" scrivener or evernote being at the front?

I just tried that (shown below) but it always says that its true, regardless of whether i open evernote or Scrivener last.

Also, at some point I disabled the second part of the macro ("otherwise execute the following actions") and decided to separate if/otherwise scenario into a different macro (you can see the disabled actions below, as well as the other 3 macros in the middle pane for scrolling up and down in both evernote and Scrivener) . This was a few months ago, so im not sure why i did this now, but must have been having some sort of issue, sorry that this is such a mess lol! Could you please advise on whether its better or makes a difference if i recollect the if/otherwise scenarios into one macro or leave them in multiple macros?

The point of the "track target application" macros is to set a TargetApp variable, so it's this variable that you want to use to determine how your macro behaves. In cases like this I would actually recommend not using If/Otherwise at all, but a different action called Switch/Case which can be used to have your macro perform many different actions based on a single condition. This also makes it possible to consolidate the functions of multiple macros into a single macro. Here's an example that should help get you most of the way there:

scroll down Scrivener:Evernote.kmmacros (6.5 KB)
Untitled

As you requested in your OP, this macro will open either Scrivener or Evernote depending on which one was opened last before scrolling the front window down, and it determines this by looking at the current contents of the TargetApp variable, which is of course set by the most recent of the two apps to have been used. You should be able to duplicate this macro and change the scroll action and triggers to have it do the same thing for scrolling up, and of course the same "do different actions depending on which app was last used" principle can be used in any other way you like.

1 Like

WOW works perfectly thanks so much!!!

1 Like