Write to file configurator using multi-lines with variable

Post Reply
Evan
Member
Posts: 30
Joined: Mon Feb 28, 2011 6:44 pm

Write to file configurator using multi-lines with variable

Post by Evan »

Hello,



Is there a way to write multi-lines text using variables to a file?



I want to write an XML using PDF metadata and XML data from my MIS.



The only way I found is to send an email, retreive it and inject message as file.



I'm looking now to create a custom configurator but it is a little hard without nothing to start with. All .sscript file I found are lock with a password.



Where I can find an unlock .sscript file? Is somebody can create me a configurator that will doing it? It should not be hard to do but I'm new to javascript.



Enfocus should add this tool in the next update. It is a basic thing to do giving a lot of possibilities.



Thanks!
dkelly
TOP CONTRIBUTOR
Posts: 628
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Write to file configurator using multi-lines with variable

Post by dkelly »

You can access variables via Switch's Javascript getVariableAsString() call.



Give me the name of variables you want to write and I'll generate you an example. Do you want each variable on a separate line, comma-separated, etc?
Evan
Member
Posts: 30
Joined: Mon Feb 28, 2011 6:44 pm

Write to file configurator using multi-lines with variable

Post by Evan »

Hi dkelly,



I want to be able to put free text around variable and create lines.



Here two variable



[Metadata.Text:Path="/powerswitch/preflight",Dataset="Xml",Model=XML]

[Stats.NumberOfPages]



The result should be a text file containing:





cmykcoated

32





Thanks for your time.


dkelly
TOP CONTRIBUTOR
Posts: 628
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Write to file configurator using multi-lines with variable

Post by dkelly »

This snippet should work; however, I haven't had time to test it in a real flow so there maybe a syntax issue or two with quoting.





var myFile = new File("/Users/dkelly/Desktop/out.xml");

myFile.open( File.WriteOnly | File.Truncate );

myFile.writeLine("<powerswitch>");

myFile.writeLine(" <preflight>" + job.getVariableAsString("[Metadata.Text:Path="/powerswitch/preflight",Dataset="Xml",Model=XML]", s) + "</preflight>");

myFile.writeLine(" <numberofpages>" + job.getVariableAsString("[Stats.NumberOfPages]", s) + "</numberofpages>");

myFile.writeLine("</powerswitch>");

myFile.close();

Evan
Member
Posts: 30
Joined: Mon Feb 28, 2011 6:44 pm

Write to file configurator using multi-lines with variable

Post by Evan »

Thanks dkelly it works but I don't want to write a new script every time. I found a better way like i was asking at first.



Now I can write everything I want using the multi-lines with variable dialog box and using all variable available into Powerswitch. I can also choose the file extension.



The script is:







function jobArrived( s : Switch, job : Job )

{

var extension = s.getPropertyValue("Extension");

var tempFile = job.createPathWithExtension(extension);

var myFile = new File(tempFile);

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)

}





For newbies like me:



For make it work a "Text_to_Write" and "Extension" Flow Element Properties in SwitchScripter must be create.



Then into Powerswitch choose into tool "Script Element" and choose the .sscript file you save from SwitchScripter.



This is my first configurator. No guaranty on it! It surely have more efficient way to doing it. But I think Enfocus should include something like this in the installation package.
dkelly
TOP CONTRIBUTOR
Posts: 628
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Write to file configurator using multi-lines with variable

Post by dkelly »

So you are putting the entire XML syntax in the "Text_to_Write" property?
Evan
Member
Posts: 30
Joined: Mon Feb 28, 2011 6:44 pm

Write to file configurator using multi-lines with variable

Post by Evan »

Exactly, I write the XML syntax directly into the multi-lines dialog box of Powerswith and I pickup varibale on the fly. It is easier that way.



What I like also is I can write other kind of file like txt, xdp or email.
User avatar
JimmyHartington
Member
Posts: 28
Joined: Tue Mar 22, 2011 7:38 am

Write to file configurator using multi-lines with variable

Post by JimmyHartington »

Hi



I have used this script to write a text-file.

It seems that the text-file is encoded as ISO-8859.

But I would like to get a UTF-8 encoded file.

Does anybody know how to do this?
Evan
Member
Posts: 30
Joined: Mon Feb 28, 2011 6:44 pm

Write to file configurator using multi-lines with variable

Post by Evan »

Hi Jimmy,



Try





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)

}





The line I change is:

var myFile = new File(tempFile, "UTF8");



--

Evan
User avatar
JimmyHartington
Member
Posts: 28
Joined: Tue Mar 22, 2011 7:38 am

Write to file configurator using multi-lines with variable

Post by JimmyHartington »

Hi Evan



Thanks. That worked perfect.



Now I have another question.



Could this script be modified to instead of writing a new file, then it would add a line to the begining of an incoming text-file?



For example to insert a header row to incoming tab-delimited text-files, which does not have the header row.
Evan
Member
Posts: 30
Joined: Mon Feb 28, 2011 6:44 pm

Write to file configurator using multi-lines with variable

Post by Evan »

Hi Jimmy,



This maybe not the more efficient way to do it but it works.



function jobArrived( s : Switch, job : Job ){

var extension = s.getPropertyValue("Extension");

var tempFile = job.createPathWithExtension(extension);

var myFile = new File(tempFile, "UTF8");

var InputPath = job.getPath();

var myHeader = s.getPropertyValue("Text_to_Write");

var inputFileText = File.read(InputPath);

myFile.open( File.WriteOnly | File.Truncate );

myFile.writeLine(myHeader);

myFile.writeLine(inputFileText);

myFile.close();

job.sendToSingle(tempFile);

job.sendToNull(InputPath);

}



--

Evan
User avatar
JimmyHartington
Member
Posts: 28
Joined: Tue Mar 22, 2011 7:38 am

Write to file configurator using multi-lines with variable

Post by JimmyHartington »

Hi Evan



Fantastic. This just works.



And if I need to add a line at the bottom, I imagine I would make a new variable linking to a new property and add it after myFile.writeLine(inputFileText);
Evan
Member
Posts: 30
Joined: Mon Feb 28, 2011 6:44 pm

Write to file configurator using multi-lines with variable

Post by Evan »

Exactly, to make your code more robust you should add a try-catch routine so if an error occur your job will go to the problem folder and an error log will be generated.



function jobArrived( s : Switch, job : Job )

{

try {

var extension = s.getPropertyValue("Extension");

var tempFile = job.createPathWithExtension(extension);

var myFile = new File(tempFile, "UTF8");

var InputPath = job.getPath();

var myHeader = s.getPropertyValue("Text_to_Write");

var inputFileText = File.read(InputPath);

myFile.open( File.WriteOnly | File.Truncate );

myFile.writeLine(myHeader);

myFile.writeLine(inputFileText);

myFile.close();

job.sendToSingle(tempFile);

job.sendToNull(InputPath);

}

catch(e)

{

job.fail(e)

}

}



--

Evan
Peter Kleinheider
Newbie
Posts: 17
Joined: Mon Dec 13, 2010 4:52 pm

Write to file configurator using multi-lines with variable

Post by Peter Kleinheider »

For creating XML files, I use a script from impressed called "XML magic".



a multiline filed with variables let you specify the structure of the resulting xml and looks like







& Element /customer jobs

& Element /customer/jobs job

+ Attribute /customer/jobs/job name [Job.NameProper]

+ Attribute /customer/jobs/job group [Job.Hierarchy:Index=1]

+ Attribute /customer/jobs/job outputtype [Job.Hierarchy:Index=2]

+ Element /customer/jobs/job options

+ Element /customer/jobs/job/options option

+ Attribute /customer/jobs/job/options/option[[last()] name printpdf

+ Text /customer/jobs/job/options/option[[last()] 1







The resulting XML looks like:













1











The script allows to modify the content of the job itself (if the job is a XML file) or modify the XML as dataset.



You can create new XML files or modify existing XML files.



This is the number 1 script I use when creating flows based on metadata.
Post Reply