knowing if a printer is connected

Peter Brigham MD pmbrig at gmail.com
Sat Nov 21 07:46:17 EST 2009


Here's what I've come up with, in case anyone else needs a solution to  
this problem. If the printer is not connected or is off, the user is  
asked to check the connection, and if there is a live printer  
connected and it is not the one currently chosen in the print  
preferences panel then the user is asked to select the printer to use.

Scripts appear below (watch linewraps). The getActivePrinter()  
function will need adjusting to cope with all the possibilities for  
printer names and other USB device types, but this setup is now  
working on my system -- it will break, however, if all non-printer USB  
devices are not filtered out, so this will need some error-checking.

Again, anyone know the significance of the number after the "@"  
character in the shell("ioreg") results? It looks like a hex value.  
Port number? Or something specific to the device type?

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig

----------

-- before any printing, insert this snippet, eg,:
-- at the start of a "print" button handler

if not checkPrinter() then
    uChoosePrinter
end if

----------

-- the following handlers go in the stack script
-- or a library script

function checkPrinter
    put getActivePrinter() into tLivePrinter
    --  DeskJet 845C
    put getDefaultPrinter() into tDefaultPrinter
    --  HP DESKJET 845C
    return tLivePrinter is in tDefaultPrinter
end checkPrinter

on uChoosePrinter
    put getActivePrinter() into tActivePrinter
    if tActivePrinter = empty then
       put "You are not connected to any active printer. Please check  
that" && \
              "your printer is turned on and connected properly." into  
tPrompt
       answer tPrompt as sheet
       exit to top
    else
       put "You appear to have changed printers. Please select" && \
              "the printer you are now using." into tPrompt
       answer tPrompt as sheet
       set the systemprintselector to true
       answer printer
    end if
end uChoosePrinter

function getDefaultPrinter
    put word 4 of shell("lpstat -d") into tDefaultPrinter
    --   shell returns:  system default destination: HP_DESKJET_845C
    replace "_" with space in tDefaultPrinter
    return tDefaultPrinter
end getDefaultPrinter

function getActivePrinter
    put shell("ioreg") into tList
    filter tList with "*IOUSBDevice*"
    --  result:
    --  | |   |   +-o DeskJet 845C at 1d100000  <class IOUSBDevice, \
    --          registered, matched, active, busy 0, retain 8>
    --  | |   |   +-o IR Receiver at 5d100000  <class IOUSBDevice, \
    --          registered, matched, active, busy 0, retain 8>
    --  | |   |   +-o Apple Internal Keyboard / Trackpad at 5d200000  \
    --          <class IOUSBDevice, registered, matched, active, busy  
0, retain 10>
    --  | |   |   +-o IOUSBWirelessControllerDevice at 1a100000  <class \
    --          IOUSBDevice, registered, matched, active, busy 0,  
retain 10>
    --  | |   |   +-o wireless laser notebook mouse at 1a200000  <class \
    --          IOUSBDevice, registered, matched, active, busy 0,  
retain 8>
    --  | |   |   +-o Built-in iSight at fd400000  <class IOUSBDevice, \
    --          registered, matched, active, busy 0, retain 9>
    filter tList without "*Keyboard*"
    filter tList without "* IR *"
    filter tList without "*Wireless*"
    filter tList without "*mouse*"
    filter tList without "*iSight*"
    --  do we need more filters to exclude other devices?
    repeat for each line d in tList
       put offset("+-o",d)+4 into startChar
       put offset("@",d)-1 into endChar
       put (char startChar to endChar of d) & cr after newList
    end repeat
    delete char -1 of newList
    --  tList should actually be just one line at this point
    --  DeskJet 845
    if the number of lines of newList > 1 then
       --  catch unfiltered devices for refining
       answer "Error in filtering USB devices:" & cr & newList as sheet
       exit to top
    else
       return newList
    end if
end getActivePrinter

-----------


On Nov 20, 2009, at 6:40 AM, JosepM wrote:

>
> I'm very glad that this will be usefully for you. As I say, the only
> problem is convert the printer name to use "_". I preparing my own
> stack printer options to print using the shell commands.
>
> Here you can found more info with more deep.
>
> http://www.cups.org/doc-1.1/sum.html
>
> When I have something visible I send you for if can be usefully.
>
>
> Salut,
> Josep
>
> El 20/11/2009, a las 6:14, Phil Davis-5 [via Runtime Revolution]
> escribió:
>
>> This is the best so far!
>>
>> Phil Davis
>>
>>
>>
>> JosepM wrote:
>>
>>> Hi,
>>>
>>> Also you can use from the shell:
>>>
>>> lpstat -p --> to see the available printers
>>> lpstat -d --> to know the default printer name
>>>
>>> and to send directly to the printer:
>>>
>>> lpr -P <name of the printer> -o page-ranges=1 -o landscape <path
>> to the file
>>> to print>
>>>
>>> If you check the lpr command in CUPS manual you can see a lot of
>> options to
>>> control the job sended to the printer. The question is capture the
>> name of
>>> the printer or class.
>>> The name use "_" for spaces, assigning the name directly don't
>> work, almost
>>> for me.
>>>
>>>
>>> Salut,
>>> Josep
>>>
>>
>> -- 
>> Phil Davis
>>
>> PDS Labs
>> Professional Software Development
>> http://pdslabs.net
>>
>> _______________________________________________
>> use-revolution mailing list
>> [hidden email]
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-revolution
>>
>>
>> View message @ http://n4.nabble.com/knowing-if-a-printer-is-connected-tp624188p624409.html
>> To unsubscribe from Re: knowing if a printer is connected, click  
>> here.
>>
>
>
> -- 
> View this message in context: http://n4.nabble.com/knowing-if-a-printer-is-connected-tp624188p624552.html
> Sent from the Revolution - User mailing list archive at Nabble.com.
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your  
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution




More information about the use-livecode mailing list