img

PaperCut Print Script of the Month #3 – Random Quote


PaperCut print script – Random quotes

 

After all of the fun from the holidays we have decided to keep this month’s PaperCut print script simple so we can let the mince pies properly digest, this month we are showing users random inspirational / motivation quotes.

So let’s grab a cup of coffee or tea if you don’t like the good stuff and let’s get started. Our print script this month will make use of an array and a couple of Math methods that are available to us in Javascript.

An Array is a handy way to store multiple values in a single variable each of the values will have an index (number) assigned to it so we can reference it later, It might be easier to think of this like the track listing on your favorite album (Origin of Symmetry by Muse if you wanted to know) where each song has it’s own number.

So to create our array we will create a new variable and assign some values separated by commas:

var quotes = [ "Hello","World","Selectec","PaperCut","Print Scripts" ];

So now we have an array that contains 5 values and we need to return one of them, There is a bit of a trick to this as computers start counting from 0 so get the value “Hello” back we would need to use quotes[0] and to get “World” we would need to use quotes[1]. As covered in a previous print script we are using to use the actions.client.sendMessage() method to show our values.

As a quick test you could use something like the line below to send a message to the user:

actions.client.sendMessage( quotes[0] );

But that would be to simple and wouldn’t be random at all so lets look at the first of the Math functions. Math.random() will return a pseudo-random number between 0 and 0.999999 now for those who have noticed we have 5 values in our array so the number generated is not going to help us. This is where we use a method array called Length this will tell us how many values are stored in our array so we just multiple Math.random() by that and end up with the line below:

actions.client.sendMessage( quotes[ Math.random() * quotes.length ] );

Now this gives us another problem as we would be looking for an array index that is between 0 and 5.999999 and this is where our last Math method comes into play. Math.Floor() will round our number down to the nearest whole number so 0.123456 would become 0 and 5.999999 would become 5. Now we have everything we need and our sendMessage line will end up looking like this:

actions.client.sendMessage( quotes[ Math.floor( Math.random() * quotes.length ) ] );

All that is left to do is fill your quotes array with any quotes you can find on the internet and you are good to go. Hopefully this helped you understand Arrays in print scripts and given you an idea you can use to help inspire or motivate your users.

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

/*
 * Random Quote
 * 
 * Show a random quote when a user prints
 *
 * Read more at http://selectec.com/script-of-the-month-3-random-quote/
 */
 
 function printJobHook( inputs, actions ) {
 
     if (!inputs.job.isAnalysisComplete) {
         // No job details yet so return.
         return;
     }
 
     // Array of quotes
     var quotes = [
         "Things work out best for those who make the best of how things work out. ~John Wooden",
         "If you are not willing to risk the usual you will have to settle for the ordinary. ~Jim Rohn",
         "All our dreams can come true if we have the courage to pursue them. ~Walt Disney",
         "Success is walking from failure to failure with no loss of enthusiasm. ~Winston Churchill",
         "Try not to become a person of success, but rather try to become a person of value. ~Albert Einstein",
         "Great minds discuss ideas; average minds discuss events; small minds discuss people. ~Eleanor Roosevelt",
         "I have not failed. I've just found 10,000 ways that won't work. ~Thomas A. Edison",
         "The whole secret of a successful life is to find out what is one's destiny to do, and then do it. ~Henry Ford",
         "What seems to us as bitter trials are often blessings in disguise.~ Oscar Wilde",
         "The distance between insanity and genius is measured only by success. ~Bruce Feirstein",
         "If you can't explain it simply, you don't understand it well enough. ~Albert Einstein",
         "Innovation distinguishes between a leader and a follower. ~Steve Jobs",
         "The starting point of all achievement is desire. ~Napolean Hill",
         "Courage is resistance to fear, mastery of fear - not absense of fear. ~Mark Twain",
         "Only put off until tomorrow what you are willing to die having left undone. ~Pablo Picasso",
         "You must expect great things of yourself before you can do them. ~Michael Jordan",
         "Failure is the condiment that gives success its flavor. ~Truman Capote",
         "Life is 10% what happens to me and 90% of how I react to it. ~Charles Swindoll",
         "Believe you can and you’re halfway there. ~Theodore Roosevelt",
         "Do or do not. There is no try. ~Yoda",
         "The only way to do great work is to love what you do. ~Steve Jobs",
         "We can’t help everyone, but everyone can help someone. ~Ronald Reagan"
     ];
 
     // Send message to the PC Client with a random quote from the array
     actions.client.sendMessage( quotes[ Math.floor( Math.random() * quotes.length ) ] );
 
 }
PaperCut print script
Latest News from Nick Kean
img

Drivve Image 9

Drivve Image 9 has just been released! It comes with lots of great new...

Written by: Nick Kean

More
img

Drivve Image v8

Premium scanning solution Drivve Image is now on version 8. Drivve Image v8 h...

Written by: Nick Kean

More

img

You guys are a pleasure to do business with your support is fantastic I wish all suppliers behaved like you

Duncan Vass Dell Ltd
Back to Top ↑