Store Currently Active Application in a Variable?


I would like to build a macro which switches to a different application, does something, and then switches back to the original application.

Is it possible to store the currently executing application in a variable? I can't seem to figure out a way to do it.


1 Like


However, you may find this Action more useful:


Thank you very much!

Hello @JMichaelTX !
As a follow up to the above question, is there any action I could use to activate the application Current_App_Name ?
I can't find any app action which takes variables
thanks very much !

You can do this with the Use Variable action:


more elegant than my solution. I thanks very much !

1 Like

I created this little macro just to test the variable to application and to window actions.

Basically, it should do nothing since it is restoring the current application and window.

Both last actions generate an error message shown below

Thank you very much

TEST App to Variable then Show Variable.kmmacros (23.5 KB)

MACRO ERROR OCCURRED  on  2021-08-19 12:55:59
This info was pulled from the KM Engine Log for the
Last Macro error that occurred.

TEST App to Variable then Show Variable

Action Type:	Use Variable to Set Application By Name
Action Name:	Use Variable “CurrentApplication” to Set the Front Application by Name

ACTION ERROR:	 for variable CurrentApplication 

KM Engine Log:	
Use Variable to Set Application By Name failed to for variable CurrentApplication In macro “TEST App to Variable then Show Variable” (while executing Use Variable “CurrentApplication” to Set the Front Application by Name

Log File:  ~/Library/Logs/Keyboard Maestro/Engine.log

I'm not sure how you ended up using %Application%Foreground% for your front application token, but that was the first problem; it presents a list of foreground applications rather than just the frontmost one. Try it in the Value Inspector (6 in the KM editor):

What you want is %Application%1%, which only has the frontmost app name:

As for the window name error, that was just a timing issue; the macro was trying to bring the window in the front app forward before that app was actually frontmost. You can resolve this by adding a generic pause in between the "Use Variable to set app" action and the "Bring window to front" action, or do something like this that pauses until the app in the variable is at the front:

1 Like

great !! thank you so much !

1 Like