Airplay automation

I am trying to automate playback to appletv via airplay. I need it to be able to open video files passed from terminal and then send it (using quicktime player) to appletv. Right now i have the following script:

To AirplayStream(aFile)
	tell application "QuickTime Player"
		activate
		try
			set frontDoc to front document
			close front document
		on error err number errNum
			if errNum is -1719 then
				-- There is no open document
			else if errNum is -10000 then
				-- Front doc exists, but does not really...
			else
				log err
			end if
		end try
		open aFile
		tell application "System Events" to tell process "QuickTime Player"
			tell window 1
				click button 4
				delay 1
				pick menu item "AppleTV" of menu 1 of button 4
			end tell
		end tell
		play front document
	end tell
end AirplayStream

on run 
	set unixFile to "path/to/file"
	set macFile to POSIX file unixFile
	set fileRef to macFile as alias
	my AirplayStream(fileRef)
end run

When i run it i get to a point, where the airplay button is clicked and then i get an error “Can’t get menu 1 of button 4 of window 1 of process “QuickTime Player”. Invalid index”. I tried many combinations of indexing and none seem to work.

I am on MacOS 12.

I’m using 10.12 so it’s possible that things have changed — conceivably even with quicktime player.

That said, button 4 for me is ‘fast forward’. The airplay button is button 5.

If I get the ‘name’ of the buttons, the following are returned with ‘show external playback menu’ being the airplay button.

accessibility description of buttons
{"full volume", "mute", "rewind", "fast forward", ¬
				"show external playback menu", "share"}

I don’t have any airplay devices handy so I can’t test anything beyond that.

FWIW, I have no idea where the pick command comes from. It is not in System Events’ dictionary on my system. It is however, formatted like a command (ie in bold type).

On Ventura here so possibly not applicable, but with multiple airplay destinations.

tell application "QuickTime Player"
	activate
end tell
tell application "System Events" to tell process "QuickTime Player"
	tell button 5 of window 1--This is the airplay button under MacOS 13 Ventura
		perform action "AXPress" 
		delay 1
		--Selection menu with multiple AirPlay destinations appears. These are checkboxes in a scroll area.
		--There the trail ends. I can find no access to this window.
	end tell
end tell