Using a PaperCut Script using advanced scripting brings a whole new level to the control you have over a print job. That power can also bring with it a few technical issues if the code for the script is not perfect.

We are happy to troubleshoot script problems and below is a sample of our thoughts during the process.

We were sent a script that was not working as expected, you can see this script below.
<pre class="">function printJobHook(inputs, actions) {

if (!inputs.job.isAnalysisComplete) {
return;
}
// Title tag used by the email application
var EMAIL_APPLICATION_TITLES = [
'Microsoft Outlook',
'http://',
'https://',
'outbind://',
'Microsoft Office Outlook'
];

if (inputs.job.isColor &amp;&amp; matchesAny(inputs.job.documentName, EMAIL_APPLICATION_TITLES )) {

actions.job.convertToGrayscale()

return;
}

if (inputs.job.isColor)

var dialogOptions = {"defaultChoice":"Colour"};
var options = ["Mono", "Colour"];

var response = actions.client.promptForChoice("Are you sure you want to print in colour? :",options,dialogOptions);

if (response == "Mono") {
actions.log.debug("The user clicked Mono");
actions.job.convertToGrayscale();
}
else if (response == "Colour") {
actions.log.debug("The user clicked Colour");
}
else if (response == "CANCEL") {
actions.log.debug("The user clicked cancel on the classification");
actions.job.cancel();
}
else if (response == "TIMEOUT") {
actions.log.debug("The user did not respond within 5 minutes (default)");
actions.job.cancel();
}
else {
actions.log.debug("The user selected Document classification: " + response);
}
}

function matchesAny(str, matchStrs, actions) {
if (str == null || matchStrs == null) {
return false;
}

for (var i in matchStrs) {
if (str.match(matchStrs[i])) {
return true;
}
}
}
</pre>
<h3>Puting Things Right</h3>
Our first step is to always add the script to our own test installs of PaperCut and see what happens. In this instance, the script worked fine for us, which is a bit odd the customer was getting a message about an error displayed in PaperCut. The error in question was:
<pre class="">Error in "printJobHook" - Can't find method actions.client.promptForChoice(string,object,org.mozilla.javascript.Undefined). (printer-script#27</pre>
To break this down, we can see the message is informing us that on line 27 (printer-script#27) there is a problem with actions.client.promptForChoice, If you look at the parameters being used we have a string, an object and something that is undefined.

When we look at the line in the code we can see this:
<pre>var response = actions.client.promptForChoice("Are you sure you want to print in colour? :",options,dialogOptions);</pre>
So we know the string is the "Are you sure you want to print in colour?" and the Object is the options variable. This suggests the problem is with the dialogOptions, If we look at the dialogOptions variable in the script above, it all looks good, but for some reason, it is not being set. If we look at the line above it:
<pre>if (inputs.job.isColor)</pre>
Everything under this, in theory, should only run if the job is in colour. As a test, we sent another print job but this time selected black and white, and that was when we managed to reproduce the error.
There is a bit of a trick when it comes to if statements if you don't surround everything with { } it will only apply to the one line under it which in this case is the line that sets the dialogOptions.

To fix this, we suggested putting an { on line 22 and add another } on line 47. Luckily this didn't cause any actual problems for what they are using the script for as only B/W jobs would trigger the error but it is something to keep an eye on in the future.

While PaperCut comes with dozens of pre-written script recipes, we also provide a script creation service to our channel. If you have a requirement or have questions? Simply <a href="/contact/">get in touch</a> with your Account Manager.
<div class="well well-lg">
<h3 style="text-align: center;">Get in touch today | <a href="mailto:sales@selectec.com">sales@selectec.com</a></h3>
</div>
&nbsp;

&nbsp;

&nbsp;