System Events got an error: application isn't running

Hi everyone.

I’m fairly new to AppleScript, so apologize for any newbie questions. I’m trying to automate a press of a button of a dialog in a certain application. The automation is ran in a virtual machine using vCenter.

The dialog is a native modal. I used “Automator” to generate the script to press it:

tell application "System Events"
    click UI Element 5 of group 1 of sheet 1 of window 1 of application process "MyProgram"
end tell

I’m running the script by calling the osascript command with a filename (myscript.scpt) from within a Python script. The Python script is executed remotely through the vCenter API if that matters.

I’m getting the following error: “System Events got an error: application isn’t running”. I’m not sure why that happens, because sometimes when I manually run the Python script from the virtual machine, it does work, but remotely it always throws this error.

So after some digging, I found out that when running the osascript manually from the VM (using terminal) it works fine, however when using vSphere’s API, the above error gets thrown. I assume it’s because of a permissions issue, however I already added “vmware-tools-daemon” under pretty much everything in “Privacy & Security”.

I am not familiar with vSphere’s API (I use Parallel Desktop), but one idea would be that it is not executed within the user account.
For example, I have a similar problem when I use the Parallel Desktop API to execute Windows commands from the Mac. These are executed under the “system” user.
I have therefore written my own web servers for both cases (Mac to Windows and Windows to Mac) that run in the user account, instead of using the APIs. Everything runs smoothly with this.