Id do something like get a smart switch that can handle the amp/watt of the air conditioner, and use ifttt or home assistant to connected it to a binary sensor that is on the window, if the window is closed the switch is on, if the window is open the switch is off.
Then just set the thermostat on the window unit to your desired on/off thresholds. You can further add temperature sensors and add additional smart functionality.
If you do not want to go "smart" with this as you would need to take the time to learn a few things or set it up, I can envision several dumb solutions as well using physical switches connected to the window frame.
You can go uber smart as well, and have things like servo's that would close/open the windows. Sky is the limit.