Applescript activate not making the application fully active

I'm writing a script for an application that does not support scripting. This is a proprietary app that I'm stuck with, so there's no alternative for me. The problem I'm having is this app does not respond well to "activate". When I'm activating it in an applescript, it brings its main window to the foreground, but the window is still not fully active (e.g. the Close/Minimize/Fullscreen buttons are grey, the shadow around the window is faint, etc). I have to click on it or cmd-tab twice to get it to activate. The application has some logic in it that prevents it to start functioning until the window is truly active, so I have to make the window truly active.

The workaround I'm using right now is to make the script press cmd-tab twice.

tell application "MyApp" to activate
key down command
keystroke tab
key up command
key down command
keystroke tab
key up command

I was wondering if there's a more elegant solution to make the app truly active.


The alternative is to activate your app through Dock:

tell application "System Events" to click UI element "MyAPP" of list 1 of application process "Dock"

The way I got here is to use Automator's record feature and copy the steps" and paste them in Script Editor. It translates them to AppleScript.