Page 1 of 1

Call function ONE time if I have two incoming files?

Posted: Tue Dec 03, 2019 1:58 pm
by automation
I have switchscripter with two incoming files (one xml and one pdf). I get the size of them both and want to sum the size of the two files. I have tested a log but can't find out how I get ONE result (the calculated size) if I have two incoming files. I have tested to move the function outside the jobArrived function but it's still not working.

If the incoming file have Width 100 and Width 200 I want to return/log 300 in the calculation function.

Code: Select all

function jobArrived( s : Switch, job : Job )
{
var xmlSize, pdfSize;
//xmlSize = 100, pdfSize = 200
if(job.isType("xml")) {
	xmlSize = getXmlSize();
}

if(job.isType("pdf")) {
	pdfSize = getPdfSize();
}

function getXmlSize() {
	var theXML = new Document(job.getPath());
	var xmlWidth = theXML.evalToString("//PrintWidthCover");	

	return xmlWidth;
}

function getPdfSize() {
	var pdfWidth = job.getVariableAsNumber("[Stats.PageWidth]");	

	return pdfWidth;
}


calculateSize(xmlSize, pdfSize);

function calculateSize(xml, pdf) {
	s.log(2, "Result: " + xml + pdf);
}
}
The xml have the size of 100 and the pdf 200 and the expected result in he log I want is 300 (xml size + pdf size). But I get one size for each job with "undefined".

How can I call the function "calculateSize" ONE time and get one result, 300, the result of the xml size on 100 + pdf size on 200?

I get the result in the log if I use the code above:
Job *.XML - Result: 100undefined
Job *.PDF - Result: undefined200

Re: Call function ONE time if I have two incoming files?

Posted: Tue Dec 03, 2019 2:45 pm
by jan_suhr
In the FileStatistics class you can use getByteCount( ) : Number

http://www.enfocus.com/manuals/Develope ... class.html

Re: Call function ONE time if I have two incoming files?

Posted: Tue Dec 03, 2019 2:58 pm
by automation
jan_suhr wrote: Tue Dec 03, 2019 2:45 pm In the FileStatistics class you can use getByteCount( ) : Number

http://www.enfocus.com/manuals/Develope ... class.html
Hi!

I don't think you understand my question.

I get the Width of the pdf (not the file size).
and I read a Width from a XML file (not the size of the XML file it self).
I want to sum the Width of the pdf (not the file size) with the value from the XML.

Re: Call function ONE time if I have two incoming files?

Posted: Tue Dec 03, 2019 2:59 pm
by gabrielp
I think there's more than a few ways to handle this. Here are a couple of ideas: you could consume the XML as a dataset and attach it to the PDF with a metadata pickup. Then, you could send in the PDF and access the XML as a dataset within scripter. Another option would be to hold and assemble to put both files in a single folder before proceeding to your script. Then your incoming job is a folder you could look through to find both files.

Re: Call function ONE time if I have two incoming files?

Posted: Tue Dec 03, 2019 4:33 pm
by automation
gabrielp wrote: Tue Dec 03, 2019 2:59 pm I think there's more than a few ways to handle this. Here are a couple of ideas: you could consume the XML as a dataset and attach it to the PDF with a metadata pickup. Then, you could send in the PDF and access the XML as a dataset within scripter. Another option would be to hold and assemble to put both files in a single folder before proceeding to your script. Then your incoming job is a folder you could look through to find both files.
I can use a folder with the two files as a incoming connection.

But if I as a job send in like this

Code: Select all

--MyFolder
----somepdfname.pdf
----somexmlname.xml
How do get information of the files inside the folder. Ex if I want to get the size in a XML? sense the xml-file is in a folder I can't use this to get the size in var xmlWidth.

Code: Select all

	var theXML = new Document(job.getPath());
	var xmlWidth = theXML.evalToString("//PrintWidthCover");

Re: Call function ONE time if I have two incoming files?

Posted: Tue Dec 03, 2019 5:23 pm
by gabrielp
automation wrote: Tue Dec 03, 2019 4:33 pm
How do get information of the files inside the folder. Ex if I want to get the size in a XML? sense the xml-file is in a folder I can't use this to get the size in var xmlWidth.

Code: Select all

	var theXML = new Document(job.getPath());
	var xmlWidth = theXML.evalToString("//PrintWidthCover");

I don't have Switch anymore so take this pseudocode with a grain of salt. You could loop through the files in the folder with Dir.entryList (https://www.enfocus.com/manuals/Develop ... class.html)

Code: Select all

// For each helper function
var forEach = function(array, callback){
   var currentValue, index;
   var i = 0;
   for (i; i < array.length; i += 1) {
      if(typeof array[i] == "undefined"){
         currentValue = null;
      } else {
         currentValue = array[i];
      }
      index = i;
      callback(currentValue, i, array);
    }
}

// Check if job is a folder before doing this
var dir = new Dir(job.getPath());
var filesInFolder = dir.entryList("*", Dir.Files, Dir.Name);

forEach(filesInFolder, function(fileName, i){
	s.log(-1, "Picking up file " + fileName);
	// Determine if file is PDF or XML
	// If XML...
	var theXML = new Document(job.getPath() + fileName);
	var xmlWidth = theXML.evalToString("//PrintWidthCover");
});

Re: Call function ONE time if I have two incoming files?

Posted: Tue Dec 03, 2019 5:24 pm
by gabrielp
Do you not have the metadata module? XML pickup seems like an easier approach.

Re: Call function ONE time if I have two incoming files?

Posted: Tue Dec 03, 2019 5:52 pm
by jan_suhr
automation wrote: Tue Dec 03, 2019 2:58 pm
jan_suhr wrote: Tue Dec 03, 2019 2:45 pm In the FileStatistics class you can use getByteCount( ) : Number

http://www.enfocus.com/manuals/Develope ... class.html
Hi!

I don't think you understand my question.

I get the Width of the pdf (not the file size).
and I read a Width from a XML file (not the size of the XML file it self).
I want to sum the Width of the pdf (not the file size) with the value from the XML.

I am assuming you have one value of the width in the XML and then the PDF has its width an dyou want to add those values.

Simplest way to do that is to use XML Pickup to get the XML as a Dataset to the PDF. Then you can use the Switch.calculation to add the XML value from the dataset with the Stats.trimboxWidth and the resulting value can be used in Private data for processing later on in the flow.