Re: How to switch the default printer?
Re: How to switch the default printer?
- Subject: Re: How to switch the default printer?
- From: Scott Ribe <email@hidden>
- Date: Sat, 17 Oct 2009 15:18:47 -0600
- Thread-topic: How to switch the default printer?
> Well... suppose from my application, I want to convert a Word document to PDF.
> How would I do that? I did quite a bit of research and the only way I found
> was to use CUPS-PDF and have Word print to that printer. Then, I can retrieve
> the converted PDF document. Is there any other way?
You are right, this is the only way.
So there's a couple of things you have to deal with, and in particular
things are different in 10.4 & 10.5. (I haven't actually tried this under
10.6 yet.) A particular quirk is that using lpoptions to set the default
printer does not work if "use last printer" is the option selected for
default printer in system preferences. The below code isn't your exact
answer since it depends on my own code for executing processes, and is C++,
but I think you can read it and get the idea:
SwitchToPrinterStk::SwitchToPrinterStk( const char *pname )
: mDidSwitch( false ), mOldDefaultPrinterIsLast( false )
{
ProcessResults pr = RunProcess( "/usr/bin/lpstat", "-d", NULL );
if( pr.exited && !pr.exitval )
{
int idx = pr.outstr.find( ": " );
if( idx >= 0 )
mOldPrinterName = pr.outstr.substr( idx + 2, pr.outstr.length()
- idx - 3 );
}
pr = RunProcess( "/usr/bin/defaults", "-currentHost", "read",
"com.apple.print.PrintingPrefs",
"UseLastPrinterAsCurrentPrinter", NULL );
if( pr.exited && !pr.exitval )
{
mOldDefaultPrinterIsLast = pr.outstr[0] == '1';
if( mOldDefaultPrinterIsLast )
RunProcess( "/usr/bin/defaults", "-currentHost", "write",
"com.apple.print.PrintingPrefs",
"UseLastPrinterAsCurrentPrinter", "0", NULL );
}
/*
10.5: lpoptions works to set default print iff "use last printer" is not
the default
defaults -currentHost read -globalDomain ColorSyncDevices
look for "DefaultDevice-prtr" = 24300;
*/
pr = RunProcess( "/usr/bin/lpoptions", "-d", pname, NULL );
mDidSwitch = pr.exitval == 0;
}
SwitchToPrinterStk::~SwitchToPrinterStk()
{
if( mOldPrinterName != "" )
RunProcess( "/usr/bin/lpoptions", "-d", mOldPrinterName.c_str(),
NULL );
if( mOldDefaultPrinterIsLast )
RunProcess( "/usr/bin/defaults", "-currentHost", "write",
"com.apple.print.PrintingPrefs",
"UseLastPrinterAsCurrentPrinter", "1", NULL );
}
--
Scott Ribe
email@hidden
http://www.killerbytes.com/
(303) 722-0567 voice
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden