Difference between revisions of "Setting a Default Printer"

From AFP548 Wiki
Jump to navigation Jump to search
m (pass the first argument as the printer queue name, and exit the script with the result code of PMPrinterSetDefault)
 
(One intermediate revision by one other user not shown)
Line 3: Line 3:
 
* 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''')
 
* 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''')
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
defaults write /Library/Preferences/org.cups.PrintingPrefs UseLastPrinter False
+
defaults write /Library/Preferences/org.cups.PrintingPrefs UseLastPrinter -bool False
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 11: Line 11:
  
 
import objc
 
import objc
 +
import sys
  
 
# Seems I need to import _some_ Framework in PyObjC, but doesn't matter what it is
 
# Seems I need to import _some_ Framework in PyObjC, but doesn't matter what it is

Latest revision as of 16:45, 30 August 2012

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