The last part of your question has answered @steven.hamoen already at "create a Global Table in flexscript". Maybe you adjust my answer at the same question there to add global variables by source code.
EDIT: Try for Global Variables
treenode globalVariableNode = model().find("Tools/GlobalVariables");
treenode newVariable = createcopy(globalVariableNode.first,globalVariableNode,0);
newVariable.name = "myNewVariable";
newVariable.subnodes[1].value = "myNewVariable";
newVariable.subnodes[2].value = 4; // Type 1: int/ 2: double/ 3: treenode/ 4: string /5: Array
newVariable.subnodes[3].dataType = DATATYPE_STRING;
newVariable.subnodes[3].value = "MyData";// Array entries as Subnodes, Datatype is string
refreshglobalvariables();
buildnodeflexscript(model());
treenode globalVariableNode = model().find("Tools/GlobalVariables");
treenode newVariable = createcopy(globalVariableNode.first,globalVariableNode,0);
newVariable.name = "myNewVariable";
newVariable.subnodes[1].value = "myNewVariable";
newVariable.subnodes[2].value = 5; // Type 1: int/ 2: double/ 3: treenode/ 4: string /5: Array
newVariable.subnodes[3].dataType = DATATYPE_STRING;
newVariable.subnodes[3].value = "1";// Array entries as Subnodes, Datatype is string
newVariable.subnodes[3].subnodes.assert("1","3"); // the node name is typically blank
// then you can only add one entry
newVariable.subnodes[3].subnodes.assert("2","newvalue");
newVariable.subnodes[3].subnodes.assert("3","ddd");
refreshglobalvariables();
buildnodeflexscript(model());