[futurebasic] OS X Printer name and make/model

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2003 : Group Archive : Group : All Groups

From: Robert Purves <robert.purves@...>
Date: Tue, 10 Jun 2003 10:55:07 +1200
The demo clarifies (I think) the business of printer name versus 
printer make+model in OS X.

Robert P.


'~'A
'                           CPU : Carbon
'               DIM'd Vars Only : On
'                    CALL Req'd : Off
'~'B

/*
A printer in OS X has two identifying strings
  name           (for example Stylus_COLOR_740)
  make and model (for example EPSON Stylus COLOR 740)

The demo shows how to get these two strings
and how to replace ugly underscores with spaces.

Robert P.   10 June 2003
*/

#define CFArrayRef as Ptr
toolbox fn CFArrayGetValueAtIndex( CFArrayRef theArray, ¬
    CFIndex idx ) = Ptr

#define PMPrinter as Ptr // to Opaque Object representing a printer
toolbox fn PMPrinterGetMakeAndModelName( PMPrinter printer,¬
    CFStringRef *makeAndModel ) = OSStatus
toolbox fn PMSessionCreatePrinterList( PMPrintSession printSession, ¬
    CFArrayRef *printerList,¬
    CFIndex *currentIndex, PMPrinter *currentPrinter ) = OSStatus


end globals

local mode
local fn CreatePrinterList( session as PMPrintSession )
'~'1
dim as OSStatus         err
dim as CFIndex        @ currIndex
dim as PMPrinter      @ currPrinter
dim as CFArrayRef     @ list

list = 0
long if ( session )
err = fn PMSessionCreatePrinterList( session, @list, @currIndex, 
@currPrinter )
end if
end fn = list


local mode
local fn PascalFromCFString$( cfStr as CFStringRef )
'~'1
dim as Str255    s
dim as Boolean   ok
s[0] = 0
long if ( cfStr )
ok = fn CFStringGetPascalString( cfStr, @s, ¬
       sizeof( s ), _kCFStringEncodingMacRoman )
end if
end fn = s


local mode
local fn UnderscoreToSpace$( s as Str255 )
'~'1
dim as long j

j = instr( 1, s, "_" )
while ( j )
mid$( s, j, 1 ) = " "
j = instr( j + 1, s, "_" )
wend
end fn = s


local mode
local fn CurrentPrinterNameMakeModel$( session as PMPrintSession, ¬
    makeAndModel as Boolean )
'~'1
dim as Str255           s
dim as CFStringRef    @ name
dim as CFIndex        @ currIndex
dim as PMPrinter      @ currPrinter
dim as CFArrayRef     @ list

s[0] = 0
long if ( session )
if fn PMSessionCreatePrinterList( session, @list, ¬
    @currIndex, @currPrinter ) then exit fn
long if makeAndModel
if fn PMPrinterGetMakeAndModelName( currPrinter, @name ) then exit fn
xelse
name = fn CFArrayGetValueAtIndex( list, currIndex )
end if
s  = fn PascalFromCFString$( name )
CFRelease( list )
end if
end fn = fn UnderScoreToSpace$( s )



// Main program
'~'1
dim as OSStatus   err

// All Carbon printer stuff happens within a PMPrintSession.
// Create session if none exists
long if ( gFBPrintSession == _nil )
err = fn PMCreateSession( @gFBPrintSession )
if err then stop "PMCreateSession error " + str$( err )
end if
window 1

long if ( system( _sysVers ) >= 1000 )
print fn CurrentPrinterNameMakeModel$( gFBPrintSession, _false )
print fn CurrentPrinterNameMakeModel$( gFBPrintSession, _zTrue )
xelse
print "Requires OS X "
end if
do
HandleEvents
until 0