NSPrinter
Inherits From: NSObject
Conforms To: NSCoding, NSCopying NSObject (NSObject)
Declared In: AppKit/NSPrinter.h
Class Description
An NSPrinter object describes a printer's capabilities, such as whether the printer can print in color and whether it provides a particular font. An NSPrinter object represents either a particular make or type of printer, or an actual printer available to the computer.
There are two ways to create an NSPrinter:
. To create an abstract object that provides information about a type of printer rather than an object that represents an actual printer device, use the printerWithType: class method, passing a printer type (an NSString) as the argument. The printerTypes class method provides a list of the printer types recognized by the computer. Printer types are described in files written in PostScript Printer Description (PPD) format. The location of these files is platform dependent.
. To create or find an NSPrinter that corresponds to an actual printer device, use the printerWithName: class method, passing the name of a printer. The way you find out what the available printer names are depends on the platforms you are using.
Once you have an NSPrinter, there's only one thing you can do with it: Retrieve information regarding the type of printer or regarding the actual printer the object represents. You can't change the information in an NSPrinter, nor can you use an NSPrinter to initiate or control a printing job.
When you create an NSPrinter object, the object reads the file that corresponds to the type of printer you specified and stores the data it finds there in named tables. Printer types are described in files written in the PostScript Printer Description (PPD) format. Any piece of information in the PPD tables can be retrieved through the methods stringForKey:inTable: and stringListForKey:inTable:, as explained later. Commonly needed items, such as whether a printer is color or the size of the page on which it prints, are available through more direct methods (methods such as isColor and pageSizeForPaper:).
Note: To understand what the NSPrinter tables contain, you need to be acquainted with the PPD file format. This is described in PostScript Printer Description File Format Specification, version 4.0, available from Adobe Systems Incorporated. The rest of this class description assumes a familiarity with the concepts and terminology presented in the Adobe manual. A brief summary of the PPD format is given below; PPD terms defined in the Adobe manual are shown in italic.
PPD Format
A PPD file statement, or entry, associates a value with a main keyword:
*mainKeyword: value
The asterisk is literal; it indicates the beginning of a new entry.
For example:
*ModelName: "MMimeo Machine"
*3dDevice: False
A main keyword can be qualified by an option keyword:
*mainKeyword optionKeyword: value
For example:
*PaperDensity Letter: "0.1"
*PaperDensity Legal: "0.2"
*PaperDensity A4: "0.3"
*PaperDensity B5: "0.4"
In addition, any number of entries may have the same main keyword with no option keyword yet give different values:
*InkName: ProcessBlack/Process Black
*InkName: CustomColor/Custom Color
*InkName: ProcessCyan/Process Cyan
*InkName: ProcessMagenta/Process Magenta
*InkName: ProcessYellow/Process Yellow
Option keywords and values can sport translation strings. A translation string is a textual description, appropriate for display in a user interface, of the option or value. An option or value is separated from its translation string by a slash:
*Resolution 300dpi/300 dpi: " ... "
*InkName: ProcessBlack/Process Black
In the first example, the 300dpi option would be presented in a user interface as 300 dpi. The second example assigns the string Process Black as the translation string for the ProcessBlack value.
NSPrinter treats entries that have an *OrderDependency or *UIConstraint main keyword specially. Such entries take the following forms (the bracketed elements are optional):
*OrderDependency: real section mainKeyword [optionKeyword]
*UIConstraint: mainKeyword1 [optionKeyword1] mainKeyword2 [optionKeyword2]
There may be more than one UIConstraint entry with the same mainKeyword1 or mainKeyword1/optionKeyword1 value. Below are some examples of *OrderDependency and *UIConstraint entries:
*OrderDependency: 10 AnySetup *Resolution
*UIConstraint: *Option3 None *PageSize Legal
*UIConstraint: *Option3 None *PageRegion Legal
Explaining these entries is beyond the scope of this documentation; however, it's important to note their forms in order to understand how they're represented in the NSPrinter tables.
NSPrinter Tables
NSPrinter defines five key-value tables to store PPD information. The tables are identified by the names given below:
Name Contents
PPD General information about a printer type. This table contains the values for all entries in a PPD file except those with the *OrderDependency and *UIConstraint main keywords. The values in this table don't include the translation strings.
PPDOptionTranslation Option keyword translation strings.
PPDArgumentTranslation Value translation strings.
PPDOrderDependency *OrderDependency values.
PPDUIConstraints *UIConstraint values.
There are two principle methods for retrieving data from the NSPrinter tables:
. stringForKey:inTable: returns the value for the first occurrence of a given key in the given table.
. stringListForKey:inTable: returns an array of values, one for each occurrence of the key.
For both methods, the first argument is an NSString that names a keywhich part of a PPD file entry the key corresponds to depends on the table (as explained in the following sections). The second argument names the table that you want to look in. The values that are returned by these methods, whether singular or in an array, are always NSStrings, even if the value wasn't a quoted string in the PPD file.
The NSPrinter tables store data as ASCII text, thus the two methods described above are sufficient for retrieving any value from any table. NSPrinter provides a number of other methods, such as booleanForKey:inTable: and intForKey:inTable:, that retrieve single values and coerce them, if possible, into particular data types. The coercion doesn't affect the data that's stored in the table (it remains in ASCII format).
To check the integrity of a table, use the isKey:forTable: and statusForTable: methods. The former returns a boolean that indicates whether the given key is valid for the given table; the latter returns an error code that describes the general state of a table (in particular, whether it actually exists).
Retrieving Values from the PPD Table
Keys for the PPD table are strings that name a main keyword or main keyword/option keyword pairing (formatted as mainKeyword/optionKeyword). In both cases, you exclude the main keyword asterisk. The following example creates an NSPrinter and invokes stringForKey:inTable: to retrieve the value for an un-optioned main keyword:
/* Create an NSPrinter object for a printer type. */
NSPrinter *prType = [NSPrinter
printerWithType:@"My_Mimeo_Machine"]
NSString *sValue = [prType stringForKey:@"3dDevice" inTable:@"PPD"];
/* sValue is "False". */
To retrieve the value for a main keyword/option keyword pair, pass the keywords formatted as mainKeyword/optionKeyword:
NSString *sValue = [prType stringForKey:@"PaperDensity/A4"
inTable:@"PPD"];
/* sValue is "0.3". */
stringForKey:inTable: can determine if a main keyword has options. If you pass a main keyword (only) as the first argument to the method, and if that keyword has options in the PPD file, the method returns the empty string. If it doesn't have options, it returns the value of the first occurrence of the main keyword:
NSString *sValue = [prType stringForKey:@"PaperDensity" inTable:@"PPD"];
/* sValue is empty string*/
NSString *sValue = [prType stringForKey:@"InkName" inTable:@"PPD"];
/* sValue is "ProcessBlack" */
To retrieve the values for all occurrences of an un-optioned main keyword, use the stringListForKey:inTable: method:
NSArray *sList = [prType stringListForKey:@"InkName" inTable:@"PPD"];
/* [slist objectAtIndex:0] is "ProcessBlack",
[slist objectAtIndex:1] is "CustomColor",
[slist objectAtIndex:2] is "ProcessCyan", and so on. */
In addition, stringListForKey:inTable: can be used to retrieve all the options for a main keyword (given that the main keyword has options):
NSArray *sList = [prType stringListForKey:@"PaperDensity"
inTable:@"PPD"];
/* [slist objectAtIndex:0] is "Letter",
[slist objectAtIndex:1] is "Legal",
[slist objectAtIndex:2] is "A4", and so on. */
Retrieving Values from the Option and Argument Translation Tables
A key to a translation table is like that to the PPD table: It's a main keyword or main/option keyword pair (again excluding the asterisk). However, the values that are returned from the translation tables are the translation strings for the option or argument (value) portions of the PPD file entry. For example:
NSString *sValue = [prType stringForKey:@"Resolution/300dpi"
inTable:@"PPDOptionTranslation"];
/* sValue is "300 dpi". */
NSArray *sList = [prType stringListForKey:@"InkName"
inTable:@"PPDArgumentTranslation"];
/* [slist objectAtIndex:0] is "Process Black",
[slist objectAtIndex:1] is "Custom Color",
[slist objectAtIndex:2] is "Process Cyan", and so on. */
As with the PPD table, requesting an NSArray of NSStrings for an un-optioned main keyword returns the keyword's options (if it has any).
Retrieving Values from the Order Dependency Table
As mentioned earlier, an order dependency entry takes this form:
*OrderDependency: real section mainKeyword [optionKeyword]
These entries are stored in the PPDOrderDependency table. To retrieve a value from this table, always use stringListForKey:inTable:. The value passed as the key is, again, a main keyword or main keyword/option keyword pair; however, these values correspond to the mainKeyword and optionKeyword parts of an order dependency entry's value. As with the other tables, the main keyword's asterisk is excluded. The method returns an NSArray of two NSStrings that correspond to the real and section values for the entry. For example:
NSArray *sList = [prType stringListForKey:@"Resolution"
inTable:@"PPDOrderDependency"]
/* [slist objectAtIndex:0] = "10", [slist objectAtIndex:1] = "AnySetup" */
Retrieving Values from the UIConstraints Table
Retrieving a value from the PPDUIConstraints table is similar to retrieving a value from the PPDOrderDependency table: always use stringListForKey:inTable: and the key corresponds to elements in the entry's value. Given the following form (as described earlier), the key corresponds to mainKeyword1/optionKeyword1:
*UIConstraint: mainKeyword1 [optionKeyword1] mainKeyword2 [optionKeyword2]
The NSArray that's returned by stringListForKey:inTable: contains the mainKeyword2 and optionKeyword2 values (with the keywords stored as separate elements in the NSArray) for every *UIConstraints entry that has the given mainKeyword1/optionKeyword1 value. For example:
NSArray *sList = [prType stringListForKey:@"Option3/None"
inTable:@"PPDUIConstraints"]
/* [slist objectAtIndex:0] = "PageSize", [slist objectAtIndex:1] = "Legal",
[slist objectAtIndex:2] = "PageRegion", [slist objectAtIndex:3] = "Legal" */
Note that the main keywords that are returned in the NSArray don't have asterisks. Also, the NSArray that's returned always alternates main and option keywords. If a particular main keyword doesn't have an option associated with it, the string for the option will be empty (but the entry in the NSArray for the option will exist).
Finding an NSPrinter