Help Needed: Launching Apps in Background

What I'm Trying to Do

I'm trying to launch apps in the background without them popping up on the main screen and causing a havoc.

This applies to Todoist, Cron, Spotify, Bitwarden, and Spotify.

What I've Tried / What Actually Happens

When I test the action, it runs by itself, but for some reason the macro does not reach the point where it's supposed to resize, minimize, or close the app. Here's the macro I've been using:


Position App Windows.kmmacros (44 KB)

Any help would be greatly appreciated!

Pause until app is running by itself is useless in a launch scenario.

Most apps show as running long before you can affect their UI.

So you have to combine is running with something like menu-path exists or front-window exists, before you start trying to work with the app.

Keyboard Maestro doesn't support launching apps in the background.

To do this you can use the shell:

open -a Calculator -g

Or AppleScript:

tell application "Calculator"
   if it is not running then
      run
   end if
end tell
2 Likes

I love this method and use it quite a bit myself...

For @szfkamil , if you want to open the app hidden, you can use the -j flag instead of the -g flag.

Example:
open -a Calculator -j

Open an application hidden.kmmacros (2.1 KB)

Macro screenshot (click to expand/collapse)

5 Likes

For posterity’s sake, I recently discovered that the command open -a ${app_name} -j only works if the app is not currently running. If it’s already running, it opens it visible. Here’s a workaround (using Maps as an example) to ensure that the app is either opened hidden, or if running, hide it.

# determine if app is running
if pgrep -x "Maps" > /dev/null
	then
	# if running, hide it
	osascript -e 'tell application "Finder"' -e 'set visible of process "Maps" to false' -e 'end tell'
else
	# if not running, open hidden
	open -a "Maps" -j
fi
5 Likes

Hey @cdthomer, I am running this script and other solutions for the Todoist app on mac and can't seem to get it to work properly:

The application /Applications/Todoist.app cannot be opened for an unexpected reason, error=Error Domain=NSOSStatusErrorDomain Code=-600 "procNotFound: no eligible process with specified descriptor" UserInfo={_LSLine=388, _LSFunction=_LSAnnotateAndSendAppleEventWithOptions}

This is the output when I try to run the entire command or just the open -a command with -j in the terminal.

This is the text script I'm using:

# determine if app is running
if pgrep -x "Todoist" > /dev/null
	then
	# if running, hide it
	osascript -e 'tell application "Finder"' -e 'set visible of process "Todoist" to false' -e 'end tell'
else
	# if not running, open hidden
	open -a "Todoist" -j
fi

Do you know what may be wrong, or steps to take in order to find out for myself?

Many thanks,

I’m not familiar with that error, and I don’t have that app for testing, but it looks like it can’t find the application process to launch it. Can you change the app name to something simpler, like Maps, and get it to work?

I just had a stubborn Qt application that refuses to reliably hide or stay in the background with the shell open -a <App> -j (or -g) command.

The thing that finally works – so far – is the combination of AppleScript’s run + immediately setting visible of the process to false:

tell application "<StubbornApp>"
	if it is not running then
		run
	end if
end tell
tell application "System Events"
	if application process "<StubbornAppProcess>" exists then
		set visible of application process "<StubbornAppProcess>" to false
	end if
end tell
1 Like

That was one of my initial solutions, but I didn’t like the app flashing on the screen. But when all else fails... :wink: