I think if you add the line
app.runMenuItem(stringIDToTypeID('selectNoLayers'));
just after
var theLayers = GetSelectedLayers();
the script will do what you want.
I think if you add the line
app.runMenuItem(stringIDToTypeID('selectNoLayers'));
just after
var theLayers = GetSelectedLayers();
the script will do what you want.