Page 1 of 1
Writing a Text File via (Java) Scripting
Posted: Wed Jan 22, 2020 9:33 pm
by rgpepper
I searched the existing posts, didn't find an exact fit for what I want to be able to do. I'm simply looking for a code example showing the combining of perhaps a data field from the job, and perhaps some other static custom text, and writing it to a local file (on Windows), UTF-8. So: defining the file path and name, the guts of it and executing.
Re: Writing a Text File via (Java) Scripting
Posted: Thu Jan 23, 2020 7:01 am
by r.zegwaard
Hi,
If you take a look at :
https://www.enfocus.com/manuals/Develop ... class.html you can find this code :
Code: Select all
//copies the content of the jobfile (in UTF-8) to a new path
//read the contents of the jobfile
var str = File.read(job.getPath(), "UTF-8");
//create new path
var destPath = job.createPathWithName("dest." + job.getExtension());
//write the contents to destPath
File.write(destPath, str, 'UTF-8');
//send to single outgoing connection
job.sendToSingle(destPath);
This reads a file and writes it to a new file.
You can change the var str with other content.
Re: Writing a Text File via (Java) Scripting
Posted: Thu Jan 23, 2020 8:52 am
by JimmyHartington
Hi,
I have a script package, which does this.
You can download it here:
https://d.pr/f/xrVmAx+
It writes a UTF-8 file with the content you provide in the variables text-box.
Out is the new file and the original file.
The code probably could be better, so if any of the coding wizards have some improvements please let my know.
This is the scripts code:
Code: Select all
// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job )
{
var extension = s.getPropertyValue("Extension");
var tempFile = job.createPathWithExtension(extension);
var myFile = new File(tempFile, "UTF8");
var TextToWrite = s.getPropertyValue("Text_to_Write");
var InputJob = job.getPath();
myFile.open( File.WriteOnly | File.Truncate );
myFile.writeLine(TextToWrite);
myFile.close();
job.sendToSingle(InputJob);
job.sendToSingle(tempFile)
}
// Is invoked at regular intervals regardless of whether a new job arrived or not.
// The interval can be modified with s.setTimerInterval().
function timerFired( s : Switch )
{
}
Re: Writing a Text File via (Java) Scripting
Posted: Thu Jan 23, 2020 3:53 pm
by rgpepper
I forget, what is the .sfixture file used for?
Re: Writing a Text File via (Java) Scripting
Posted: Fri Jan 24, 2020 12:29 pm
by JimmyHartington
I do not know what the .sfixture is used for. But I would imagine it is used by the Script Editor.
Re: Writing a Text File via (Java) Scripting
Posted: Fri Jan 24, 2020 12:56 pm
by Padawan
When you work in scripted, then you can add a test job with test metadata etc in the fixture pane. This way you can test the script in scripter with the test data.
All the test data is stored in the sficture file. You don't need it to run the script in Switch.