I have been trying to do this too, although I don't really care that the front application's window goes all the way to the back, I just don't want it in the front and would be happy to just bring the second application forward.
So far, what I've been trying is to get details of the front app's window: app, filename, size, position, then close that window. That seems to be the only way to actually get that app out of the way so that I can find out what the second app is. When it is close, then I get details of whatever the new frontmost window is for the second app. I reopen the window I just closed in the first app and then bring the second app's window to the front.
I've been trying to do the whole thing in AppleScript so that it runs a little faster, but so far I haven't been able to figure out how to reopen the original front window in the same position it had been in, I have to open it and then move it. As fast as AppleScript can do that, it still shows the window in a different location from where it "belongs".
I've asked about how to do this step in a different thread: