Just expanding on what bumper314 has posted.
Code:
tell application "System Events"
set frontmostApp to name of application processes whose frontmost is true
tell application frontmostApp
quit
end tell
end tell
This wont work in Applescript Editor, as Applescript editor will be the fronmost app, and will still be processing and running the script, so will only work when the script is compiled.
Regards Mark