Uhm, that involves continuously stepping the focusing motor though. Theoretically you could, instead, set the focus to an arbitrary point and then evaluate sharpness from the viewport image, and shoot when it reaches a deemed-maximum.
This is strictly related to implementing an effective
autofocus algorithm. If that is achieved, your request should also be easily satisfiable (though not just with a script, of course).