Script Execution And Interface Execution Perform Differently

Hello,
this is a strange thing that I encountered in Maya. If you copy and run the below given code in Maya, it runs perfectly.
But if you perform the operation in parts as specified below, Maya crashes.

  • Execute in script editor till line number 9 'connectAttr -force newMesh.outMesh deleteComponent2.inputGeometry;'
  • Execute the next two commands using the interface. Values could be anything random.
  • Execute rest of the code in script editor. Line number 12 'polyCloseBorder -ch 1 $meshParent[0];' onwards .

  • Any logic behind this?

    _________________________CODE START_________________________________

    file -f -new;
    polyCube;
    delete "pCube1.f[5]" ;
    move -r -0.0659429 0 0 "pCube1.vtx[2]" ;
    delete "pCube1.f[3]" ;
    createNode mesh -n newMesh;
    $meshParent = listRelatives -p newMesh;

    /////////Inserting a modifier node between intermediate History Nodes/////////
    connectAttr -force polyTweak1.output newMesh.inMesh;
    connectAttr -force newMesh.outMesh deleteComponent2.inputGeometry;

    /////Following commands are supposed to be executed by the user//////
    move -r -1.215641 0 0 $meshParent[0]; //moving the modifier node away from main object to avoid confusion
    move -r 0 0.307836 0 "polySurface1.vtx[2]"; //this and more will be executed by user using the Maya interface

    /////Script execution resumes/////
    polyCloseBorder -ch 1 $meshParent[0]; //This is to flush out the last tweak information (if any) from the modifier mesh

    ///Following commands are executed to remove the effect of "polyCloseBorder" command//////////
    $temp = listConnections -destination false newMesh;
    $buff = listConnections -plugs true -destination false $temp[0];
    connectAttr -force $buff[0] newMesh.inMesh;

    _____________________________________CODE END___________________________________