Use layer name when exporting

I’m new to scripting.
I wrote a script that export a jpg of objects on every single layer.
Now, when I launch the script I write the file name every time the window come up.

I’d like to automate this action so the exported jpg has the name of the layer.

Thanks
Ivan

# -*- coding: utf-8 -*-
import rhinoscriptsyntax as rs

#export a jpg of objects on every single layer
def ExportLayerObject():
    layers = rs.LayerNames()
    for layer in layers:
        if not rs.IsLayerEmpty(layer):
            rs.ObjectsByLayer(layer, True)
            rs.ZoomSelected()
            rs.Command(" _Invert _Hide")
            rs.Command(" _ViewCaptureToFile")
            rs.Command(" _Show")

ExportLayerObject()

ExportLayerObjects.py (435 Bytes)

Try with a - before _ViewCaptureToFile like-_ViewCaptureToFile
Test this first in rhino. Then you can see a lot of settings.
If you want to continue or exit the function use _Enter

Hope this helps.