Infuriatingly the problem has returned, despite the 2yb's fix which worked for a while.
This is what seems to happen:
The Wifi is working fine for a variable amount of time. Then it drops the signal, or loses the signal. Then it starts looking for a Wifi signal. At this point the Wi-Fi indicator still shows all its bars. However, it can't find the Wifi signal and the internet doesn't work. On some occasions it finds the signal and goes back to normal. However, on other occasions it is not able to find the signal however long you leave it. I then try to turn the Wifi off and on again. I right click on the Wifi signal and turn off the Wifi. The Wifi icon, as expected turns to white triangle without any bars. However, it is then not possible to turn it back on again, either by right clicking on it, or in network connections.
I will then restart the computer. Sometimes it starts working again, although usually not for long. Othertimes, the white triangle remains and it says 'no hardware installed' when I click on the Wifi icon. Sometimes this continues for several hours or even days. Then for no clear reason it all starts working again
I have tried absolutely everything that I can find to fix this:
-Reset the PRAM and SMC
-Delete the system configurations folder in Library>Preferences
-Change lots of settings in Network connections
-2yb's script above
Most of these sort out the problem for a few hours/days/weeks, but it always comes back.
At one point, stopping using Safari and changing to Firefox sorted out the problem. This worked for a couple of weeks, and I could recreate the problem by opening Safari, and I thought it was related to Safari. But, as always the problem came back, even with Firefox.
I've just reinstalled Mountain Lion (as I think this problem only started after installing Mountain Lion, although this could of course be coincidence). Wifi working now, but I expect the problem will recur soon.
Just to add, it is definitely not a problem with the router as other Mac and phones etc all working fine.
I get the feeling, that there is some software bug, in which when the Wifi loses a connection it sometimes turns itself off, and it becomes unable to turn itself on again and it all goes wrong. But I can't for the life of me work out where the problem is or how to fix it. Needless to say the Apple Genius Bar people haven't suggested anything cleverer.
If anyone has got any further ideas or solutions, please, please post it.
saul_eye