PaperCut Print Script of the Month #1 – Halloween Trick
Welcome to the first PaperCut Print Script of the month
As Halloween is approaching I’ve decided to take to the Selectec tradition of picking on Ross online. This month we will be creating a simple PaperCut print script which will cancel or double the cost of his print jobs depending on what second his job was sent. So, grab a cup of coffee and we can start when you’re ready.
So, the first thing we are going to do is create two variables, one for the user we are going to target and another to hold the seconds from the time the job was sent. Creating a variable is nice and easy in Javascript we just come up with a name that we can remember or easily recognise later and assign it a value.
var username = "Ross";
Our second variable is going to use the inputs.job.date method, and the seconds from the time the job was submitted.
var seconds = inputs.job.date.getSeconds();
Now that we have the variables we need let’s build out our if block. To make sure Ross has a hard time, we are only going to run the script if the job was submitted using his username using the inputs.job.username method. This could also be changed to a group of users by using inputs.user.isInGroup(group_name_variable).
if ( inputs.job.username == username ) { // The rest of our code will go here }
Now inside that if block we are going to add another if block to compare the second’s value against 2 numbers so if seconds is 11 or seconds is 22 we are going to cancel the job and send a message back to him to tell him it has been cancelled.
if ( seconds == 11 || seconds == 22 ) { actions.job.cancel(); actions.client.sendMessage( "Sorry the job has been cancelled" ); }
It wouldn’t be fun if we just left it like that, so let’s add another if using else if to check if seconds is 33 or 44 and if it is we are going to double the cost of his job and leave a comment in the job log so we know what happened.
else if ( seconds == 33 || seconds == 44 ) { actions.job.setCost( inputs.job.cost * 2 ); actions.job.addComment( "Cost doubled for Halloweeen trick" ) }
Now, we can end this with an else to catch anything else and leave it blank in an attempt to make him think it’s just random. We could of course use actions.job.convertToGrayscale() to convert the other jobs to Grayscale if we wanted to, but that might give away the game.
else { // Don't do anything }
That is it! You’ve made it to the end of the first monthly script, and hopefully you have some ideas of what you could do in your environment for a little bit of fun. The full script is below and if you have any ideas for a future script get in touch and let us know.
If you have your own requirements for a PaperCut 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
/* * Halloween User Trick * * Cancel or charge double for some jobs from a certain user * * Read more at http://selectec.com/script-of-the-month-1-halloween-trick/ */ function printJobHook(inputs, actions) { if ( !inputs.job.isAnalysisComplete ) { // No job details yet so return. return; } // The user we want to mess with var username = "Ross"; // Get the seconds from the time the job was sent var seconds = inputs.job.date.getSeconds(); if ( inputs.job.username == username ) { // If 11 or 22 seconds if ( seconds == 11 || seconds == 22 ) { // Cancel the job and let the user know actions.job.cancel(); actions.client.sendMessage( "Sorry the job has been cancelled" ) // Else If seconds is 33 or 44 } else if ( seconds == 33 || seconds == 44 ) { // Double the cost of his job and a comment to the job actions.job.setCost( inputs.job.cost * 2 ); actions.job.addComment( "Cost doubled for Halloweeen trick" ) } else { // Don't do anything } } }