<h2>PaperCut Print Script - Colour only extensions</h2>
&nbsp;

This month's PaperCut Print Script keeps it simple and highlights how we can only allow colour printing from certain applications (for example, Excel, Powerpoint, etc.) based on the file extension. In this example, all mono printing is permitted, and colour is cancelled if not from one of the pre-defined whitelisted applications

Before you get excited, this will not work for all applications. It depends on how the print job from the application arrives in a print queue. If there is no extension being sent, then a bit of research would need to be done to work out what the applications send instead that can be used to identify it in the queue. With that small disclaimer out of the way, it is time to grab a cup of coffee and get started.

This month, we start out with an array which will contain the list of extensions we want to allow colour printing for. If you missed last month's PaperCut Print Script, where we first introduced arrays, you can catch up <a href="/script-of-the-month/script-of-the-month-3-random-quote/">here</a>.
<pre class="">var fileExtensions = [ '.xls', '.ppt', '.doc', '.xlsx', '.pptx', '.docx' ];</pre>
Next up is to work out if the job was actually sent in colour. All of our code will be in this if block to make sure it only attempts to run the rest of the script for colour jobs Luckily PaperCut has a handy method we can use to do this
<pre class="">if ( inputs.job.isColor ) {
 // Code will go here
}</pre>
Now, it is time to work out how we will get the information out of the array. <a href="/script-of-the-month-3-random-quote/">Last month</a>, we directly specified the index to use using something like fileExtensions[1] (in this example, that would return .ppt), but we actually want to work with each value and be able to easily change what extension we are allowing. For this, we are going to a for loop which at first might look complicated but it will all make sense
<pre class="">for ( i = 0; i &lt; fileExtensions.length; i++ ){
 // More code will go here
}</pre>
So what we are doing is starting out by setting the value of i to 0 and checking that the value of i is less than the number of extensions in the array if it is we will add 1 to the value of i on the next run. Remember how we manually set the index last month well this month we are making the computer do the work to check each value. This means that on the first loop the value of fileExtensions[i] will be .xls, on the second loop i will equal 1 so fileExtensions[i] will be .ppt and it will keep going through them until the value of i matches what we have in the array.

Now we have that out of the way... lets look at how to work out if the document name contains the extension we are looking for we can use inputs.job.documentName and the indexOf method that can be used on Strings, indexOf will tell us the position that the search string appears and will return a -1 if it doesn't appear at all. So for this example, we only want to proceed if the extension is found in the filename but the position could be anywhere so for our check we will just make sure the result is not -1.
<pre class="">if ( inputs.job.documentName.indexOf( fileExtensions[i] ) !== -1 ) {
 return;
}</pre>
Now that is all done outside of the loop we can send a message to the client to tell them the document is not allowed to be printed in colour and cancel the job
<pre class="">actions.client.sendMessage( "Colour jobs must be .xls(x), .ppt(x), .doc(x). Cancelling Job!" );
actions.job.cancel();</pre>
As always the complete PaperCut Print Script is below and if you have your own requirements for a print script and you don’t have the time or in-house experience to make it you can find out how we can help you by sending an email to our sales team at <a href="mailto:sales@selectec.com">sales@selectec.com
<img class="size-full wp-image-1435 aligncenter" src="https://staging.selectec.com/wp-content/uploads/2015/11/megaphone.gif" alt="megaphone" width="86" height="87" /></a>
<pre class="">/*
 * White List Applications for Printing
 *
 * Read more at https://staging.selectec.com/script-of-the-month-4-color-only-extensions
 */
 
function printJobHook(inputs, actions) {
  
  if (!inputs.job.isAnalysisComplete) {
    return;
  }
  
  var fileExtensions = [ ".xls", ".ppt", ".doc", ".xlsx", ".pptx", ".docx" ];
  
  if (inputs.job.isColor){
    for (i = 0; i &lt; fileExtensions.length; i++) { 
      if (inputs.job.documentName.indexOf( fileExtensions[i] ) !== -1 ) {
        return;
      }
    }
    actions.client.sendMessage( "Colour jobs must be .xls(x), .ppt(x), .doc(x). Cancelling Job!" );
    actions.job.cancel();
  }
}

</pre>