System Events Catch-22

I want to know the active application so I call

from a script run from the Script Menu. The problem is that when run, System Events becomes the active application. Is there a way to find out what the active application was before I ran the script?

Thanks,
Jon

Jon, take a look at Rob Jorgensen’s comments about this…

http://discussions.info.apple.com/WebX?13@112.kO0Sahoxana.6@.4aaa67f7/0

Unless you’re telling System Events to activate, this shouldn’t be the case. In fact, you don’t even need ‘System Events’ to do this:

on run
    set the_process to (path to frontmost application as string)
end run

all on its lonesome will tell you the name of the script editor you’re using. However, even including it in a tell application 'System Events" block returns the same thing:

on run
    tell application "System Events"
        set the_process to (path to frontmost application as string)
    end tell
end run

But if you’re activating System Events:

on run
	tell application "System Events"
		activate
		set the_process to (path to frontmost application as string)
	end tell
end run

You’ll get back the System Events.app since that’s the active app (you just told it to be active).

In summary, System Events is a background application that can do all it needs to in the background without ever being ‘activated’ or brought forwards.

Yes, that’s my point the trouble is that running a script from the Script Menu seems to activate System Events.