Rhino Script Compiler for Rhino5, second Beta

I think I figured out what was going wrong with your script. I can try to fix this in the core in the future, but for now here is a simple change you can make to get your script running.

At the beginning of your script, add another import

import scriptcontext

Right after you call the command that creates a new file, set the scriptcontext doc to the new active doc

...
rs.Command("_-New _None",False)
scriptcontext.doc = Rhino.RhinoDoc.ActiveDoc
...

I need to adjust the core to account for when the document changes in a script. The scriptcontext.doc variable was pointing at the old closed document and was therefore not giving valid results when things like views were being looked up.