Automatically connecting to a network with a hidden SSID is a bad idea.
Since your computer cannot passively listen for the SSID broadcast and automatically connect when it sees the SSID (which will not show in the beacon broadcast, since that is how hiding the SSID works), it has to actively send probe packets with the network's SSID, even if it is nowhere near the access point, and wait for a response. This means that, instead of the access point broadcasting its name all the time, you have all computers configured to automatically connect to it broadcasting its name all the time, no matter where they are.
Not to mention that, to be able to roam between several access points with the same SSID, the computer has to know their BSSID (essentially, the AP's MAC address). Usually they do this by listening to the beacons broadcast by the access points. Since the beacons do not have the SSID (hey, it's hidden!), the computer has to periodically send probe requests even if it is already connected to the access point. Making it laughably easy for an intruder to find out the SSID if even one computer is connected to the network. Not to mention the desassociation attacks.
So, it gains almost zero security (it is still way too easy to find the SSID) and loses a bit more security (the client computers constantly announcing to the world "hey, I am a computer belonging to someone who works at company XYZ!" even when nowhere near company XYZ). The net result is negative.
The only way to reduce or even avoid the security loss is to have it connect manually instead of automatically. Which seems to be what Apple is doing. (Windows Vista, from what I recall, warns you of the security issues when you try to set it to automatically connect. The NetworkManager used by most Linux distributions also seems to make you chose the saved connection from a dropdown manually.)
In theory, it would be possible to save the known BSSIDs for each ESSID and only send the probe request when a beacon for one of them is received (that is, when you are near an access point which has in the past been used for that SSID). I do not know why nobody seems to have tried that yet.
This is not something that can be done simply through the OS.
The aforementioned Locations is one option, along with freeware Airport Location or MarcoPolo or paid Network Location. There is also an AppleScript called WiFiScriptor that could help.
However! you need not go into System Preferences->Network->[Location dropdown] . You can go through the Apple Menu->Location (about 6 options down)->[select the location you want]
-b
Best Answer
Here is an Applescript and shell script to check if Airport is connected and if not to connect it.
Replace
<networkName>
with the network's name and<passwordToNetwork>
with its password. The "en1" should be correct if you have not changed your Network devices.Now all you need to do is loop this or call it to check the connection.
Nevertheless airport should automatically reconnect if the connection is lost. Maybe try resetting all network preferences and see if that works.
How to completely reset your Network Preferences: Turn Airport off. Close System Preferences. Navigate to:
/Library/Preferences/SystemConfiguration/
Make a copy, then delete the following files: