Tuesday, January 23, 2018
  • Index
  •  » Code Exchange
  •  » Bonjour Events, a scriptable application to scan Bonjour Services

#1 2012-10-13 03:36:11 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Bonjour Events, a scriptable application to scan Bonjour Services

In this thread I posted a small faceless scriptable app to scan Bonjour services for a given type and domain and have access to the (DNS resolved) properties.

Meanwhile I made a few improvements:

The scan command waits now for finishing the scanning process, a repeat loop isn't needed any more.
To control the timing you can add an optional parameter time out after .

The result of the scan command is now a record:

scan error : missing value or the error reason (text)
timed out : like gave up in display dialog (boolean)
number of services : the number of detected services (integer)

If number of services is not zero, services contains instances of a Bonjour service
including the properties domain, host, IPv4 address, IPv6 address, name, port, and service type

As a possible error is passed in the result record, a try block could be omitted too.

Sample code:

Applescript:


tell application "Bonjour Events"
   scan for type "_afpovertcp._tcp" in domain "local" time out after 5.0
   set {scan error:scanError, timed out:timedOut, number of services:numberOfServices} to result
   if scanError is not missing value then
       -- do some error handling
   else if timedOut then
       -- handle time out
   else if numberOfServices > 0 then
       tell service 1
           set ipv4Address to IPv4 address
           set theHost to host
       end tell
   end if
   -- Bonjour Events quits automatically after 2 minutes of inactivity
end tell

Requirements: Intel Mac - 10.5 and higher

Download: Bonjour Events (The app is code signed)

Last edited by StefanK (2012-10-27 06:20:42 am)


regards

Stefan

Offline

 

#2 2012-10-18 02:46:58 pm

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Bonjour Events, a scriptable application to scan Bonjour Services

Hi. I'm on 10.8. Downloaded and started Bonjour Events. Opened the applescript and it doesn't compile. It highlights the word "type"  in "scan type" with the error "Syntax error: Expected end of line but found identifier".

EDIT: never mind. I checked activity monitor to see if Bonjour Events was running... and it wasn't. I then tried the applescript again and now it compiles. Not sure what happened but it's working.

Last edited by regulus6633 (2012-10-18 02:59:02 pm)

Offline

 

#3 2012-10-18 03:03:18 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Bonjour Events, a scriptable application to scan Bonjour Services

I guess a new scriptable app must be launched once to be registered to the AppleScript environment


regards

Stefan

Offline

 

#4 2012-10-18 03:44:46 pm

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Bonjour Events, a scriptable application to scan Bonjour Services

StefanK wrote:

I guess a new scriptable app must be launched once to be registered to the AppleScript environment


No, I think I figured it out. Somehow the word type is causing the problem. It seems there are 2 types in your applescript dictionary, the scan type and in the service record there is a type property. Somehow they're getting mixed up. Here's why I say this. Here's a portion of my script...

Applescript:

tell application "Bonjour Events"
   scan type "_afpovertcp._tcp" in domain "local" time out after 5.0
   set {scan error:scanError, timed out:timedOut, number of services:numberOfServices} to result
   set theServices to {}
   repeat with aService in services
       tell aService
           set a to IPv4 address
           set h to host
           set p to port
           set t to type of aService
           set end of theServices to {h, a, p, t}
       end tell
   end repeat
   return theServices
end tell

Notice the line "set t ...". I need to specify "of aService" otherwise I do not get a proper returned value from the record. Since that's the same word that the script had trouble compiling on then I think it has something to do with "type" having multiple meanings to your app.

Offline

 

#5 2012-10-18 04:03:01 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Bonjour Events, a scriptable application to scan Bonjour Services

thanks Hank, I'm using the original property names from the documentation,
I'll look for a solution to avoid that terminology clash


regards

Stefan

Offline

 

#6 2012-10-18 04:20:58 pm

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Bonjour Events, a scriptable application to scan Bonjour Services

No problem and thanks for the tool.

One more question. Is there a way to scan for any service type rather than specifying one? Maybe it's an easy update? In general I'd like to know any available service on my network and would prefer not to loop through all of them... I found a complete list here.

Offline

 

#7 2012-10-19 09:20:41 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Bonjour Events, a scriptable application to scan Bonjour Services

Of course it's possible (see Bonjour Browser), but it's quite expensive,
because you have to instantiate one browser instance for each type, there is no "searchAll" or "searchInList" method.

Resolving the terminology clash I changed the command
scan type to scan for type
and the property of service
type to service type

the keyword type is internally used in AppleScript, probably that caused the problem

Version 1.3, same link

Last edited by StefanK (2012-10-19 09:42:46 am)


regards

Stefan

Offline

 

#8 2012-10-19 04:22:28 pm

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Bonjour Events, a scriptable application to scan Bonjour Services

Your 2 changes worked. Your suggestion about the possible problem with "type" makes sense. Thanks for the upgrades. You've got a nice tool here.

Offline

 

#9 2012-10-26 04:33:09 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Bonjour Events, a scriptable application to scan Bonjour Services

Finally I discovered how to implement an callback event handler (the Cocoa Scripting documentation is very misty regarding this issue), so you can use Bonjour Events asynchronously.

This option can be enabled by using the (optional) parameter target passing the reference to a script (usually me) and implementing the did detect services event handler

Applescript:


using terms from application "Bonjour Events"
   on did detect services serviceList for type forType with result replyResult
       
       set {scan error:scanError, timed out:timedOut, number of services:numberOfServices} to replyResult
       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 "Number of Services: " & numberOfServices
           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 detect services
end using terms from


tell application "Bonjour Events"
   scan for type "_afpovertcp._tcp" in domain "local" time out after 5.0 target me
end tell

Version 1.4, same link as in the first posting


regards

Stefan

Offline

 

#10 2014-12-23 08:24:02 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Bonjour Events, a scriptable application to scan Bonjour Services

[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:

Applescript:


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

Applescript:

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:

Applescript:


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

Applescript:

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

Applescript:


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

Last edited by StefanK (2014-12-23 08:49:11 am)


regards

Stefan

Offline

 

#11 2014-12-24 12:33:14 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: Bonjour Events, a scriptable application to scan Bonjour Services

Update: for consistency I changed the event handler definitions to

Applescript:


on did appear services serviceList of browser aBrowser with result replyResult
on did disappear services hostNames of browser aBrowser

extended sample code of the asynchronous version

Applescript:


using terms from application "Bonjour Events"
   on did appear services serviceList of browser aBrowser with result replyResult
       set {scan error:scanError, timed out:timedOut} to replyResult
       if scanError is not missing value then
           display dialog "do some error handling"
       else if timedOut then
           display dialog "event timed out"
       else if (count serviceList) > 0 then
           my notifyAppearedServices(aBrowser, serviceList)
       end if
   end did appear services
   
   
   on did disappear services hostNames of browser aBrowser
       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
end using terms from

set bonjourType to "_afpovertcp._tcp"
tell application "Bonjour Events"
   -- create new browser instance, the name property is mandatory
   if (exists browser "MyBrowser") then
       set AFPBrowser to browser "MyBrowser"
   else
       set AFPBrowser to make new browser with properties {name:"MyBrowser"}
   end if
   tell AFPBrowser
       if is scanning then
           set availableServices to services
           my notifyAppearedServices(it, availableServices)
       else
           scan for type bonjourType in domain "local" target me
       end if
   end tell
end tell

on notifyAppearedServices(theBrowser, theServices)
   tell application "Bonjour Events"
       display dialog ((count theServices) as text) & " services in browser " & name of theBrowser & " appeared"
       repeat with aService in theServices
           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 tell
end notifyAppearedServices

Download Version 2.1 (same link) : Bonjour Events 2.1


regards

Stefan

Offline

 
  • Index
  •  » Code Exchange
  •  » Bonjour Events, a scriptable application to scan Bonjour Services

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)