I would remove unnecessary variables, and coercions:
-- this script makes sure that a particular program keeps on running,
-- even if it has frozen, in which case it'll be force-quit and relaunched.
property timeoutInSeconds : 2
property appName : "Handbrake"
property bundleID : id of application appName
on idle
if running of application id bundleID then
tell application id "com.apple.systemevents" to set theID to ¬
unix id of (first process whose bundle identifier is bundleID)
with timeout of timeoutInSeconds seconds
try
tell application id bundleID to get documents
on error errMsg number errNum
if errNum is -1712 then -- is frozen
do shell script "/bin/kill -9 " & theID
repeat while its running of application id bundleID
delay 0.2
end repeat
activate application id bundleID
end if
end try
end timeout
else
activate application id bundleID
end if
return 60
end idle
That version is quite nice and concise, though it fails at one little detail compared to mine: If there are several versions of the same app, it may launch the wrong one because it ignores the explicit path I may want to specify for it.