knowing if a printer is connected

Peter Brigham MD pmbrig at gmail.com
Tue Nov 24 18:41:05 EST 2009


Here is the debugged script. It turned out to be more complicated than  
I thought, as usual. (Murphy was a programmer, right?)

Call "checkPrinter" before any printing commands. The first time you  
plug in a never-encountered printer and try to print something you  
will be asked to identify the printer by selecting it in an "answer  
printer" dialog; thereafter that printer will be used for printing  
automatically whenever it is connected and turned on.

Note that the currently chosen default printer (in the system  
preferences) will *not* be changed, just the Rev global property "the  
printerName", which contains the name of the printer the Rev engine  
will use for printing from any RevTalk commands.

This is Mac OSX only at this point. Someone with WIndows expertise  
could adapt this as needed -- might be trivial, might be complex. I  
don't know, I don't do Windows.

-- Peter

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

--------

on checkPrinter
    put getActivePrinter() into tLivePrinterInIOR
    --##  listing of connected printer(s) in IO registry
    if tLivePrinterInIOR = 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 if the number of lines of tLivePrinterInIOR > 1 then
       --##  more than one connected device might be a printer
       --##  but we might know this printer already
       repeat for each line p in tLivePrinterInIOR
          put the IORtoPrinterName[p] of stack "myLibrary" into  
tPossiblePrinterName
          --##  "myLibrary" or whatever stack you use to store the  
customprop
          --##  customprop IORtoPrinterName[p] gives name of the  
(possible) printer
          --##  in system prefs, stored when printer first encountered
          --##  will return empty if p is not a known printer
           if tPossiblePrinterName <> empty then
             --## found a known printer, set the printername, then done
             set the printername to tPossiblePrinterName
             exit checkPrinter
          end if
       end repeat
       --##  no entries are a known printer,
       --##  ask user to sort it out
       --##  have to construct answer dialog
       --##  with buttons for each device
       put q("cancel") into btnList
       repeat for each line p in tLivePrinterInIOR
          put q(truncate(p,18)) into btnName
          --##  so button names are not too long
          put q(truncate(p,50)) into promptLine
          put " and " & btnName after btnList
          put " & cr & " & promptLine after promptList
       end repeat
       put "More than one device might be a connected printer. Please  
select the" \
              && "printer from the following:" & quote & promptList  
into theDo
       put "answer" && quote before theDo
       put " with" && btnList && "as sheet" after theDo
       do theDo
       put it into whichDevice
       if whichDevice = "cancel" then exit to top
       if char -1 of whichDevice = "…Â
" then delete char -1 of whichDevice
       --##  ellipsis character (option-;) from the truncate() function
       put line lineoffset(whichDevice,tLivePrinterInIOR) of  
tLivePrinterInIOR into \
             tLivePrinterInIOR
    end if
    --##  now tLivePrinterInIOR contains one entry
    put the IORtoPrinterName[tLivePrinterInIOR] of stack "myLibrary" \
              into tLivePrinterName
    --##  "myLibrary" or whatever stack you used to store the customprop
    --##  IORtoPrinterName[tLivePrinterInIOR] gives name of this printer
    --##  in system prefs, stored when printer first encountered
    --##  it will return empty if not a known printer
    put the printername into tCurrentRevPrinterName
    --##  printer currently designated as the printer for Rev to use
    if tCurrentRevPrinterName = tLivePrinterName and  
tCurrentRevPrinterName \
             <> empty then
       exit checkPrinter
       --##  currently connected to the chosen printer,
       --##  do nothing further
    end if
    --##  else:
    if tLivePrinterName = empty then
       --##  never seen this printer
       put "Please choose the current printer (" & tLivePrinterInIOR & \
                 ") so it can be identified in the future." into tPrompt
       answer tPrompt as sheet
       set the systemprintselector to true
       answer printer
       --##  sets the printername (used by Rev for printing)
       if the result = "cancel" then exit to top
       put the printername into tPrinterName
       set the IORtoPrinterName[tLivePrinterInIOR] of stack  
"myLibrary" to tPrinterName
       --##  "myLibrary" or whatever stack you use to store the  
customprop
       else
       --##  a known printer is currently live, just start using it
       set the printername to tLivePrinterName
    end if
end checkPrinter

function getActivePrinter
    put shell("ioreg") into tList
    filter tList with "*<class IOUSBDevice*"
    filter tList without "*UserClient*"
    filter tList without "*Keyboard*"
    filter tList without "* IR *"
    filter tList without "*Wireless*"
    filter tList without "*mouse*"
    filter tList without "*iSight*"
    --##  more filtering needed for other devices?
    --##  ... to avoid too many instances of multiple
    --##  possible printers
    filter tList without empty
    repeat for each line d in tList
       --##  could be more than one
       put offset("+-o",d)+4 into startChar
       put offset("@",d)-1 into endChar
       put (char startChar to endChar of d) & cr after printerList
    end repeat
    delete char -1 of printerList
    return printerList
end getActivePrinter

function q str
    return quote & str & quote
end q

function truncate str,tLength
    if length(str) <= tLength then return str
    return char 1 to tLength -1 of str & "…Â
" --##  ellipsis character (option-;)
end truncate

--------

On Nov 23, 2009, at 4:40 PM, Phil Davis wrote:

> Hi Peter,
>
> If you filter out all lines from "ioreg" output except ones that  
> contain "IOUSBCompositeDevice", do you see the printer name in any  
> line? If so, maybe available printer names could be matched to the  
> device names in "IOUSBDevice" and "IOUSBCompositeDevice" records.
>
> Phil
>
>
>
> Peter Brigham MD wrote:
>> This worked for me on my home printer, and I had high hopes for it,  
>> but if fails this morning here at work. The problem seems to be  
>> that with some printers the listing from the ioreg call has little  
>> relation to the name of the printer. For instance, my Brother  
>> laserjet MFC 8220 (combo printer fax copier) has an ioreg listing of:
>>
>> +-o IOUSBCompositeDevice at fd100000  <class IOUSBDevice, registered,  
>> matched, active, busy 0, retain 12>
>>
>> and there is no way of telling that this is a Brother 8220 or  
>> relating it to the availablePrinters, not on the face of it, anyway.
>>
>> I'm working on a solution that will involve storing the ioreg name  
>> of a given printer as a customprop "ioregListing[printerName]" More  
>> to come. I'm determined to make this work as invisibly as possible.  
>> It has long been a irritation for me that the system doesn't  
>> automatically just send print jobs to the available printer.  
>> There's no reason on earth why you should have to change printers  
>> manually (after the first time you use one, of course). The system  
>> knows what's plugged in, for goodness sake. [grrrr...]
>>
>> -- Peter
>>
>> Peter M. Brigham
>> pmbrig at gmail.com
>> http://home.comcast.net/~pmbrig
>>
>>
>> On Nov 21, 2009, at 11:36 PM, Phil Davis wrote:
>>
>>> Everyone is doing it, so... here is what I came up with. Watch  
>>> line wraps please. Hopefully the comments explain what the code is  
>>> doing.
>>>
>>>
>>> on mouseUp
>>> answer UsbPrinterList()
>>> end mouseUp
>>>
>>>
>>> function UsbPrinterList
>>> -- set item delimiter
>>> set the itemDelimiter to tab
>>>  -- make a list of all active USB I/O devices
>>> put shell("ioreg") into tActiveDeviceList
>>> filter tActiveDeviceList with "*IOUSBDevice*" -- remove all but  
>>> USB devices from list
>>> replace "@" with tab in tActiveDeviceList -- isolate device name
>>> replace "+-o" with tab in tActiveDeviceList -- device name is item  
>>> 2 of each line
>>>  -- get all known printer names, whether active or not
>>> put the availablePrinters into tPrinterNames
>>>  -- identify active USB printers in the USB device list
>>> put empty into tUsbPrinters
>>> repeat for each line tDeviceLine in tActiveDeviceList
>>>    put word 1 to -1 of item 2 of tDeviceLine into tDeviceName --  
>>> could be partial device name
>>>    get tPrinterNames
>>>    filter it with ("*" & tDeviceName & "*")
>>>    if it = empty then next repeat -- device is not a printer
>>>        -- USB device is a printer, so get full name
>>>    repeat for each line tFoundPrinter in it
>>>       put tFoundPrinter & cr after tUsbPrinters
>>>    end repeat
>>> end repeat
>>> delete last char of tUsbPrinters -- trailing CR
>>>  return tUsbPrinters
>>> end UsbPrinterList
>>>
>>>
>>> Thanks -
>>> Phil Davis
>>>
>>>
>>>
>>> JosepM wrote:
>>>> Hi,
>>>>
>>>> In English work, but in Spanish and others languages don't.
>>>>
>>>> The result of the shell command is:
>>>> destino por omision del sistema: HP_Photosmart_C4200_series
>>>>
>>>> So we must check for the ":" and get the printer name.
>>>>
>>>>  set itemdel to ":"
>>>>  put item 2 of shell("lpstat -d") into tDefaultPrinter
>>>>
>>>>
>>>> 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
>>>>
>>>> The getActivePrinter get all the USB devices. In my case, my  
>>>> LaCie disk, the
>>>> iPhone, the DataTraveler and the HP printer.. How to filter  
>>>> between them?
>>>>
>>>> Salut,
>>>> Josep
>>>>
>>>
>>> -- 
>>> Phil Davis
>>>
>>> PDS Labs
>>> Professional Software Development
>>> http://pdslabs.net
>>>
>>> _______________________________________________
>>> 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
>>
>> _______________________________________________
>> 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
>>
>
> -- 
> Phil Davis
>
> PDS Labs
> Professional Software Development
> http://pdslabs.net
>
> _______________________________________________
> 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