Writing a Text File via (Java) Scripting

Post Reply
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Writing a Text File via (Java) Scripting

Post 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.
r.zegwaard
Member
Posts: 93
Joined: Fri Jul 08, 2011 10:31 am
Location: The Netherlands

Re: Writing a Text File via (Java) Scripting

Post 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.
User avatar
JimmyHartington
Advanced member
Posts: 293
Joined: Tue Mar 22, 2011 7:38 am

Re: Writing a Text File via (Java) Scripting

Post 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.

Image

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 )
{
}
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Re: Writing a Text File via (Java) Scripting

Post by rgpepper »

I forget, what is the .sfixture file used for?
User avatar
JimmyHartington
Advanced member
Posts: 293
Joined: Tue Mar 22, 2011 7:38 am

Re: Writing a Text File via (Java) Scripting

Post by JimmyHartington »

I do not know what the .sfixture is used for. But I would imagine it is used by the Script Editor.
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: Writing a Text File via (Java) Scripting

Post 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.
Post Reply