How to bring an application made in Automator to the foreground upon launch?

I've created an application with AppleScript in Automator.

I've created a Service in Automator which launches the aforementioned app.

The Service is triggered by a keyboard shortcut.

The problem is that upon launch, the application's dialog box has a light gray top bar. I have to click anywhere on the dialog for the dialog to turn to the darker gray, which shows that the app is now selected.

Is it possible for my application to do this automatically?


Solution 1:

Have a look at the activate command in the AppleScript Language Guide Commands Reference.

activate Brings an application to the front, and opens it if it is on the local computer and not already running.

I'd add activate me after the start of the on run handler.

Solution 2:

I see that the OP's question says "an application made in Automator."

But, as an aside, and since this question has most of the right keywords to solve the problem I was having (applications launched from an Automator "Run Shell Script" action showing up behind other open applications)…

If you are launching a GUI application from a shell script action in Automator and find that it is not coming to the foreground and receiving focus, try something like

open -a ProgramName.app --args "$@"

and set "Pass input" to "as arguments". The "$@" will be expanded as an individually-quoted list of the input arguments, separated by whitespace (this is a feature of the Unix shell, not Automator).

This works great, for example, for creating a "Writer" Automator app that launches LibreOffice Writer (open -a LibreOffice.app --args --writer "$@"), and also accepts drag-and-dropped files.

If you are still having trouble getting the application to come to the foreground (maybe it's already open, for example), try appending these lines to the action in Automator:

sleep 1  # adjust as you see fit
osascript <<<'tell application "ProgramName.app" to activate'