I’ve found it annoying to write a script to interact with other Macs and have it fail because the Mac isn’t on or for whatever reason can’t be connected to. The task goes undone and I don’t know about until I hear about a problem later. So I wrote a script today that finds the Macs connected to the local network and get their host names. Then the script can check to see if the Mac is ready before before starting the script and the script can report back to me right away if the Mac isn’t responding. This can differentiate between the Mac not being on the network and the script timing out. Which usually required different actions. I still have yet to do the part that sends me a text message to my Verizon phone. But I figured this was close enough.
If anyone does care about that verizon has an email address for all its phones and if a mail message is set to it, the message is forwarded to the phone. For verizon the format is AAANNNNNN@vtext.com where AAA is the area code and NNNNNN is the rest of the number. I don’t know the format for other cell phone providers. I’ve never cared for any of the other solutions I’ve found.
If anyone is interested here is the script. I put a bunch of comments in the script:
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
-- This script finds the other Macs that can be seen over the network from your current Mac.
-- This script finds the other Bonjour service types of the specified type that can been seen from the current Mac.
-- There is a long list of these services on https://developer.apple.com/library/content/qa/qa1312/_index.html
-- It's simpler to look at the list then me explaning Bonjour beyond that it allows services on the network to be discovered dynamicly
-- If you want to have you Mac in the the list then remove the part where it says (NetworkServiceName ≠ CurrentHostName) and
property OtherHostsList : {}
property CurrentHostName : ""
property DoneGettingServices : false
on netServiceBrowser:NotUsed1 didFindService:NetworkService moreComing:StillServicesLeft
set DoneGettingServices to not StillServicesLeft
set NetworkServiceName to (NetworkService's |name|()) as text
-- if (NetworkServiceName ≠ my ServiceName) and (NetworkServiceName is not already in OtherHostsList list)
if (NetworkServiceName ≠ CurrentHostName) and (NetworkServiceName is not in OtherHostsList) then
set the end of OtherHostsList to NetworkServiceName
end if
end netServiceBrowser:didFindService:moreComing:
set DoneGettingServices to false
set OtherHostsList to {}
-- This is the host that goes with my Mac
set CurrentHostName to (current application's NSHost's currentHost()'s localizedName()) as string
set TheServiceBrowser to current application's NSNetServiceBrowser's alloc()'s init()
TheServiceBrowser's setDelegate:me
-- _afpovertcp._tcp indicates searching for AppleTalk Filing Protocol (AFP) which is used by Personal File Sharing
-- This is opposed to _nfs._tcp which which searches Network File System (NFS) servers
-- To do the same thing, except search for servers use "_nfs._tcp" instead of "_afpovertcp._tcp"
-- This line will automtically netServiceBrowser:didFindService:moreComing: at the beginning of the script
TheServiceBrowser's searchForServicesOfType:"_afpovertcp._tcp" inDomain:""
repeat while not DoneGettingServices
-- Need this delay or it will not work
delay 1.0E-5
end repeat
-- Reset the delegate to unused
TheServiceBrowser's setDelegate:(missing value)
OtherHostsList