Created
October 12, 2010 02:37
-
-
Save atr000/621579 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <Carbon/Carbon.h> | |
#import <Cocoa/Cocoa.h> | |
void usage(void) { | |
NSLog(@"Usage: pulpmill -l|<printer>"); | |
} | |
int main (int argc, const char * argv[]) { | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
OSStatus status; | |
CFArrayRef printers; | |
PMPrinter printer; | |
NSString *targetDefaultPrinter; | |
NSString *printerName; | |
BOOL isDefault; | |
int i; | |
char c; | |
BOOL list_only = NO; | |
// No -l or no printer, generate usage | |
if (argc == 1) { | |
usage(); | |
return(-1); | |
} | |
// Parse options | |
opterr = 0; | |
while ((c = getopt(argc, (char **)argv, "l")) != -1) | |
switch (c) { | |
case 'l': | |
list_only = YES; | |
break; | |
case '?': | |
usage(); | |
return(-1); | |
break; | |
} | |
// We had a printer specified on the command line | |
if (optind == 1) { | |
targetDefaultPrinter = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding]; | |
} | |
// Obtain printer list | |
status = PMServerCreatePrinterList(kPMServerLocal, &printers); | |
// Loop through printers | |
for (i = 0; i < CFArrayGetCount(printers); i++) { | |
printer = (PMPrinter)CFArrayGetValueAtIndex(printers, i); | |
printerName = (NSString *)PMPrinterGetName(printer); | |
isDefault = PMPrinterIsDefault(printer); | |
// Only showing the list | |
if (list_only) { | |
NSLog(@"%@%@", printerName, PMPrinterIsDefault(printer) ? @" (default printer)" : @""); | |
// Setting default printer | |
} else { | |
// We do a prefix compare because that's easier on the usage, case insensitive | |
NSRange range = [printerName rangeOfString:targetDefaultPrinter options:(NSCaseInsensitiveSearch)]; | |
if (range.location != NSNotFound) { | |
NSLog(@"pulpmill: Setting default printer to '%@'", printerName); | |
status = PMPrinterSetDefault(printer); | |
if (status == 0) { | |
// Now set printing preferences to disable UseLastPrinterAsCurrentPrinter | |
CFPreferencesSetValue(CFSTR("UseLastPrinterAsCurrentPrinter"), | |
kCFBooleanFalse, | |
CFSTR("com.apple.print.PrintingPrefs"), | |
kCFPreferencesCurrentUser, | |
kCFPreferencesAnyHost); | |
CFPreferencesSynchronize(CFSTR("com.apple.print.PrintingPrefs"), kCFPreferencesCurrentUser, kCFPreferencesAnyHost); | |
return(0); | |
} else { | |
NSLog(@"pulpmill: failed, OSStatus error %d", status); | |
return(1); | |
} | |
} | |
} | |
} | |
if (! list_only) { | |
NSLog(@"pulpmill: '%@' didn't match any printers; use -l to list", targetDefaultPrinter); | |
return(2); | |
} | |
[pool drain]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment