Difference between revisions of "Setting a Default Printer"

From AFP548 Wiki
Jump to navigation Jump to search
m (depends_on elements don't seem necessary)
 
(2 intermediate revisions 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>
  
* Run this script to set the default printer. Replace 'my_queue_name' with the name of the print queue you wish to make default
+
* 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.
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
#!/usr/bin/python
 
#!/usr/bin/python
  
 
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
Line 29: Line 30:
 
""", globals(), '/System/Library/Frameworks/ApplicationServices.framework/Frameworks/PrintCore.framework')
 
""", globals(), '/System/Library/Frameworks/ApplicationServices.framework/Frameworks/PrintCore.framework')
  
thePrinter = PMPrinterCreateFromPrinterID('my_queue_name')
+
thePrinter = PMPrinterCreateFromPrinterID(sys.argv[1])
 
result = PMPrinterSetDefault(thePrinter)
 
result = PMPrinterSetDefault(thePrinter)
print result
+
sys.exit(result)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
You should how be able to change the default printer at will using this python script.
 
You should how be able to change the default printer at will using this python script.
 +
 +
For more information on the Core Printing API:<br />
 +
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
 
Thanks to frogor and tvsutton on ##osx-server on irc.freenode.net
  
 
[[Category:Scripts]][[Category:Settings]]
 
[[Category:Scripts]][[Category:Settings]]

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