Make Application have focus (launched from launch agent)

Okay, I know this used to work, hence my frustration.

I’m launching an app from a launch agent: The launch agent calls a .sh that has:

#!/usr/bin/env bash
/usr/bin/osascript /myscriptlocation/com.acme.myscript.scpt

From within the scpt, I’m using this to call the application:

tell application (passedLoc & "myapp.app") to activate

Again, all good.

However, as the application is part of our policy enforcement, we want to bring the application to the front of whatever the user is doing. Within applicationWillFinishLaunching_ I’ve tried:


tell current application's NSApp to activateIgnoringOtherApps_(true) 
tell theWindow to makekeyandorderfront_

As the script is being run every 20 seconds, the ONLY thing that works is tell theWindow to makekeyandorderfront_

This only works if the application is already open and the launch agent runs again, opening the app.

Any Ideas? I’ve looked across the forum and have to avoid anything with System Events, given how you need the SQLite command to register the app. As the scripts mat be delivered by means other than an installer, we can’t assume evaluated privileges

Cheers,

Paul

You lost me at “As the script is being run every 20 seconds”…

As mentioned.

I have a launchAgent that is scheduled to run every 20 seconds

The script runs through a set of tests and calls an application (Created by me). Once called, the script ends

The application may already be open when it is called again, and it’s only then that it tends to be brought into focus

I can simulate this by creating a completely empty application and opening it from the command line. Terminal will still have focus, not the newly opened app.

However, the results are not predictable. Sometimes the app opens in front, and sometimes it doesn’t

I’m afraid I still don’t fully follow, but have you tried using performSelector:withDelay: to call makekeyandorderfront_?

tell myApp to activate

or

tell (the Finder, I think) to set the front most of application process myApp to true

I think you need another handler on application did finish launching. In there, you need tell me to activate or activate me. But, I was also thinking that you might want to do this in ASObjC and don’t know yet how to do that.

Yep. Works fine:

tell current application's NSApp to activate

Thanks for the help all.