The SSMS allows you to manually generate scripts for a database by right-clicking a database and selected Generate Scripts and selecting what elements you wish to be scripted. However, suppose you do wish do manually perform this task, how can it be automated?
Actually there is no way to automate it from SSMS, currently the best way to do this without third party commercial tools is by using Scriptio (which uses the MS SMO API) and can act as an improved replacement for the database publishing wizard (sqlpubwiz.exe).
Scriptio has been released to CodePlex where there is a on how to compile for use with SQL Server 2008 and is available at http://scriptio.codeplex.com/
Alternatively you can purchase one of the Compare/Diff tools from SQL Server add-on vendors which can function as a more full-featured replacement of the sql publishing wizard. You can choose a database, backup, or snapshot as the source, and then select a folder as the output location and the tool will dumps everything into a folder structure.