Waiting for an arbitrary application's window to actually appear.

I know how to start an arbitrary application and wait for its process to begin. However, this doesn’t always ensure that the application is actually visible on the screen. What I’m wondering is whether there’s a way for me to wait until there is an actual visible representation of the application on the screen.

Here’s the way I know of to wait for the application’s process to start:

tell application "Arbitrary Application"
end tell
tell application "Finder"
	set arbitraryProc to a reference to process "Arbitrary Application"
	repeat until exists arbitraryProc
		delay 0.25 -- this delay is optional
	end repeat
end tell

So beyond this, is there any way I can tell that the application actually has attained a visible presence?

Thanks in advance.

I think “activate” kind of guarantees this, as it is designed to open an app as the front app so it can get AS commands. (As opposed to “launch” which will open the app in the background). In which case I’d try:

tell application "Safari" --or whatever app you want
	set myNameIs to name of (info for (path to frontmost application))
	display dialog "Current front application is " & myNameIs
end tell

Well, the reason I asked about this functionality is because I have some code which switches from one Space to another and starts up application instances in each of these Spaces before moving on to the next one. I have found that if I simply move to a Space, activate an app instance, and then move to another Space, the instance often ends up actually appearing in the second Space.

I know I can automatically associate given apps with given Spaces, but that feature doesn’t help me here, because I want certain apps to have running instances in different Spaces. This mostly involves a number of Terminal and Emacs instances that I want to distribute over various Spaces, although I’m looking for a general solution that would work with all well-behaved applications.

Right now, I want use the loop I described above (or actually, some sort of enhanced variation that checks for individual windows) after activation to try to ensure that the given app instance actually appears in the specified Space before switching. However, in some preliminary tests, I have found empirically that this, also, doesn’t always guarantee that the app instance appears in the Space I intend. So I now have to issue a delay command after looping to make it even more likely that each instance starts up in the desired Space before I switch, but I was hoping that I could come up with something better that didn’t involve timed waits.

I’m starting to think that this isn’t possible without those timed waits, but I’m hoping that I’m wrong.