PaperCut Print Script of the Month #9 – Everyone has an opinion
Topical and political for this month’s Advanced PaperCut Print Script
The Selectec pre-sales team developed an interesting idea to use PaperCut to record people’s opinions and not just their print & copy history. The how will be explained below, the why is anyone’s guess, but grab your coffee and let’s jump in.
The first part of this is just setting variables as we have done before so we will set the Title for the pop-up window, the question to ask and the choices the user can pick from.
var TITLE = "Referendum on the United Kingdom's membership of the European Union"; var QUESTION = "Should the United Kingdom remain a member of the European Union or leave the European Union?"; var CHOICES = ["Remain", "Leave", "Undecided"];
Now for something new we are going to set some options for the prompt which will help us hide the job details and set the default choice.
If you wanted to change the default option to something else you can modify the value that has been set for defaultChoice but you need to make sure it exists in your list of options.
var OPTIONS = { 'defaultChoice' : 'Undecided', 'hideJobDetails' : true, 'dialogDesc' : QUESTION, 'dialogTitle' : TITLE }
With the setup out of the way, all we need to do is show the prompt and then save the results so we can view them. When showing the prompt we will need to make sure we set the response to a variable, so we know what option has been selected.
This is a bit different to how we would normally create a prompt as we are not setting the text to display. Instead, we are simply using the Title and Description fields established in the options above. If you did wish to include extra text you can put it between the two ‘.
var vote = actions.client.promptForChoice('', CHOICES, OPTIONS);
We now know what the user has selected and the result is saved in the “vote” variable. We could use a bunch of “if statements” and have one for each possible choice. We could then base our action on the response but as we know that the end goal is to increase a counter by 1 we can use a “for loop” to go over each of the choices until we have a match.
for (i = 0; i < CHOICES.length; i++) { if (vote == CHOICES[i]) { // We will increase the number here } }
To save the votes, we will use the actions.utils.onCompletionIncrementNumberProperty() method that we used a couple of months ago to track virtual queue usage, but we are going to dynamically set the property by using ‘vote.’ + vote this will then give us the custom property script.user-defined.vote.[USERS-CHOICE] that we can search for in the config editor by using the ‘vote.’ prefix.
actions.utils.onCompletionIncrementNumberProperty('vote.' + vote, 1);
That is it for this month and as always the complete Advanced PaperCut Print Script is below.
If you have your own requirements for an Advanced 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 getting in touch with our sales team.
Get in touch today | sales@selectec.com
/* * Ask users to vote on important things like staying in the EU * * Read more at http://selectec.com/script-of-the-month-9-everyone-has-an-opinion/ */ function printJobHook(inputs,actions) { // Prompt Title var TITLE = "Referendum on the United Kingdom's membership of the European Union"; // What question are we going to ask? var QUESTION = "Should the United Kingdom remain a member of the European Union or leave the European Union?"; // Array of possible choices var CHOICES = ["Remain", "Leave", "Undecided"]; // Dictionary containing Prompt options var OPTIONS = { 'defaultChoice' : 'Undecided', 'hideJobDetails' : true, 'dialogDesc' : QUESTION, 'dialogTitle' : TITLE } // Analysis has not completed return if (!inputs.job.isAnalysisComplete) { return; } // Show the prommpt and set the response to vote var vote = actions.client.promptForChoice('', CHOICES, OPTIONS); // We are not bothered by Timeouts or those who don't want to vote if (vote == "TIMEOUT" || vote == "CANCEL") { return; } // Loop choices for (i = 0; i < CHOICES.length; i++) { // If vote = choice if (vote == CHOICES[i]) { // increase vote.[users-choice] by 1 actions.utils.onCompletionIncrementNumberProperty('vote.' + vote, 1); } } }