knowing if a printer is connected

Phil Davis revdev at pdslabs.net
Sat Nov 21 23:36:00 EST 2009


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




More information about the use-livecode mailing list