As 1000's of children march to school dressed as Maltida or a BFG we thought it a good idea to do something to honour <a href="http://www.roalddahl.com/create-and-learn/join-in/roald-dahl-day" target="_blank">Roald Dahl</a> day.

This script shows off the ability to allow documents to be printed at zero cost depending on the file name. While we have gone with Roald Dahl book names, other applications could be invoice or statement depending on the customer's needs.

The script is built around a few very simple concepts. Grab a cup of tea and we can begin.

First of all we can set an optional comment for the logs, this will help admins work out why a job ended up being zero cost.
<pre>// Comment to apply to job
var JOB_COMMENT = "Free Print Job Roald Dahl 100th Anniversary";
</pre>
After that we have an array, you may remember we went into details about arrays in <a href="https://staging.selectec.com/script-of-the-month-3-random-quote/" target="_blank">SOTM#3</a>. The array in question holds a list of keywords and in this instance the keywords are Rhald Dahl's greatest stories. Note - All of this depends on the file name being useful, not all applications publish the file name when the send a job to the print queue. It could be that the application you print from names all print jobs the same.
<pre class="">// Array of Book Names
var BOOK_NAMES = [
  "The Gremlins",
  "Over To You",
  "Some Time Never",
  "Someone Like You",
  "Kiss Kiss",
  "James and the Giant Peach",
  "Charlie and the Chocolate Factory",
  "The Magic Finger",
  "Fantastic Mr Fox",
  "Charlie and the Great Glass Elevator",
  "Danny, the Champion of the World",
  "The Wonderful Story of Henry Sugar and Six More",
  "The Enormous Crocodile",
  "My Uncle Oswald",
  "The Twits",
  "George's Marvellous Medicine",
  "Revolting Rhymes",
"The BFG",
"Dirty Beasts",
"The Witches",
"Roald Dahl’s Book of Ghost Stories",
"Boy: Tales of Childhood",
"The Giraffe and the Pelly and Me",
  "Two Fables",
  "Going Solo",
  "Matilda",
  "Rhyme Stew",
  "Ah, Sweet Mystery of Life",
  "Esio Trot",
  "The Vicar of Nibbleswicke",
  "The Minpins",
  "Roald Dahl's Guide to Railway Safety",
  "My Year"
];
</pre>
Last up we have the guts of the script. We tell the script to look at the jobname and if it matches any in the list add the comment. We used toLowerCase to work around any case sensitive problems with file names.
<pre class="">  // If jobname matches any of the book names
if (matchesAny(inputs.job.documentName.toLowerCase(), BOOK_NAMES.toLowerCase())) {
  // Add comment to job
  actions.job.addComment(JOB_COMMENT);
</pre>
if everything matches up we set the cost to zero using the actions.job.setcost option.
<pre class="">// Set the cost to 0 
actions.job.setCost(0);
</pre>
That is it for this month and as always the complete print script is below. If you have your own requirements for a print script and you don’t have the time or in-house experience to make it then simply contact us.
<div class="well well-lg">
<h3 style="text-align: center;">Get in touch today | 01256 246970 | <a href="mailto:sales@selectec.com">sales@selectec.com</a></h3>
</div>
<pre class="">/*
* Keyword(s) = Free Printing
*
* Read more at https://staging.selectec.com/papercut-print-script-month-12-keywords-free-printing/
*/
function printJobHook( inputs, actions ) {
  // Comment to apply to job
  var JOB_COMMENT = "Free Print Job Roald Dahl 100th Anniversary";

  // Array of Book Names
  var BOOK_NAMES = [
    "The Gremlins",
    "Over To You",
    "Some Time Never",
    "Someone Like You",
    "Kiss Kiss",
    "James and the Giant Peach",
    "Charlie and the Chocolate Factory",
    "The Magic Finger",
    "Fantastic Mr Fox",
    "Charlie and the Great Glass Elevator",
    "Danny, the Champion of the World",
    "The Wonderful Story of Henry Sugar and Six More",
    "The Enormous Crocodile",
    "My Uncle Oswald",
    "The Twits",
    "George's Marvellous Medicine",
    "Revolting Rhymes",
    "The BFG",
    "Dirty Beasts",
    "The Witches",
    "Roald Dahl’s Book of Ghost Stories",
    "Boy: Tales of Childhood",
    "The Giraffe and the Pelly and Me",
    "Two Fables",
    "Going Solo",
    "Matilda",
    "Rhyme Stew",
    "Ah, Sweet Mystery of Life",
    "Esio Trot",
    "The Vicar of Nibbleswicke",
    "The Minpins",
    "Roald Dahl's Guide to Railway Safety",
    "My Year"
  ];

  if (!inputs.job.isAnalysisComplete) {
    // No job details yet so return.
    return;
  }

  // If jobname matches any of the book names
  if (matchesAny(inputs.job.documentName.toLowerCase(), BOOK_NAMES)) {
    // Add comment to job
    actions.job.addComment(JOB_COMMENT);
    // Set the cost to 0
    actions.job.setCost(0);
  }
}

// Function borrowed from PaperCut Recipe (Confirm printing emails in color (from Outlook))
function matchesAny(str, matchStrs, actions) {
  if (str == null || matchStrs == null) {
    return false;
  }

  for (var i in matchStrs) {
    if (str.match(matchStrs[i]).toLowerCase()) {
      return true;
    }
  }

  return false;
}
</pre>