# A simple proof that it works:
tell application "System Events"
set _flagA to frontmost of process "Script Debugger"
set frontmost of process "Safari" to true
set _flagB to frontmost of process "Safari"
set _flagC to frontmost of process "Script Debugger"
set frontmost of process "Script Debugger" to true
end tell
I prefer not to use System Events if at all possible, because it can hang for up to 2 seconds when it starts up cold on my machine.
This is quick and concise:
if (do shell script "ps -Ac | sed -En '/Safari$/p'") ≠ "" then
tell application "Safari"
if not frontmost then
# Kill _javascript_
end if
end tell
end if