Page 1 of 1

Help with created file extension

Posted: Fri May 23, 2014 6:54 pm
by jan_suhr
I have made a script that writes a file with some metadata from a submit point. Thanks to some snippets I have found on this forum :-)



But I have some problems with the code below:





function jobArrived( s : Switch, job : Job )

{

var InputPath = job.getPath();

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

var tempFile = job.createPathWithExtension(extension);



// var tempFile = new File(s.createPathWithName(job.getNameProper()+".mrg"));


The var extension gets an "Undefined" string as the file extension, at the moment I'm fixing that with a Rename element.



The commented line that should take the NameProper and ad .mrg doesn't work for me. I get errors that it can't open the file.



So there are two ways here.

Were do I get the value from "getPropertyValue" from? or how do we fix the getNameProper?





Thanks



Jan

Help with created file extension

Posted: Fri May 23, 2014 7:37 pm
by Evan
Hi Jan,

To get the "s.getPropertyValue("Extension")" you will have to create into your declaration pane a flow element called "Extension". When you will import your script element into Swith a new propertie called "Extension" will be available. You can define your extension there.



You can otherwise create a temporary path with your jobname with your desire extension:

var tempFile = job.createPathWithExtension("mrg");



Then create a file with your temporary path

var myFile = new File(tempFile);



This snipet could help you:



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)

}


To make it work you will have to create another flow element called "Text_to_Write"

--

Evan

Help with created file extension

Posted: Fri May 23, 2014 10:56 pm
by jan_suhr
Thanks Evan,



I should have known that. I hope Freddy doen't see this :-)



Jan

Help with created file extension

Posted: Tue May 27, 2014 3:29 pm
by freddyp
With a bit of a delay because I was traveling: -:)



Just happy to see you are benefitting from the forum and getting your stuff done, Jan!



Freddy

Help with created file extension

Posted: Tue May 27, 2014 3:47 pm
by jan_suhr
Over a year since I was at the training at Enfocus, so I had forgot and this was my first scripting since that.



Now it is working as supposed and the picks up metadata from one submit point and another script picks up metadata from an XML-file. They create a textfile for postprocessing in a media data bank.



One question though. The file (job) that triggers the script, how do I set up the script element to pass that job through, as it is now it vanishes and I have to have another line for it to pass to the out folder.



Both the image file and the text file has to end up in the same output folder.



I believe that it can be done smarter that to let it pass through two ways in the flow.





Thanks



Jan

Help with created file extension

Posted: Tue May 27, 2014 4:09 pm
by freddyp
The solution is already in Evan's code example. There are two job.sendToSingle statements, one that sends the text file and one that sends the input file.



When it is a script for a very specific case that is OK. For the general background I just want to add that the "proper" way of doing this would be to define the "Connection type" of the script in SwitchScripter as "Traffic light". Then you will get a Data connection and a Log connection and in the script you do a job.sendToData(1,job.getPath()) for the input file, and job.sendToLog(1,the_path_of_the_text_file_you_created) for the text file. You can also define the different levels (OK, Warning, Error) you want to allow and the corresponding parameter is of course 1, 2, and 3.



It is cleaner because it allows to treat the input job and the created text file in different ways, and it allows you to handle the error cases, eg when the text file cannot be written, the information you need is missing, etc. Again, this may not and will probably not apply in your particular case, and then the two job.sendToSingle lines are perfectly acceptable.



Freddy

Help with created file extension

Posted: Tue May 27, 2014 5:04 pm
by jan_suhr
I tried those two lines and with the "job.sendToSingle(InputJob);" the Script doesn't execute. The input job file is waiting in the folder before the Script Element.



Here is my script





// 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 InputPath = job.getPath();

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

var tempFile = job.createPathWithExtension(extension);

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



//Get field values from Submit point

var myPhotographer = job.getVariableAsString("[Metadata.Text:Path='/field-list/field[1]/value',Dataset='Mergefile',Model='XML']");

var myDescription = job.getVariableAsString("[Metadata.Text:Path='/field-list/field[2]/value',Dataset='Mergefile',Model='XML']");

var myKeywords = job.getVariableAsString("[Metadata.Text:Path='/field-list/field[3]/value',Dataset='Mergefile',Model='XML']");

var myJobName = job.getVariableAsString("[Metadata.Text:Path='/field-list/field[4]/value',Dataset='Mergefile',Model='XML']");

var myMagazine = job.getVariableAsString("[Metadata.Text:Path='/field-list/field[5]/value',Dataset='Mergefile',Model='XML']");

var myPhone = job.getVariableAsString("[Metadata.Text:Path='/field-list/field[6]/value',Dataset='Mergefile',Model='XML']");

var myEmail = job.getVariableAsString("[Metadata.Text:Path='/field-list/field[7]/value',Dataset='Mergefile',Model='XML']");

var myContact = job.getVariableAsString("[Metadata.Text:Path='/field-list/field[8]/value',Dataset='Mergefile',Model='XML']");

var myCustomer = job.getVariableAsString("[Metadata.Text:Path='/field-list/field[9]/value',Dataset='Mergefile',Model='XML']");



//Write Metadata to file.

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

myFile.writeLine("userName:" + myMagazine);

myFile.writeLine("13:");

if (myDescription != null)

{

myFile.writeLine("41:" + myDescription);

}

else

{

myFile.writeLine("41:");

}

myFile.writeLine("84:");

myFile.writeLine("42:");

myFile.writeLine("99:" + myJobName);

if (myCustomer != null)

{

myFile.writeLine("101:" + myCustomer);

}

else

{

myFile.writeLine("101:");

}

myFile.writeLine("98:" + myPhotographer);

myFile.writeLine("100:" + myPhone);

if (myKeywords != null)

{

myFile.writeLine("19:" + myKeywords);

}

else

{

myFile.writeLine("19:");

}

myFile.writeLine("102:" + myEmail);

if (myContact != null)

{

myFile.writeLine("103:" + myContact);

}

else

{

myFile.writeLine("103:");

}

myFile.close();

job.sendToSingle(InputJob);

job.sendToSingle(tempFile);

}




A couple of fields in the Submit point are not mandatory there for the IF-statements.





Jan

Help with created file extension

Posted: Tue May 27, 2014 7:00 pm
by jan_suhr
I found it. I hadn't specified the Var InputJob.



Now it works.





Jan