"Connection is invalid" means that the script cannot communicate with the application. Perhaps it has been quit during execution… perhaps there is a duplication in your list? My best guess is that there is more than one process spawned by an application.
Anyway, I think your methodology could be improved. One of your problems is that you're checking the process for each item in your list. You should compile a list of processes, and check that. This avoids the problem of duplicate processes for the same application. It also deals with the issue of the process list changing during script execution.
set appList to {"Caffeine", ...}
tell application "System Events"
set procList to (name of every process)
end tell
repeat with appName in appList
if (procList contains appName) is true then
tell application appName to quit
else
tell application appName to activate
end if
end repeat
If you still get errors, you might have to enclose the tell application statements in try
blocks.
One more thing: the approach you're taking doesn't work well if only some of the applications are open, and some are not. You're checking each individually and quitting if it's open, launching if it's not. So your list of applications will still be split between open/shut down. It sounds like you want to open a "set" of applications and close a "set" of applications using the same script.
Therefore, you may want to consider splitting this into two scripts, "open set" and "quit set", and/or checking whether your "main" application is open and cascading the open/close of your application list depending on that one application's state.
Have you tried using the built in tools to force a log out upon reaching a certain idle time?
It is an Advanced Option (see the bottom button) in the Security & Privacy preference pane on Lion.
If you want one user to have a different timeout you can script things easily with Automator or AppleScript:
tell application "System Events"
log out
end tell
The trick is making your custom app keep a timer for 30 minutes of inactivity which is why the system control panel is the best option for this request.
Best Answer
Interesting problem.
Here's a snippet to help you start listing windows of apps:
I didn't bother to set line feeds for multiple windows -- as there is a TREMENOUS drawback to this approach:
This script does not and will not find windows on each and every "space" set in Mission Control.
It's a pity, but as yet Mission Control is un-scriptable.
Your best -if ugly- solution would be to have each app put frontmost, then checked, then quit ...