def addRecentFile( application, fileName ) :

	if isinstance( application, Gaffer.Application ) :
		applicationRoot = application.root()
	else :
		applicationRoot = application

	try :
		applicationRoot.__recentFiles
	except __HOLE__ :
		applicationRoot.__recentFiles = []

	if fileName in applicationRoot.__recentFiles :
		applicationRoot.__recentFiles.remove( fileName )

	applicationRoot.__recentFiles.insert( 0, fileName )
	del applicationRoot.__recentFiles[6:]

	f = file( os.path.join( applicationRoot.preferencesLocation(), "recentFiles.py" ), "w" )
	f.write( "# This file was automatically generated by Gaffer.\n" )
	f.write( "# Do not edit this file - it will be overwritten.\n\n" )

	f.write( "import GafferUI\n" )
	for fileName in reversed( applicationRoot.__recentFiles ) :
		f.write( "GafferUI.FileMenu.addRecentFile( application, \"%s\" )\n" % fileName )

## A function suitable as the command for a File/Save menu item. It must be invoked from a menu which
# has a ScriptWindow in its ancestry.
dataset/ETHPy150Open ImageEngine/gaffer/python/GafferUI/FileMenu.py/addRecentFile