I'm trying to export separate layers too. So far, I have the script below, and it's working OK. It exports only named layers. The script is so simple, I'm amazed this function is not built in!

If anyone knows how to get the current painting's filename and path as the baseName, or some other way I can replace the InputBox with something more elegant, help would be much appreciated.

Code:
<Events>

int i;
string baseName;
string layerName;
string fileName;
flag overwriteExisting = false;

InputBox( "Base name: $$baseName" );

for ( i = 0; i < LayerCount(); i++ ) {

	layerName = LayerName( i );

	if ( layerName != "" ) {
	
		fileName = baseName + "_" + layerName + ".png";
		
		if ( FileExists( fileName ) ) {
		
			if ( !overwriteExisting ) {
				overwriteExisting = YesNoBox( fileName + " already exists. Overwrite layer files?" );
				
				if ( !overwriteExisting ) {
					MessageBox( "Cancelling export" );
					exit;
				}
				
			}
			
			if ( overwriteExisting ) {
				FileDelete( fileName ); // overwrites existing files
			}
			
		}
		
		Wait: 0.000s	EvType: Command	CommandID: ExportLayer	Idx: i	Channels: NO	Path: fileName	
		
	}
	
}