Setting a Default Printer

From AFP548 Wiki
Jump to: navigation, search

To set a default printer so that it shows as "Default" in the "Print & Scan" Preference pane, it can be done in the following fashion:

  • Make it so that the default printer is not the last one used. We will put this into /Library/Preferences so it effects all users (Note: If this is set in the User's org.cups.PrintingPrefs.plist, the User's setting will take precedence)
defaults write /Library/Preferences/org.cups.PrintingPrefs UseLastPrinter -bool False
  • Run this script to set the default printer. It expects the first (and only) argument to be the printer queue name you wish to set as default. There is no error handling, but the script will exit with the result of the attempt to set the default.
#!/usr/bin/python
 
import objc
import sys
 
# Seems I need to import _some_ Framework in PyObjC, but doesn't matter what it is
import Foundation
 
objc.parseBridgeSupport("""<?xml version='1.0'?>
<!DOCTYPE signatures SYSTEM "file://localhost/System/Library/DTDs/BridgeSupport.dtd">
<signatures version='1.0'>
<function name='PMPrinterCreateFromPrinterID'>
<arg type='^{__CFString=}'/>
<retval type='^{OpaquePMPrinter=}'/>
</function>
<function name='PMPrinterSetDefault'>
<arg type='^{OpaquePMPrinter=}'/>
<retval type='l' type64='i'/>
</function>
</signatures>
""", globals(), '/System/Library/Frameworks/ApplicationServices.framework/Frameworks/PrintCore.framework')
 
thePrinter = PMPrinterCreateFromPrinterID(sys.argv[1])
result = PMPrinterSetDefault(thePrinter)
sys.exit(result)

You should how be able to change the default printer at will using this python script.

For more information on the Core Printing API:
http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CorePrintRef/Reference/reference.html

Thanks to frogor and tvsutton on ##osx-server on irc.freenode.net