Turn on Wi-Fi periodically to do a quick network discovery
One solution to this problem: use a tasking program such as Locale or Tasker and create certain conditions that will enable your wifi (essentially writing a script as you described).

For example:  Use GPS and determine if I'm at my home (I believe you can use real GPS, which will consume quite a bit of power, or base it off of which cell tower you are connected to, which you would already know anyways if not in airplane mode).

Or you could even setup certain times of the day you know you will be at home, or work, or wherever you regularly have wifi.