system events and processes with the same name

I’m sure this has been answered before, but my searching abilities are not up to finding the solution.

How can one pass a System Events command via something other than the process name? I need to do this when there are multiple processes with the same name, and I would like them all to do the same action (imagine different versions of a piece of software, all of which use the same application process name).

I thought it might be possible to use the process IDs, but my abilities limit me to getting the proper applications to the front.

Here’s something simple to illustrate the problem—I’ve used the name of the application as “foo”, but you could duplicate any application on your hard driver, and then launch both the original and the copy. (TextEdit is fine)

tell application "System Events"
	set theIDs to the id of every process whose name is "foo"
	repeat with anID in theIDs
		set frontmost of process id anID to true
                --- need something like (or a workaround for)
		tell frontmost application to keystroke "hello, world"
	end repeat
end tell

Any tips would be helpful.

I don’t understand why you’re running two instances of TextEdit say, instead of two windows. Won’t the newer version of your software open the older file?

I’m not really running two instances of TextEdit… I had included it as an example to illustrate the problem.

I do, however, sometimes run multiple versions of my favorite statistical analysis software (Stata) at the same time. The application is not currently scriptable, so I’m using System Events to control it. Since the versions have the same process names, using a ‘tell application …’ block limits me to working with the version which has the smallest process id number. I would like a method of either sending the same events to all open versions or to be able to pick a version to receive the events.

I realize that this goes against ‘you should run only one instance of an application’ philosophy, but it does come in handy from time to time.