Posts RSS Comments RSS 48 Posts and 155 Comments till now

Delete printers using AppleScript

I recently had a request for a script that would delete printers from a lab build. The user wanted to make one system build for all of his labs with all of the printers installed. Then, just run a script to select the correct printer for that lab and delete the others. Here’s the script I came up with. With this script you can choose more then one printer if that is more appropriate.


--get the name of every printer
tell application "Printer Setup Utility"
set theList to the name of every printer as list
end tell

--present the list to choose the printer from
choose from list theList with title "Lab Printer" with prompt "Pick the correct printer for this lab" with multiple selections allowed
set theItem to the result as list

--loop through the list and delete any printer not in the list theItem
repeat with x from 1 to the count of theList
if theItem does not contain (item x of theList) then
do shell script "lpadmin -x " & (item x of theList)
end if
end repeat

4 Responses to “Delete printers using AppleScript”

  1. on 18 Jan 2011 at 2:36 pmPat Best

    Hi there, this looks like something I could definitely use. I am a novice scripter and when I try to run this command through ARD or from the apple script editor I get the error “lpadmin: The printer or class was not found.” number 1″. I am unsure of where to go with this. Any guidance would be great. I am using 10.6.4 and have attempted with bonjour, ip, and shared printer connections to get this to work. Thank you for the great little utility! Pat Best

  2. on 18 Jan 2011 at 2:57 pmwebmaster

    I haven’t looked at this script since 10.6 came out so it’s probably there are bugs in it now. I’ll try to revisit it soon.

    Also, this script isn’t meant to be sent through ARD as it requires user interaction, which you can’t really give through ARD unless you’re doing screen sharing.

    My suggestion for what you are doing is go to one of the machines that has this printer and enter “http://localhost:631” in a web browser to see the CUPS page. Get the queue name for that printer from the “Printers” tab. You could also send “lpstat -p” via the ARD “Send UNIX Command” to get the names. Then send “lpadmin -x the_queue-name” to your machines.

  3. on 19 Jan 2011 at 1:28 pmPat Best

    Thank you for such a quick response. I appreciate the tips and will run through your suggestions. Thanks!

  4. on 28 Nov 2012 at 12:40 amJosh

    My issue is that my printer naming scheme has a – in it. “The List” name is therefore Location-Printer; however, the actual CUPS queue name is Location_Printer. Is there a way to script the substitution of – for _ on the removal portion of this script?