Hardly need to explain what Fit gizmo do in UVW Mapping modifier, nor yet the need and benefit of it automation via script, right?
That operation is not directly accessible to MAXScript, but thanks to Borislav "Bobo" Petrov and his nice tutorial on this task. He explain very well (as always) the matrix transforms.
Well, maybe my code hold some learning value too. However, as I not found such script online, I hope my script would be welcome to all Max'ers. (Script page)