MacOS – How to Hide and show an app via a single shortcut

automationkeyboardmacoswindow-manager

I'd like to show & give focus to and hide an app using a single shortcut. The app (or apps) i like to use this way do not natively support this on their own. Is this possible on macOS (10.15 in my case)? Maybe using some 3rd party app or Automations?

I'm using this with iTerm2, it's called "Show/hide all windows with a system-wide hotkey", also, Launchbar has this feature but i'd like the same behaviour in PCalc and/or Calcbot.

Best Answer

This answer is a step by step explanation of what ankii proposed in a comment.

First, open Script Editor and write an Apple Script to toggle the visibility of your application. The following code should work, just change the value of appName (currently set to Finder) to be the app you want.

set appName to "Finder"

tell application "System Events"
    if visible of application process appName is true then
        set visible of application process appName to false
    else
        set visible of application process appName to true
    end if
end tell

Save this Apple Script as an application (File -> Save -> File Format -> Application). I named it Toggle Visibility, but you can call it anything.

Next, open Automator and create a new Quick Action (previously a called a service). Change the values in the drop down menu so that "Workflow receives no input in any application" is displayed at the top.

Drag Launch Application from the list of options on the left (you can use the search bar to find it) into your workflow. Set the selected application to other, and find the Apple Script you saved as an application previously.

Go to File -> Save to save your Quick Action, and give it a name.

Finally, open System Preferences and go to Keyboard -> Shortcuts -> Services, and scroll down to the General Tab. There, you should see the name of your Quick Action you just saved. Click the check box to enable it, and add a keyboard shortcut to activate it. Make sure the shortcut you added doesn't conflict with other shortcuts on your computer.