> how do I keep this code working in the back all the time?
It is possible to do via AppleScript's idle handler - the script will stay in the background and periodically trigger its actions:
on idle
-- do something
return 10 -- come back and do it again in 10 seconds
end idle
However, you need to be clearer about how you want/expect this to work. Having the script blinding run every 10 seconds seems counter-intuititive - how do you know that the window in question is not actively being worked on?
Also you have to consider what happens when you normally close a window - for example, most applications would prompt you about whether or not to save the document before closing. How do you expect your script to handle this?
I suspect that what you want is to detect if the application has been idle for 10 seconds (or more), which is much harder to do than just running a periodic check. You'd need some way to detect changes/activity in the application to know if it's been idle or not.