AppleScript for checking and acting on ring events

Hi all,

I currently have the below AppleScript, which works just fine, but wondering if there’s a more efficient way of writing it. I originally had it running without the repeat loop, but the script was too slow at checking for text in the window, and would miss when someone rang the doorbell.


on idle
	tell application "Ring"
		activate
		tell application "System Events" to tell process "Ring"
			repeat
				if name of front window contains "ding" & "apartment" then
					tell application "System Events"
						tell process "Ring"
							click button "Talk" of window 1
							set theFile to ((path to home folder as text) & "Greeting.aiff")
							delay 1
							tell application "VLC"
								open theFile
							end tell
							tell application "System Events"
								tell process "SystemUIServer"
									click (menu bar item 1 of menu bar 1 whose description contains "Displays")
									click menu item "Living Room" of menu 1 of result
								end tell
							end tell
							delay 2
							say "Excuse me. Someone is at your front door."
							delay 20
							tell application "System Events"
								tell process "Ring"
									click button "End" of window 1
									tell application "System Events"
										tell process "SystemUIServer"
											click (menu bar item 1 of menu bar 1 whose description contains "Displays")
											click menu item "Turn Airplay Off" of menu 1 of result
										end tell
									end tell
								end tell
							end tell
						end tell
					end tell
				else
					if name of front window contains "ding" then
						tell application "System Events"
							tell process "Ring"
								click button "Talk" of window 1
								set theFile to ((path to home folder as text) & "Greeting.aiff")
								delay 1
								tell application "VLC"
									open theFile
								end tell
								delay 20
								tell application "System Events"
									tell process "Ring"
										click button "End" of window 1
									end tell
								end tell
							end tell
						end tell
					end if
				end if
			end repeat
		end tell
	end tell
end idle

Thanks so much,

Rocco

Hi.

The apparent slowness was probably because your idle handler doesn’t return an integer at the end to set the interval before the next call, so the interval defaults to 30 seconds.

I’ve tidied up the script a bit to get rid of the unnecessarily nested tells and to compress the logic, but I don’t have either Ring or VLC (or indeed a front doorbell connected to my computer!), so I haven’t been able to test what I’ve done. The only real effiency improvement is that the idle interval is now explicitly set to 1 second.

global theFile

on run
	tell application "Ring" to activate
	set theFile to ((path to home folder as text) & "Greeting.aiff") as alias
end run

on idle
	tell application "System Events" to set RingWindowName to name of front window of process "Ring"
	set ding to (RingWindowName contains "ding")
	if (ding) then
		tell application "System Events"
			tell process "Ring" to click button "Talk" of window 1
		end tell
		delay 1
		tell application "VLC" to open theFile
		set dingapartment to (RingWindowName contains "ding" & "apartment") -- The name contains "dingapartment"?
		if (dingapartment) then
			tell application "System Events"
				tell process "SystemUIServer"
					set DisplaysMenuBarItem to (menu bar item 1 of menu bar 1 whose description contains "Displays")
					click DisplaysMenuBarItem
					click menu item "Living Room" of menu 1 of DisplaysMenuBarItem
				end tell
			end tell
			delay 2
			say "Excuse me. Someone is at your front door."
		end if
		delay 20
		tell application "System Events"
			tell process "Ring" to click button "End" of window 1
			if (dingapartment) then
				tell process "SystemUIServer"
					click DisplaysMenuBarItem
					click menu item "Turn Airplay Off" of menu 1 of DisplaysMenuBarItem
				end tell
			end if
		end tell
	end if
	return 1 -- Call this handler again in one second's time.
end idle

Thank you so much for this. It looks awesome, but haven’t gotten it to work yet. I tried to fix the “ding apartment” thing, but I think it’s not seeing the window name correctly. Here’s my script now:


global theFile

on run
	tell application "Ring" to activate
	set theFile to ((path to home folder as text) & "Greeting.aiff") as alias
end run

on idle
	tell application "System Events" to set RingWindowName to name of front window of process "Ring"
	set home to (RingWindowName contains "ding from front")
	if (home) then
		say "home doorbell activated"
		tell application "System Events"
			tell process "Ring" to click button "Talk" of window 1
		end tell
		delay 3
		tell application "VLC" to open theFile
		set dingapartment to (RingWindowName contains "ding from apartment")
		if (dingapartment) then
			say "apartment doorbell activated"
			tell application "System Events"
				tell process "SystemUIServer"
					set DisplaysMenuBarItem to (menu bar item 1 of menu bar 1 whose description contains "Displays")
					click DisplaysMenuBarItem
					click menu item "Living Room" of menu 1 of DisplaysMenuBarItem
				end tell
			end tell
			delay 2
			say "Excuse me. Someone is at your front door."
		end if
		delay 20
		tell application "System Events"
			tell process "Ring" to click button "End" of window 1
			if (dingapartment) then
				tell process "SystemUIServer"
					click DisplaysMenuBarItem
					click menu item "Turn Airplay Off" of menu 1 of DisplaysMenuBarItem
				end tell
			end if
		end tell
	end if
	return 1 -- Call this handler again in one second's time.
	log file ((path to home folder as text) & "Greeting.log") as alias
end idle


As I don’t own the app Ring, here is just a guess.

tell application "VLC" to open theFile
say RingWindowName # ADDED, Last time you defined it, it contained "ding from front"
tell application "System Events" to set RingWindowName to name of front window of process "Ring" # ADDED
say RingWindowName # ADDED to hear if it changed and contains "ding from apartment"
set dingapartment to (RingWindowName contains "ding from apartment")

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mercredi 15 février 2017 18:02:37

My compression of the logic was based on the understanding from the original script that the relevant window names all contain “ding” and that some of these contain the longer string “dingapartment” and some don’t. The way you’ve rewritten it, the relevant names all contain “ding from front” and some of these may also contain “ding from apartment”, which is probably not the case. Try changing “ding from front” back to “ding”, or possibly “ding from”. This will pass names containing either “ding from front” or “ding from apartment” and allow the actions common to both to be performed. The “ding from apartment” test later on will differentiate between the two where relevant.

The ‘log’ instruction you’ve inserted at the bottom won’t be executed because it comes after the return line. It wouldn’t tell you anything anyway as it’s for displaying messages in your script editor, whereas a script with an ‘idle’ handler will be running as a stay-open application in its own right.

ah, that was it, thank you!! Works beautifully now. Here’s the final code in case someone has a ring doorbell and wants to use this to trigger an automatic greeting when someone rings the bell:


global theFile

on run
	tell application "Ring" to activate
	set theFile to ((path to home folder as text) & "Greeting.aiff") as alias
end run

on idle
	tell application "System Events" to set RingWindowName to name of front window of process "Ring"
	set home to (RingWindowName contains "Ding from")
	if (home) then
		tell application "System Events"
			tell process "Ring" to click button "Talk" of window 1
		end tell
		delay 3
		tell application "VLC" to open theFile
		set dingapartment to (RingWindowName contains "Apartment")
		if (dingapartment) then
			tell application "System Events"
				tell process "SystemUIServer"
					set DisplaysMenuBarItem to (menu bar item 1 of menu bar 1 whose description contains "Displays")
					click DisplaysMenuBarItem
					click menu item "Living Room" of menu 1 of DisplaysMenuBarItem
				end tell
			end tell
			delay 2
			say "Excuse me. Someone is at your front door."
		end if
		delay 20
		tell application "System Events"
			tell process "Ring" to click button "End" of window 1
			if (dingapartment) then
				tell process "SystemUIServer"
					click DisplaysMenuBarItem
					click menu item "Turn Airplay Off" of menu 1 of DisplaysMenuBarItem
				end tell
			end if
		end tell
	end if
	return 1 -- Call this handler again in one second's time.
end idle