Applescript Tell Application of Specific Instance If I have 2 instances of one application running, and I want to target one of them by their PID to run an Applescript snippet, how would I go about that? I have the PID of the instance I want to target, but if I run the following, it will execute on whichever instance was started last.

tell application "/Applications/Adobe After Effects CC 2018/Adobe After Effects CC 2018.app" to DoScript "alert()"


Is there a way to either set an instance as the "Frontmost" so it get's targeted, or get an application by PID and tell that one specifically?

Thanks!
Scripting Bridge Approach

With ScriptingBridge, create an SBApplication instance with applicationWithProcessIdentifier: and then you can call the bridged AppleScript methods on the process of your choice.


  Overview
  
  Scripting Bridge is a technology that lets you control scriptable Apple and third-party applications using standard Objective-C syntax. Introduced in OS X version 10.5 (Leopard), the Scripting Bridge framework dynamically implements an Objective-C bridge to OSA-compliant applications—that is, applications having a scripting interface (usually defined in a sdef file). As part of this implementation, it generates Objective-C class implementations of the classes it finds in the scripting interface, including objects and methods representing properties, elements, commands, and so on. The objects are derived from classes defined in the Scripting Bridge framework.


AppleScript Approach

A discussion on MacScripter suggests this approach:

tell application "System Events"
    set myPID to unix id of process "TextEdit"
    -- ...for every matching process
    -- set myPIDs to the unix id of processes whose name is "TextEdit"

    set myProcess to first process whose unix id is myPID
    set the frontmost of myProcess to true
end tell


See also: How to activate Mac OS X application with a given process ID?