Attach objects by distance

Hello. I'm looking for a script to attach objects to each other, depending on the distance between them.
Any help is welcome. Thanks