Can we write line to text file from within script?

Have we got the ability to write line to file within a script?
Am wanting to keep a couple of .txt log files for updates on API setup especially at the beginning to check all is going as expected and help solve any issues that might come up once in real world environment.
It would be incredibly difficult to do with actions I think.
Might not even need a samba helper as expect jscript can probably do it anyway but struggling to find a solution, tried a few of the methods online but no luck getting any to work.

Yes, there are file.X() API helpers for this


##FileOperations [fileop] (Script)##

Script Name: FileOperations
Script Handler: fileop

Script:

function readfile(filename)
{
  return file.ReadFromFile(filename);
}

function writefile(filename,content)
{
  return file.WriteToFile(filename,content);
}

function appendfile(filename,content)
{
  return file.AppendToFile(filename,content);
}

function nowplaying(filepath)
{
  var artist = file.ReadFromFile(filepath+'\\Snip_Artist.txt');
  var song = file.ReadFromFile(filepath+'\\Snip_Track.txt');
  if (song=='') {
    return '<size 14>Click here to start Music</size>';
  }
  return '<italic>' + song + '</italic><linebreak/>' + artist;
}

/*
string Download(string url, string regex = "")
string Upload(string url, string content)
string ReadFromFile(string fileName)
void WriteToFile(string fileName, string content)
void AppendToFile(string fileName, string content)
*/

1 Like

Thanks Q.
Presumably if we format code with correct puntuation we could create a CSV log file rather than text?

Also was looking for the easiest way to update a program setting within a script.

There is no helper for this yet that I am aware of, but @emre has mentioned that he will likely add some in the future. This would be especially useful for getting/setting Local Settings (in memory).

That said, if it is a “global” DB setting, I do this all the time with SQL and sql.X() helpers. For example


function getSettingbydb(settingname) {
  var qry = "SELECT [Value] FROM [ProgramSettingValues] WHERE [Name]='"+settingname+"'";
  var r = sql.Query(qry).First;
  return r;
}

function updateSettingbydb(settingname,settingvalue) {
  var qry = "UPDATE [ProgramSettingValues] SET [Value]='"+settingvalue+"' WHERE [Name]='"+settingname+"'";
  var r = sql.ExecSql(qry);
  return r;
}
2 Likes

Thanks again Q, I did see that post when searching.
The write to file was my main one, presumably the script could use variables for file name?
How about directory? Will it create directory if not already there?
Was hoping to use date variables for directory to do daily log file in monthly folder in yearly folder.
As script will run every 10-15mins and want to log the updates to help sort any posible issues when live with real data.

Also was going to try putting a add line within the entity update loop to have a log for what data is actually updated. More so in the beginning to make adjusting any knock on errors as will know where it went wrong.

The program setting was as I was planning to save the Json post responce each time so rather than updating every room each time will be able to skip the update if nothing has changed.

Am getting much better with my scripts :wink:

@QMcKay any chance you can explain the difference between writefile and apendfile?

Does the file. helper have ability to create directory?


Suggests ‘fso.FolderExists’ and ‘fso.CreateFolder’.
fso is undefined, so tried ‘file.CreateFolder’ but object not supported.

var fso = new ActiveXObject("Scripting.FileSystemObject"); 

Seems to have done the trick


1 Like

writefile over-writes the entire file with content.
appendfile adds content to the end of the file, without over-writing previous content (like a log file).

1 Like

Am working on a way to call my list of api variables from a single list for all other scripts.
At the minute the plan is a variables list script which creates json array of values and girst part of other scripts will be to call the json from the 1 script and take the values as variables,
hows that sound? Any better sugestions?

You can include script functions from one script inside another script.

I don’t understand what your idea has to do with read/write files?

It doesnt :stuck_out_tongue:

Its not function im after its variables.

Handler: vars

function initialize() {
  var a='iamempty';
  var b=0;
}

Handler: myScript

// load var init script
var vars = script.Load('vars','initialize');
// init vars
vars.initialize();

function doSomething() {
  // do something
  alert('a:'+a + ' b:'+b + ' c:'+c);
}

{CALL:myScript.doSomething()}

Should produce:
a:iamempty b:0 c:undefined

1 Like

Are you ‘declaring’ vars outside the function?

In the second script, yes.

It loads the init script “globally” to the myScript handler, so that any functions in myScript have access to vars.initialize().

I get vars is undefined ?!?

Sorry, the script.Load portion should is defined as:

variable=script.Load("<handler>","<method1>","<method2>",...);

and so should be like this (I corrected it above):

// load variable initialization function
var vars = script.Load('vars','initialize');

This makes the handler name somewhat arbitrary, in that you could do this and effectively “rename” the handler


// load var init script
// "vars" handler renamed to "variables" via assignment
var variables = script.Load('vars','initialize');
//  ^^^^^^^^^                ^^^^   ^^^^^^^^^^
// newHandler     originalHandler     function

// init vars
variables.initialize(); // "vars" handler was renamed to "variables"
1 Like

Thanks but still strugling, not sure what im doing wrong;

Just when I thought I was getting hang of it, really want to get this bit sorted, will clean up my scripts allot.
Any ideas what my problem is @QMcKay

No idea, not yet. I didn’t test it - it was just theory. I am trying to get it to work now.

I have found that sometimes, when you get that type of error, you need to make corrections then restart SambaPOS to get it to “take”, since it seems the script does not unload/reload and gets “stuck” in an error state. It drives me crazy sometimes, when I am sure I have done it correctly and it still does not work.

Ok, so yah, after banging my head for a bit, I restarted. This works


##Variables [vars] (Script)##

Script Name: Variables
Script Handler: vars

Script:

var a='iamempty';
var b=0;

function initialize() {
  var varlist="";
  varlist+="a:"+a+"\r\n";
  varlist+="b:"+b+"\r\n";
  return "Variables initialized\r\n"+varlist;
}

##My Script [myScript] (Script)##

Script Name: My Script
Script Handler: myScript

Script:

// load var init script
var vars = script.Load("vars","initialize");
vars.initialize();
  
function doSomething() {
  return 'a:'+a + ' b:'+b;
}

1 Like