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?
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.