Mac migration 01 – list all mapped printers on the system

This will be the first in a series of articles I’ll be writing on making the filesystem tell you (the Mac admin) useful things when looking at, say, a complete workstation refresh in which all of a users’ data, fonts, departmental apps, printers, etc., need to be moved from their old mac to their new, with as few hiccups as possible. I’ve worked on streamlining this procedure for two years now, and have it pretty down pat except the process, I feel is still too manual, and I am working on a way to completely automate  it.

This is particularly useful during the company annual Mac refresh projects, where about 25% of the install base is upgraded to new hardware, some of which resides halfway around the world. Being able to literally click a button and walk away to do something else for a few hours is music to my earholes. So without further adieu , here is how to list every printer mapped on the mac, in both Applescript and bash.

APPLESCRIPT

tell application "Printer Setup Utility"
      set printerList to the name of every printer as list
end tell
repeat with aPrinter in printerList
      write aPrinter & "
" to logFile -- since this is being used in a migration report, we write the printer name out to a file
end repeat

Let’s break the Applescript down. We’re using a tell block to tell the application “Printer Setup Utility” to list every printer mapped on the Mac. Since PSU knows what the printer names are but Applescript doesn’t, we need to specifically tell PSU to do this. Hence the tell block. Setting all the names as a list ensures that we can loop through the printer names as if they were an array or text file. A caveat here: IME, invoking an Applescript from an external source such as Casper Remote can cause -10810 errors – “too many processes running.” This is especially true if the Applescript uses tell blocks, or “do shell script” commands frequently. This might not be a problem for simple scripts like this one, but for more complex scripts that require dozens of tell blocks and many do shell scripts, it can get pretty.

BASH

lpstat -a | awk '{print $1}' >> $LogFile.txt

Let’s break the bash down. the lpstat command is built in to OS X to list cups printing information. the -a switch lists all mapped printers, and the pipe to “awk ‘{print $1}'” ensures that only the printer name is captured, not any other the other information the command spits out. One caveat here: Unix does not like spaces in printer names, and will replace a space with an underscore ‘_’, so keep that in mind when using tools like Casper Remote to reinstall printers (the queue names in Remote will not match the names shown with lpstat).


About this entry