There's already a preAscensionScript that can do exactly that. For safety's sake, you'd probably want to check that they were free before grabbing them (in some rare circumstances -- mostly basement related in my experience -- you may have actually used the free ones).
I've thought about putting such a script together for a while, and maybe I will. It should:
1) Get goofballs (if they're free)
2) Make pies out of all of your keys (needs fail-checking, but since the script won't fire until you're about to ascend, it doesn't hurt for it to use turns)
...and that's where I run out of ideas (which is why I haven't bothered yet).