This is very hard to do reliability because all apps do NOT support the same scripting commands for manipulating windows.
Having said that you can take a look at a script I just posted which minimizes all windows except the frontmost for the current, frontmost app. Maybe you can adapt that for your needs.