tcable Posted February 23, 2015 Posted February 23, 2015 Hi I'm trying to develop a papercut script, but hit a bit of a brick wall, so i'm hoping someone who can code better than me can help! Basically what i am after is a single script that will do the following: If print job is colour, show an alert to ask if they want to print in colour, convert to greyscale, or cancel the job. Print/charge depending on selection Else If print job is greyscale, then show an alert asking if they want to continue, with either yes/no or print/cancel buttons I also want to put costs and various other bits in, but i can develop that once i've got the basics in. Tthe code that i have at the moment is basically what's already in papercut to alert every time, plus the colour/greyscale option combined using: function printJobHook(inputs, actions) { printJobHook1(inputs, actions); printJobHook2(inputs, actions); [color=#444444] } [/color], and isn't really what i am after. hook1 is the colour/grey/cancel hook 2 is the alert. but it charges after hook1 if they don't cancel the job, regardless of what is chosen in hook2 I can post what i have already if it helps, but i'm not sure it's going to Thanks
Cache Posted February 23, 2015 Posted February 23, 2015 (edited) This is one which I've been experimenting with which I'm tempted to put on for our staff in a few weeks, might help? I'm not an expert in papercut scripting so this is purely a bodge of 2 of papercuts scripts, but sounds like rather then multiple hooks, you just want a single hook with nested If then else? /* // Test if a user is in group called 'students'. if (inputs.user.isInGroup("students")) { // Add your action code here actions.log.debug("The user is in 'students' group."); } * Confirm jobs with a high number of pages * * Users printing jobs with many pages are asked via the client tool whether * they meant to print such a large document, giving them the opportunity to * cancel. The message is displayed in large red letters. This can be useful * for users who forget to enter a page range when printing and instead send * the whole document. */ function printJobHook(inputs, actions) { var LIMIT = 20; // Show message for jobs over 500 pages. /* * This print hook will need access to all job details * so return if full job analysis is not yet complete. * The only job details that are available before analysis * are metadata such as username, printer name, and date. * * See reference documentation for full explanation. */ if (!inputs.job.isAnalysisComplete) { // No job details yet so return. return; } if (inputs.user.isInGroup("Staff")) { if (inputs.job.totalPages > LIMIT) { /* * Job is larger than our page limit, so ask the user to confirm via * red warning message so we grab their attention. */ var message = "" + "This print job is over " + LIMIT + " pages. Please confirm the job." + ""; var response = actions.client.promptPrintCancel(message); if (response == "CANCEL" || response == "TIMEOUT") { // Cancel the job and exit the script. actions.job.cancel(); return; } // User pressed OK, so allow it to print - simply return taking no action. } } } I could be completely wrong and that is the point of multiple hooks in which case ignore me... Edited February 23, 2015 by Cache
tcable Posted February 23, 2015 Author Posted February 23, 2015 (edited) well this is what i have at the moment: /* * Color print jobs require user confirmation * * Color printing is expensive so users should be encouraged to print * in grayscale whenever they print in color. No confirmation is required * for grayscale jobs. */ function printJobHook1(inputs, actions) { /* * This print hook will need access to all job details * so return if full job analysis is not yet complete. * The only job details that are available before analysis * are metadata such as username, printer name, and date. * * See reference documentation for full explanation. */ if (!inputs.job.isAnalysisComplete) { // No job details yet so return. return; } if (inputs.job.isColor) { /* Color job, ask the use if they want to print. The job cost is displayed * prominently to encourage users to consider black and white printing instead. */ var response = actions.client.promptPrintCancel( "This print job is color" + " and costs " + inputs.utils.formatCost(inputs.job.cost) + ". You can save money by printing the job in grayscale. " + "Do you want to print this job?", {"dialogTitle" : "Color print job", "dialogDesc" : "Consider printing in grayscale to reduce costs"}); if (response == "CANCEL" || response == "TIMEOUT") { // User did not respond, cancel the job and exit script. actions.job.cancel(); return; } } } /* * Confirm jobs with a high number of pages * * Users printing jobs with many pages are asked via the client tool whether * they meant to print such a large document, giving them the opportunity to * cancel. The message is displayed in large red letters. This can be useful * for users who forget to enter a page range when printing and instead send * the whole document. */ function printJobHook2(inputs, actions) { var LIMIT = 0; // Show message for jobs over 0 pages. /* * This print hook will need access to all job details * so return if full job analysis is not yet complete. * The only job details that are available before analysis * are metadata such as username, printer name, and date. * * See reference documentation for full explanation. */ if (!inputs.job.isAnalysisComplete) { // No job details yet so return. return; } if (inputs.job.totalPages > LIMIT) { /* * Job is larger than our page limit, so ask the user to confirm via * red warning message so we grab their attention. */ var message = "" + "Please confirm the job." + ""; var response = actions.client.promptPrintCancel(message); if (response == "CANCEL" || response == "TIMEOUT") { // Cancel the job and exit the script. actions.job.cancel(); return; } // User pressed OK, so allow it to print - simply return taking no action. } } function printJobHook(inputs, actions) { printJobHook1(inputs, actions); printJobHook2(inputs, actions); } The main problem is it brings up two alert boxes, and i want to combine it so i only get one alert box. so yes i'm after a If, Else statement really. Edited February 23, 2015 by tcable
tcable Posted February 23, 2015 Author Posted February 23, 2015 actually looking at it again, i think this needs to be my starting point, as this seems to make more sense.: /* * Request user for grayscale/color printing * */ function printJobHook1(inputs, actions) { /* * This print hook will need access to all job details * so return if full job analysis is not yet complete. * The only job details that are available before analysis * are metadata such as username, printer name, and date. * * See reference documentation for full explanation. */ var CONVERT_MESSAGE = 'Your job contains color pages. Would you like to convert your job to grayscale? Selecting "No" will print the job in color, Selecting "YES" will change the job to greyscale.'; if (!inputs.job.isAnalysisComplete) { // No job details yet so return. return; } if (inputs.job.isColor) { /* Color job, ask the use if they want to print. */ if (inputs.client.isRunning) { // User is running the client software. Ask if they want to convert to grayscale or cancel. var response = actions.client.promptYesNo(CONVERT_MESSAGE); if (response == 'NO') { actions.log.debug('User chose to keep on printing in color'); } else if (response == 'TIMEOUT') { actions.log.debug('Dialog timed out, canceling job.'); actions.job.cancel(); } else { actions.log.debug('User chose to convert their job to grayscale.'); actions.job.convertToGrayscale(); } } else { actions.log.debug('Canceling job due to high number of color pages and user not running client software.'); actions.job.cancel(); } } } /* * Confirm jobs with a high number of pages * * Users printing jobs with many pages are asked via the client tool whether * they meant to print such a large document, giving them the opportunity to * cancel. The message is displayed in large red letters. This can be useful * for users who forget to enter a page range when printing and instead send * the whole document. */ function printJobHook2(inputs, actions) { var LIMIT = 0; // Show message for jobs over 0 pages. /* * This print hook will need access to all job details * so return if full job analysis is not yet complete. * The only job details that are available before analysis * are metadata such as username, printer name, and date. * * See reference documentation for full explanation. */ if (!inputs.job.isAnalysisComplete) { // No job details yet so return. return; } if (inputs.job.totalPages > LIMIT) { /* * Job is larger than our page limit, so ask the user to confirm via * red warning message so we grab their attention. */ var message = "" + "Please confirm the job." + ""; var response = actions.client.promptPrintCancel(message); if (response == "CANCEL" || response == "TIMEOUT") { // Cancel the job and exit the script. actions.job.cancel(); return; } // User pressed OK, so allow it to print - simply return taking no action. } } function printJobHook(inputs, actions) { printJobHook1(inputs, actions); printJobHook2(inputs, actions); }
MatthewL Posted February 23, 2015 Posted February 23, 2015 There is already built in scripts that will help you do the first half of what you want.
tcable Posted February 24, 2015 Author Posted February 24, 2015 The two scripts above are those already built into papercut! It's just combining them correctly that I am having trouble with. I think I need to create a function called something like if (inputs.job.isGrayscale) { and move the alerting code into there
spadam Posted February 24, 2015 Posted February 24, 2015 (edited) Haven't tested it but is this any use to you? /* * Request user for grayscale/color printing * */ function printJobHook(inputs, actions) { /* * This print hook will need access to all job details * so return if full job analysis is not yet complete. * The only job details that are available before analysis * are metadata such as username, printer name, and date. * * See reference documentation for full explanation. */ var LIMIT = 20; var CONVERT_MESSAGE = 'Your job contains color pages. Would you like to convert your job to grayscale? Selecting "YES" will change the job to greyscale. Selecting "No" will print the job in color . '; var LIMIT_MESSAGE = 'Your job is over ' + LIMIT + ' pages and needs to be confirmed. '; var convertWarning = false; var pagesWarning = false; var message = ""; if (!inputs.job.isAnalysisComplete || !inputs.client.isRunning) { // No job details yet or client not running so return. return; } //page limit exceeded if (inputs.job.totalPages > LIMIT) { pagesWarning = true; message += LIMIT_MESSAGE; } //color job if (inputs.job.isColor) { convertWarning = true; message += CONVERT_MESSAGE; } if(convertWarning){ var response = actions.client.promptYesNoCancel(message); } else { if(pagesWarning){ var response = actions.client.promptPrintCancel(message); } } if (response == "CANCEL" || response == "TIMEOUT") { // Cancel the job and exit the script. actions.log.debug('Job not printed due to ' + response); actions.job.cancel(); return; } else if (response == 'NO') { actions.log.debug('User chose to keep on printing in color'); } else if(response == 'YES'){ actions.log.debug('User chose to convert their job to grayscale.'); actions.job.convertToGrayscale(); } // User pressed OK, so allow it to print - simply return taking no action. } Edited February 24, 2015 by spadam
tcable Posted February 24, 2015 Author Posted February 24, 2015 That's looking good, I've applied it to a few printers here to give it a go. I wasn't getting very far with my script combining (kept getting script errors). I'll test it over the next few days and let you 1. know if it works, 2. if I tweak it, i'll explain why and post the tweaked script. Thanks for your help.
tcable Posted February 24, 2015 Author Posted February 24, 2015 Excellent, seems to work fine, just need to change the messages a little bit, and the font, but other than that, working just how I want it to. Thank you again for your help.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now