Posted by member 321598 on 2006-08-12 06:01:37
Here's a little library I wrote for my HTA programs. Just copy and paste and save it as LSlib.js (or anything). And include it in your HTA.
There's basically 5 functions
(1)Read_Evars(File) - Reads all Evars from a file and returns two arrays of Evar Names and Evar Values.
(2)Save_Evars(File,Evar_Names,Evar_Vals) - Write the evars to the file, it overwrites everything in the destination file, so you have to have all the evars in it.
(3)Read_Evar(File,Evar_Name) - Reads a single evar from a file.
(4)Save_Evar(File,Evar_Name,Evar_Val) - Saves a single evar to a file.
(5)Set_Evar(Evar_Name,Evar_Val) - Uses !SetEvar from xLabel to set an evar value
(6)Exec_Bang(Bang) - Executes the Bang
I'm also thinking of adding a Get_evar function to get the evar value from memory, but I don't think I'll be using it though.
There's basically 5 functions
(1)Read_Evars(File) - Reads all Evars from a file and returns two arrays of Evar Names and Evar Values.
(2)Save_Evars(File,Evar_Names,Evar_Vals) - Write the evars to the file, it overwrites everything in the destination file, so you have to have all the evars in it.
(3)Read_Evar(File,Evar_Name) - Reads a single evar from a file.
(4)Save_Evar(File,Evar_Name,Evar_Val) - Saves a single evar to a file.
(5)Set_Evar(Evar_Name,Evar_Val) - Uses !SetEvar from xLabel to set an evar value
(6)Exec_Bang(Bang) - Executes the Bang
I'm also thinking of adding a Get_evar function to get the evar value from memory, but I don't think I'll be using it though.
//Get litestep process path
var objWMIService = GetObject("winmgmts:\\\\.\\root\\CIMV2");
var objProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'litestep.exe'");
var enumProcess = new Enumerator(objProcess);
var lspath = enumProcess.item().ExecutablePath;
//Create file objects for reading/writing text
var objPref=new ActiveXObject("Scripting.FileSystemObject");
//create shell
var objShell = new ActiveXObject("WScript.Shell");
//Function to read in evars
function Read_Evars(File) {
// Open Preference file
var objPFile=objPref.GetFile(File)
var objPData = objPFile.OpenAsTextStream(1, 0 );
//Define variables
var curr_line;var Curr_Evar=new Array();
Evar_Heads=new Array();Evar_Values=new Array();
// Read Evars
for(count=0; !objPData.AtEndOfStream; count++){
curr_line = objPData.ReadLine();
Curr_Evar = curr_line.split(" ");
Evar_Heads[count]=Curr_Evar[0];
Evar_Values[count]=Curr_Evar[1].slice(1,-1);
}
//close file
objPData.Close();
return [Evar_Heads,Evar_Values]
}
//Function to save evars
function Save_Evars(File,Evar_Names,Evar_Vals) {
// Open Preference file
var objPFile=objPref.GetFile(File)
var objPData = objPFile.OpenAsTextStream(2, 0 );
//Write Evars
for(count=0; count < Evar_Heads.length; count++){
objPData.Write(Evar_Names[count]+' "'+Evar_Vals[count]+'"\n');
}
//close file
objPData.Close();
}
function Read_Evar(File,Evar_Name) {
// Open Preference file
var objPFile=objPref.GetFile(File)
var objPData = objPFile.OpenAsTextStream(1, 0 );
//Define variables
var Evar_Val;
var curr_line; var Curr_Evar=new Array();;
Evar_Heads=new Array();Evar_Values=new Array();
// Read Evars
for(count=0; !objPData.AtEndOfStream; count++){
curr_line = objPData.ReadLine();
Curr_Evar = curr_line.split(" ");
Evar_Heads[count]=Curr_Evar[0];
if (Evar_Heads[count]==Evar_Name) {Evar_Val=Curr_Evar[1].slice(1,-1);break}
}
//close file
objPData.Close();
return Evar_Val
}
//Function to save a single evar
function Save_Evar(File,Evar_Name,Evar_Val) {
//Open Preference file
var objPFile=objPref.GetFile(File)
var objPData = objPFile.OpenAsTextStream(1, 0 );
//Read all data to string
var Text=objPData.ReadAll();
objPData.Close();
//Open again for writing
objPData=objPFile.OpenAsTextStream(2, 0 );
//Split at each line
Text=Text.split("\n");
//Loop through the file until evar is found
for (count=0;count < Text.length;count++){
if (Text[count].match(Evar_Name)) {objPData.Write(Evar_Name + ' "'+Evar_Val+'"\n');}
else{
objPData.Write(Text[count]);
//Prevent trailing empty lines
if (Text[count]!=""){objPData.Write("\n");}
}
}
objPData.Close();
}
//Function to set Evar value (Uses xLabel's !SetEvar)
function Set_Evar(Evar_Name,Evar_Val) {
//Create bang
Bang="!SetEvar "+Evar_Name+" "+Evar_Val;
//Pass bang to LS
Exec_Bang(Bang);
}
//Function to get Evar value
//Function to execute bang
function Exec_Bang(Bang){
//Pass bang to LS
objShell.Run(lspath + " " + Bang);
}
var objWMIService = GetObject("winmgmts:\\\\.\\root\\CIMV2");
var objProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'litestep.exe'");
var enumProcess = new Enumerator(objProcess);
var lspath = enumProcess.item().ExecutablePath;
//Create file objects for reading/writing text
var objPref=new ActiveXObject("Scripting.FileSystemObject");
//create shell
var objShell = new ActiveXObject("WScript.Shell");
//Function to read in evars
function Read_Evars(File) {
// Open Preference file
var objPFile=objPref.GetFile(File)
var objPData = objPFile.OpenAsTextStream(1, 0 );
//Define variables
var curr_line;var Curr_Evar=new Array();
Evar_Heads=new Array();Evar_Values=new Array();
// Read Evars
for(count=0; !objPData.AtEndOfStream; count++){
curr_line = objPData.ReadLine();
Curr_Evar = curr_line.split(" ");
Evar_Heads[count]=Curr_Evar[0];
Evar_Values[count]=Curr_Evar[1].slice(1,-1);
}
//close file
objPData.Close();
return [Evar_Heads,Evar_Values]
}
//Function to save evars
function Save_Evars(File,Evar_Names,Evar_Vals) {
// Open Preference file
var objPFile=objPref.GetFile(File)
var objPData = objPFile.OpenAsTextStream(2, 0 );
//Write Evars
for(count=0; count < Evar_Heads.length; count++){
objPData.Write(Evar_Names[count]+' "'+Evar_Vals[count]+'"\n');
}
//close file
objPData.Close();
}
function Read_Evar(File,Evar_Name) {
// Open Preference file
var objPFile=objPref.GetFile(File)
var objPData = objPFile.OpenAsTextStream(1, 0 );
//Define variables
var Evar_Val;
var curr_line; var Curr_Evar=new Array();;
Evar_Heads=new Array();Evar_Values=new Array();
// Read Evars
for(count=0; !objPData.AtEndOfStream; count++){
curr_line = objPData.ReadLine();
Curr_Evar = curr_line.split(" ");
Evar_Heads[count]=Curr_Evar[0];
if (Evar_Heads[count]==Evar_Name) {Evar_Val=Curr_Evar[1].slice(1,-1);break}
}
//close file
objPData.Close();
return Evar_Val
}
//Function to save a single evar
function Save_Evar(File,Evar_Name,Evar_Val) {
//Open Preference file
var objPFile=objPref.GetFile(File)
var objPData = objPFile.OpenAsTextStream(1, 0 );
//Read all data to string
var Text=objPData.ReadAll();
objPData.Close();
//Open again for writing
objPData=objPFile.OpenAsTextStream(2, 0 );
//Split at each line
Text=Text.split("\n");
//Loop through the file until evar is found
for (count=0;count < Text.length;count++){
if (Text[count].match(Evar_Name)) {objPData.Write(Evar_Name + ' "'+Evar_Val+'"\n');}
else{
objPData.Write(Text[count]);
//Prevent trailing empty lines
if (Text[count]!=""){objPData.Write("\n");}
}
}
objPData.Close();
}
//Function to set Evar value (Uses xLabel's !SetEvar)
function Set_Evar(Evar_Name,Evar_Val) {
//Create bang
Bang="!SetEvar "+Evar_Name+" "+Evar_Val;
//Pass bang to LS
Exec_Bang(Bang);
}
//Function to get Evar value
//Function to execute bang
function Exec_Bang(Bang){
//Pass bang to LS
objShell.Run(lspath + " " + Bang);
}