How do I set the origin of a mesh to the center of its axes-aligned bounding box?
No scripting required.

1) Select your whole mesh in edit mode (it may also work in object mode)

2) Change your pivot point to "Bounding box center" (little dropdown menu at the top of your 3D view)

3) Use Shift+S and select "Cursor to selected"

4) Enjoy !!!