Skip to content

Instantly share code, notes, and snippets.

@soundsnw
Last active January 23, 2019 19:24
Show Gist options
  • Save soundsnw/27d2e366e0f7fe196c6d5b435c1b8058 to your computer and use it in GitHub Desktop.
Save soundsnw/27d2e366e0f7fe196c6d5b435c1b8058 to your computer and use it in GitHub Desktop.
#!/bin/bash
# macOS printer script: if color queue is selected, revert to grayscale
# Get logged in user
loggedInUser=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
# If the print queues have been deleted, re-add them
if lpstat -a | grep -q -w "Color_Printer"
then
echo "Color_Printer exists";
else
/usr/sbin/lpadmin -p "Color_Printer" -v "smb://printserver/follow_you" -P "/usr/local/ppd/colour_printer.ppd" -o printer-is-shared=false -o auth-info-required=username,password -o printer-is-accepting-jobs=true -o printer-location='Follow You';
/usr/sbin/cupsenable "Color_Printer";
/usr/sbin/cupsaccept "Color_Printer";
fi
if lpstat -a | grep -q -w "Grayscale_Printer"
then
echo "Grayscale_Printer exists";
else
/usr/sbin/lpadmin -p "Grayscale_Printer" -v "smb://printserver/follow_you" -P "/usr/local/ppd/grayscale.ppd" -o printer-is-shared=false -o auth-info-required=username,password -o printer-is-accepting-jobs=true -o printer-location='Follow You';
/usr/sbin/cupsenable "Grayscale_Printer";
/usr/sbin/cupsaccept "Grayscale_Printer";
fi
# Makes black and white default if Color_Printer is currently chosen, does nothing if any other printer is selected
if sudo -u "$loggedInUser" lpstat -d | grep -q -w "Color_Printer"
then
echo "Color_Printer is standard, reverting to Grayscale_Printer";
defaults write /Library/Preferences/org.cups.PrintingPrefs UseLastPrinter -bool "FALSE";
sudo -u "$loggedInUser" /usr/bin/lpoptions -d "Grayscale_Printer";
/usr/bin/lpoptions -d "Grayscale_Printer";
else
echo "Color_Printer is not default, not changing to Grayscale_Printer";
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment