global procName
global initActivated
global initKilled
global initFront
global initNotFront
on run
tell application "System Events" to set allProcs to name of every process whose visible is not false
set procName to (choose from list allProcs with prompt "Choose a process to monitor")
if procName is false then quit
set procName to procName as string
registerFlags(procName)
end run
on idle
tell application "System Events"
set nowActivated to (exists process procName)
set nowKilled to not (exists process procName)
set nowFront to frontmost of process procName
set nowNotFront to not (frontmost of process procName)
end tell
if nowActivated is true and initActivated is false then
-- code to be executed when app launches
end if
if nowKilled is true and initKilled is false then
-- code to be executed when app quits
end if
if nowFront is true and initFront is false then
-- code to be executed when app gets frontmost
end if
if nowNotFront is true and initNotFront is false then
-- code to be executed when app gets in the background
end if
set initActivated to nowActivated
set initKilled to nowKilled
set initFront to nowFront
set initNotFront to nowNotFront
return 1
end idle
on registerFlags(processName)
tell application "System Events"
if not (exists process processName) then
set initActivated to false
set initKilled to true
set initFront to false
set initNotFront to false
else
set initActivated to true
set initKilled to false
set initFront to frontmost of process processName
set initNotFront to not (frontmost of process processName)
end if
end tell
end registerFlags
Hope it works,
ief2
NOTE: Not tested, so there could be a couple mistakes in the script