You’re 80% of the way there: you got the format to use, but you’re not using it. You can use the format to write the file with format.write(map, path).
map.save() is just the same as using File > Save, and you should not be using it in custom export format.
Just one additional note I’d make, is that when you already know you want to save the map as tmx, then you should probably just use tiled.mapFormat("tmx") instead of mapFormatForFile.