A solution to play a dog barking sound when animal motion triggers an AppleScript action

We've been plagued by any number of critters and varmints (deer, bear, skunks, raccoons, cats, dogs, chickens) at our place that both create damage and leave a mess. This solution appears to be working, and you can see .gifs in a recent post I made to the Automation section of this forum. Basically, when an animal triggers the AI detection the system runs both a custom AppleScript that plays a random dog barking sound and also triggers a home automation device to turn on a very loud leaf blower for 10 seconds. The combination seems to chase off those critters, at least for now. In addition, the script auto switches audio to the bluetooth speaker.

Caveats

  1. I'm just an AppleScript hack, so the attached script may need massaging
  2. I'm not responsible for raucous fits of laugher when you watch the motion captures the next morning

Parts (may not be complete, just what I can remember)

  1. Mac mini headless (managed with nomachine) https://www.nomachine.com/
  2. SecuritySpy 8 cam (Dahua/Amcrest)
  3. HomeKit smart plugs (TP Link EP25) https://www.amazon.com/dp/B0B14C719T
  4. Anker SoundCore 2 bluetooth speaker https://www.amazon.com/dp/B01MTB55WH
  5. Ears Audio Switcher https://retina.studio/ears/
  6. SunJoe AC leaf blower https://www.amazon.com/dp/B076CS86NF
  7. Find/download dog barking sound effects (I use 8) https://pixabay.com/
  8. An audio editing app https://apps.apple.com/us/app/sound-studio/id405537804

Installation/Setup (I'm assuming you know your way around SS and some amount of tech/Mac knowledge, and the list may or may not be complete...just what I can remember)

  1. Pair an outside bluetooth speaker to your SS workstation
  2. Use Ears to identify the name of the bluetooth speaker name (you'll revise the script with that name) and also set the output back to your workstation default
  3. Create a directory within your SS instance 'Sounds' directory that is named 'DogBarkSounds'
  4. Use an audio app to convert the dog barking sound effects to aiff and copy them into the DogBarkSounds directory. You don't need to do this, but aiff is compatible with SS if you want to play sound in the Actions tab
  5. Open script editor, paste the script below into a new script and save to the Scripts folder of the SS Instance with a name something like RandomDogBark.scpt
  6. Set the Actions trigger for 'Animal' and adjust the sensitivity in Advanced Options
  7. Set the Actions 'run script' to the name of the script you saved (RandomDogBark)
  8. If you want the leaf blower racket then add the Smart Plug to Home, then add that device as an action to HomeHelper and then select that Home Action from the Actions tab. However, this isn't as granular as using AppleScript. I'll be updating my RandomDogBarking script to include triggering the smart plug from that script.
  9. Arm the camera per the schedule you want. In the future I'll also be updating the AppleScript to handle ToD execution so I can leave the SS camera action to 24/7. This is because I want certain actions to happen 24/7 and only the Animal Intruder Alert functions to happen from 6pm to 7am.
  10. If you want to test the script, comment out the 'args' on the on run statement and also modify the reasons variable from 'item3 of args' to 'Human', that way you can wander out and see if it triggers. Remember to undo those tweaks when you're done.

Good luck!


-- RandomDogBark.scpt

use AppleScript version "2.4" -- Yosemite (10.10) or later

use scripting additions



on process()

set progress description to "Play barking dog sound"

-- your code goes here

end process


on cleanup()

-- your cleanup code goes here

end cleanup



on run args

try

-- Process...

set progress total steps to -1 -- indeterminant progress

set progress completed steps to 0

set progress description to ""

set progress additional description to "Processing..."

process()

tell application "Ears"

set audio output "SoundCore 2"

end tell

set reasons to item 3 of args

if reasons contains "Animal" then

set homePath to POSIX path of (path to home folder)

set soundsPath to "SecuritySpy/Sounds/DogBarkSounds/"

set dogBarkSounds to homePath & soundsPath

set randomDogBark to some item of paragraphs of (do shell script "find " & ¬

quoted form of dogBarkSounds & ¬

" -iname \"*.aiff\"")

tell application "System Events"

if exists file randomDogBark then

do shell script "afplay " & randomDogBark

end if

end tell

tell application "Ears"

set audio output "Mac mini Speakers"

end tell

end if

-- CLeanup...

set progress total steps to -1 -- indeterminant progress

set progress completed steps to 0

set progress additional description to "Cleaning up..."

cleanup()

on error errMsg number errNum from errFrom to errTo partial result errPartialResult

-- CLeanup...

set progress total steps to -1

set progress completed steps to 0

set progress additional description to "Cleaning up..."

cleanup()

error errMsg number errNum from errFrom to errTo partial result errPartialResult -- resignal the error

end try

end run

Comments

  • If anyone is interested, I updated the AppleScript to perform different actions based upon location. For instance, when someone is at the entryway a random greeting is played. If there are animals in another location it will concurrently play dog barking sounds and cycle a HomeKit smart plug to activate a loud leaf blower, etc.