[ANN] Version 2 of Bonjour Events
the main new feature is the ability to create multiple browsers.
The (arbitrary) name property has to be specified in the make new line, the other properties service type, domain and time out can be set in separate lines or in the scan command.
Default value for domain is an empty string (any domain). To search in the local network, pass “local”.
Default value for time out is 5 (seconds).
The service type must be a Bonjour type string like _afpovertcp._tcp.
The synchronous version returns a record:
scan error : missing value or the error reason (text)
timed out : like gave up in display dialog (boolean)
bonjour services : a list of the service instances or missing value if an error occurs
Sample code:
tell application "Bonjour Events"
-- create new browser instance, the name property is mandatory
set AFPBrowser to make new browser with properties {name:"MyBrowser"}
tell AFPBrowser
scan for type "_afpovertcp._tcp." in domain "local" time out after 10.0 -- default time out value is 5.0
end tell
(*
alternatively set the properties in separate lines
tell AFPBrowser
set domain to "local" -- default is "" (any domain)
set service type to "_afpovertcp._tcp"
scan
end tell
*)
set {scan error:scanError, timed out:timedOut, bonjour services:bonjourServices} to result
if scanError is not missing value then
-- do some error handling
else if timedOut then
-- handle time out
else if (count bonjourServices) > 0 then
set firstService to item 1 of bonjourServices
tell firstService
set ipv4Address to IPv4 address
set theHost to host
end tell
end if
-- Bonjour Events quits automatically after 5 minutes of inactivity
end tell
The asynchronous version provides two event handlers
on did appear services of browser aBrowser with result replyResult
on did disappear services of browser aBrowser hosts hostNames
The former returns the browser instance in the direct parameter aBrowser and the same record type as in the synchronous version in the replyResult parameter.
The latter returns the browser instance in the direct parameter aBrowser and a list of the disappeared host names (text) in the hostNames parameter.
To use the asynchronous version add the target parameter in the scan command with the value me.
Sample code:
using terms from application "Bonjour Events"
on did appear services of browser aBrowser with result replyResult
set {scan error:scanError, timed out:timedOut, bonjour services:serviceList} to replyResult
set numberOfServices to (count serviceList)
if scanError is not missing value then
display dialog "do some error handling"
else if timedOut then
display dialog "event timed out"
else if numberOfServices > 0 then
display dialog (numberOfServices as text) & " services in browser " & name of aBrowser & " appeared"
repeat with aService in serviceList
tell aService
set serviceName to its name
set ipv4Address to IPv4 address
set theHost to host
set theType to service type
end tell
display dialog "Name: " & serviceName & return & "Host: " & theHost & return ¬
& "IPV4 Address: " & ipv4Address & return & "Type: " & theType
end repeat
end if
end did appear services of browser
on did disappear services of browser aBrowser hosts hostNames
set numberOfHosts to (count hostNames)
if numberOfHosts > 0 then
display dialog (numberOfHosts as text) & " services in browser " & name of aBrowser & " disappeared"
repeat with aHost in hostNames
display dialog "Name: " & aHost
end repeat
end if
end did disappear services of browser
end using terms from
set bonjourType to "_afpovertcp._tcp"
tell application "Bonjour Events"
-- create new browser instance, the name property is mandatory
set AFPBrowser to make new browser with properties {name:"MyBrowser"}
tell AFPBrowser
scan for type bonjourType in domain "local" target me
end tell
end tell
Note : Bonjour Events quits automatically after 5 minutes of inactivity in the synchronous version.
In the asynchronous version the quit delay property is set to 0 (infinite) when a browser starts scanning.
You can stop a browser in one of the event handlers in code with
if name of aBrowser is "MyBrowser" then tell aBrowser to stop
At the moment all browsers are stopped the quit delay property is reset to 300 (5 minutes)
You could also stop a browser from another script file
tell application "Bonjour Events"
if exists browser "MyBrowser" then
tell browser "MyBrowser" to stop
end if
end tell
As long as Bonjour Events is running the created browser instances stay alive and send Apple Events until the application quits or the respective browser stops.
Download : Bonjour Events 2