Pause Until...This Application Is Running

I have a macro that "launches" (activates) Stream Deck.app when the Stream Deck USB device is attached. Activating the app brings it to the front, and I would like to dismiss it and return to the previously active app. No matter the approach, it seems I have to insert a hard Pause between activating Stream Deck.app and returning to the previously active app for it to work. "Pause Until...This application is running" does not work - Stream Deck.app stays active. A Pause for 1.5 seconds seems to work, but anything less is inconsistent. Here is what I have:

BTW, I also tried using "Activate Last Application" instead of using a variable, but that also requires a hard Pause and it doesn't seem to recognize Keyboard Maestro as the last application so it skips to the last active application before Keyboard Maestro.

So my question is, why doesn't "Pause Until...This application is running" not work?

Hey Evan,

macOS is a bit fickle...

A given condition may be met before all the relevant pieces of the OS are finished initializing, and this can throw a macro's timing into a tizzy.

Here's the general method I use to make sure an application is frontmost before moving forward in a macro.

-Chris


Activate System Preferences > Keyboard > Text.kmmacros (7.9 KB)

1 Like

I would use Pause Until Application is currently at the front, perhaps followed by a short pause (0.1 sec). This has always worked reliably for me.

@ccstone - Thanks. I will keep that in mind for other scenarios because I have run into this before. In this case, I don't know the terminology, but the app doesn't behave like a typical app. No standard menu items. When it is active, the menubar from the previous app still shows.

@JMichaelTX - I did try that as well, which usually works, but not here. Keyboard Maestro can tell when it is running

image

but not when it is at the front

image

Grrf... I dislike when applications fail to follow normal design guidelines (within reason).

Okay, the next thing I'd try is a Pause Until action using the 'Found Image' option.

If I had my hands on the Stream Deck app I'd also look at it with UI Browser to see if there were any UI elements I could find and wait to become active.

In lieu of that you could try my window analysis tool:

-Chris

1 Like

I wasn't sure how you meant to determine if an element was active in KM, so I cobbled this together and it does work.

image

Is this what you had in mind? I'm guessing there is a more elegant way :slight_smile:

1 Like

hi Evan,

i commiserate with you as i've had similar behaviors with some non-standard apps.

one thing that isn't clear to me...

does Stream Deck require you to do anything with its window after its open? if not, perhaps just tell KM to hide the app?

also, you might want to try is the open command at the command line:

The open command opens a file (or a directory or URL), 
just as if you had double-clicked the file's icon.

options which may simplify your endeavor:

-g  Do not bring the application to the foreground.
-j  Launches the app hidden.

e.g.,

$ open -j /applications/textedit.app

Hey those are some good ideas to approach this. Unfortunately, Stream Deck does not respond to Hide. I've tried it with AppleScript, ⌘H, Keyboard Maestro Hide action, and those command line options you shared. I thought -g might do the trick, but no.

So far, checking for the UI element as @ccstone suggested is the only thing that has worked. The way I posted above using AS is the best I could come up with.

Stream Deck is a menu bar app, and those very frequently don't launch/hide the same way as regular apps that appear in the Dock.

There's a secret flag to the Stream Deck.app to prevent it from appearing when launched:
--runinbk

So here's a shell script that you can use which will launch the Stream Deck app without it becoming visible:

#!/bin/zsh -f 

/usr/bin/pgrep -x 'Stream Deck' ||\
"/Applications/Stream Deck.app/Contents/MacOS/Stream Deck" --runinbk

exit 0

The pgrep command makes sure that Stream Deck isn't already running before it runs the command.

Try that instead of the action that activate/launches Stream Deck when the USB device is attached.

4 Likes

I put that script into Execute Script action in place of the AppleScript and the Pause Until stuff. It worked, but the macro keeps running for some reason so I am going to stick with what I had. Thank you!

I think you might need to set the shell script to run "asynchronously" if you do want to try it again.

1 Like

That did it. I marked the shell script as the solution.

Just an FYI: for whatever reason, If I quit the app and trigger the macro manually, I don't even see Stream Deck app. It just shows up in the menu bar. If I let the USB device trigger fire the same macro, the app appears in the front, then gets pushed back behind the previously active app.

Not complaining :slight_smile: Just an observation. Thanks for the solution.

Curious. Not sure why that is.

@peternlewis – please note post #12 above – possible bug.

Hey Evan,

Note – your macro activates with the 'all windows' option.

Try unchecking that option and see what happens.

Another thing to try is this:

tell application "Stream Deck"
   run
end tell

Another possibility:

use AppleScript version "2.4" --» Yosemite or later
use framework "Foundation"
use scripting additions

set appBundleID to "com.apple.TextEdit"

set ws to current application's NSWorkspace's sharedWorkspace()
set appURL to ws's URLForApplicationWithBundleIdentifier:appBundleID
set appAlias to appURL as alias
tell application "Finder" to open appAlias

Change the TextEdit Bundle-ID to Stream Deck's.

-Chris

Hey Chris, 0-3.

¯_(ツ)_/¯

There is not enough information to investigate whether there is any kind of bug. But as noted, some apps like this don't really quit, they turn themselves from background-only to foreground, and so “is running” may not mean what people think it means, and activating may or may not do what you expect.

1 Like

Thank you for this tip.

1 Like