Get IP address of a networked printer?

I downloaded the latest version, but got a syntax error message when I tried to run this script:

“A forURL theURL can’t go after this host.”

I’m completely baffled by this!

Have you deleted the old version and relaunched AppleScript Editor to update the dictionary?

I deleted the old version but neglected to close down and restart the AppleScript Editor. Thank you for correcting a beginner’s mistake!

Thanks to StefanK, who did all the hard work, I have finally written a script that should return the IP address of a networked printer. Here is the code, which is surely sloppy and inefficient but seems to get the job done. Many thanks again to StefanK and to everyone else at MacScripter.net whose work I have used in this script.

-- Get the IP address of a networked IP printer
-- by Edward Mendelson, using code adapted from many posts at MacScripter.net
-- requires "Bonjour Events" by Stefan Klieme, downloadable here:
-- http://www.klieme.ch/pub/Bonjour%20Events.zip

set noPrinters to 0
try
	set printerNames to (do shell script "lpstat  -l -p | grep -i Description: |awk -F'Description: ' '{print $2}' ") -- as list
on error
	set noPrinters to 1
end try
if printerNames = "" then
	set noPrinters to 1
end if
if noPrinters is 0 then
	try
		set queueNames to (do shell script "lpstat -a | awk -F' accepting' '{print $1}'") -- as list
	on error
		set noPrinters to 1
	end try
end if

if noPrinters = 0 then
	try
		set printerList to (every paragraph of printerNames) as list
		set queueList to (every paragraph of queueNames) as list
	end try
	tell me to activate
	set thePrinter to (choose from list printerList with title "Printers" with prompt "Choose a printer:")
	if thePrinter is false then
		tell me to activate
		display dialog "Printer setup cancelled."
		error number -128
	else
		set thePrinter to item 1 of thePrinter
		set item_num to my list_position(thePrinter, printerList)
		set theQueue to item item_num in queueList
	end if
	
	set dnsURL to do shell script "lpstat -v " & theQueue
	set dnsString to (do shell script "perl -e 'use URI::Escape; print uri_unescape(\"" & dnsURL & "\")';")
	
	try
		tell application "Bonjour Events"
			scan type "_ipp._tcp" in domain "local"
			repeat until browser finished
				delay 0.5
			end repeat
			
			if (count services) > 0 then
				set nameList to name of services
				set addressList to IPv4 address of services
			else
				set nameList to {}
				set addressList to {}
			end if
			quit
			-- Bonjour Events quits automatically after 2 minutes of inactivity
		end tell
	on error errorMessage number errorNumber
		display dialog "An error occurred: " & errorMessage & " (" & errorNumber & ")"
	end try
	
	set ipPrinterFound to false
	repeat with currentItem in nameList
		if currentItem is in dnsString then
			set ipPrinterFound to true
			set currentListItem to currentItem as item
			set dnsNumber to my list_position(currentListItem, nameList)
			set printerIP to item dnsNumber in addressList
			display dialog currentItem & return & return & "has the IP address:" & space & printerIP
			exit repeat
		end if
	end repeat
	
	if ipPrinterFound is false then
		display dialog thePrinter & " does not seem to support IP printing."
		error number -128
	end if
	
else
	tell me to activate
	display dialog "No printers found!"
	error number -128
	
end if

on list_position(this_item, this_list)
	repeat with i from 1 to the count of this_list
		if item i of this_list is this_item then return i
	end repeat
	return 0
end list_position

It is just a pity that Stefan’s utility doesn’t work with Snow Leopard :frowning:

I set the deployment target to 10.5. Now it should work down to Leopard (Intel)

Thanks Stefan! :slight_smile: