PaperCut Print Script

PaperCut Print Script of the Month #4 – Colour Only Extensions

PaperCut Print Script – Colour only extensions

 

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 here.

var fileExtensions = [ '.xls', '.ppt', '.doc', '.xlsx', '.pptx', '.docx' ];

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

if ( inputs.job.isColor ) {
 // Code will go here
}

Now, it is time to work out how we will get the information out of the array. Last month, 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

for ( i = 0; i < fileExtensions.length; i++ ){
 // More code will go here
}

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.

if ( inputs.job.documentName.indexOf( fileExtensions[i] ) !== -1 ) {
 return;
}

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

actions.client.sendMessage( "Colour jobs must be .xls(x), .ppt(x), .doc(x). Cancelling Job!" );
actions.job.cancel();

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 sales@selectec.com
megaphone

/*
 * White List Applications for Printing
 *
 * Read more at http://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 < 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();
  }
}