Return to a application saved in a variable

Hi all,
I have the need to save the current application in a variable, then I make a series of windows manipulations and then I want to return to the application I started. I know the token %Application%1% or %CurrentApplication% and the function “Set Variable to Text”, but then I’d found the possibility to use this variable do switch to the application, because in “Open a file, folder oder document” i can’t use the variable.

Any idea o suggestion?

Thanks in advance!

Solved with Applescript:

tell application “Keyboard Maestro Engine” to set currentApp to getvariable "cApp"
activate application currentApp

Where cApp is the variable setted before with “Set Variable to Text” and “CurrentApplication”

Done :slight_smile:

Hey Manny,

Well done.  :smile:

Personally I’d use the app’s bundle id for robustness:

Script 1 – store frontmost app bundle id:

tell application "System Events"
   set currentApp to bundle identifier of first process whose frontmost is true
end tell

tell application "Keyboard Maestro Engine"
   setvariable "currentApp" to currentApp
end tell

Script 2 – restore the original app to the front:

tell application "Keyboard Maestro Engine"
   set currentApp to getvariable "currentApp"
end tell

tell application id currentApp
   activate
end tell

-Chris

Use the Use Variable action.

1 Like

Thanks Chris! Look below at the solution by Peter!

Excellent Peter, Thanks!

I cannot find any bundle tokens in latest Keyboard Maestro, so what is the best way to save current front app, go do something, and reinstate where you are using KM9? Assume multiple steps so %lastapplication% is not really strong enough.

Also is there any way of running KM as a background task so the screen is not flashing the steps apart from telling the USER to go make a cup of coffee now?

Very easy:

image

4 Likes