Create Trigger to Close App/Window When Unfocused

Is it possible to make a trigger that will close a specific app or window whenever it is unfocused?

In terms of an app - yes, even if it takes a little more effort:

  • Create an AppleScript file with the following content (change the application name from "DeepL" to "Your name"):
use framework "Foundation"
use scripting additions

set appName to "DeepL" -- change the app name here

property ca : current application
property workspaceClass : class "NSWorkspace"

set ws to workspaceClass's sharedWorkspace()
set wnc to ws's notificationCenter()
tell wnc to addObserver:me selector:"applicationDidiDeactivate:" |name|:"NSWorkspaceDidDeactivateApplicationNotification" object:(missing value)

on applicationDidiDeactivate:theNSNotification
	set userInfo to theNSNotification's userInfo()
	set theApp to userInfo's valueForKey:(ca's NSWorkspaceApplicationKey)
	set theAppName to (theApp's localizedName()) as text
	
	if theAppName is my appName then
		tell application theAppName to quit
	end if
end applicationDidiDeactivate:
  • Add the predefined trigger "After BTT was started" and add the action "Open Application / File / Apple Script". Select the script file.

  • Restart BTT

Note:
The observer (NSWorkspaceDidDeactivateApplicationNotification) runs during the application is running which executed the script file (in this case BTT). If the script file is called multiple times, multiple observer instances will be created!

I have just seen that Andreas has included a predefined trigger for this:


(Specific app has been deactivated)

Then all you need is an AppleScript action:

tell application "Your app name" to quit