How to close applications on sleep? I have a password management application (KeePassX) of which backup I sync over Dropbox across several computers.

However the password database can be open only on one computer at a time for file locking reasons. Problems arise when one computer goes to sleep and the application is left open on this computer, the database locks are never removed and in the worst case there are unsaved changes in the database.

Would it be possible to make a program to prompt closure on sleep (like with shutdown) or record a macro which closes the program when the computer puts itself to sleep or is made sleep through Apple menu?
It's hard for me not to to question the validity of your setup (broken sync!), but ... for doing something on sleep or wake, you should try SleepWatcher by Bernhard Baehr (more context at Mac OS X Hints). It is using launchd for monitoring.


  [SleepWatcher] is a command line tool (daemon) […] that monitors sleep, wakeup
  […] of a Mac. It can be used to execute a Unix command when the Mac
  […] goes to sleep mode or wakes up, […]


If you have a bit of terminal experience, I think you can set it up. Make sure you read the well-hidden "Installation for new SleepWatcher users" section in ReadMe.rtf