1

I have an Apple TV in the living room. I have a Mac mini in my office. I have a wireless keyboard and mouse that can easily reach between the two locations, but am not willing to walk between them to turn on Airplay.

I'd like to connect via AirPlay with a keystroke from my wireless keyboard, which would turn on Airplay to my living room computer. I would prefer this to be silently performed in the background.

This is apparently quite difficult. I spent two hours on the command line approach. I found no working commands. This is very surprising to me.

I've attempted connecting using Automator with a Run AppleScript action selecting the menu item as a service with a keystroke.

The AppleScript code:

on run {input, parameters} tell application "System Events" tell process "SystemUIServer" click (menu bar item 1 of menu bar 1 whose description contains "Displays Menu") key code 125 key code 125 keystroke return delay 5 end tell end tell return input end run 

It works from Automator. It only runs when Finder is frontmost, and even then it requires me first to open the Finder > Services menu once (not even choose anything, just open that). This seems to be a known bug...

I need this command to work all the time, without any mouse input (because I cannot see my screen).

Any suggestions as to a viable path forward would be very much appreciated.

3
  • What happens when you try to run the Automator Quick Action as a keyboard shortcut? Commented Dec 18, 2020 at 15:28
  • It plays the alert sound. Nothing else. Unless the finder is focal, in which case it works. Not very useful. Commented Dec 19, 2020 at 23:57
  • I have a similar setup. My lazy alternative to avoid walking is to use an iOS device that's usually with me to connect via VNC and move the cursor to select AirPlay. The lock screen may appear upon initial connection, but I can use the wireless keyboard to get past that. This is far from automated, but at least it eliminates the walking. Commented May 7, 2024 at 1:48

2 Answers 2

0

If you first brought finder to the front in the script, would it work:

 on run {input, parameters} tell application "Finder" to activate tell application "System Events" tell process "SystemUIServer" click (menu bar item 1 of menu bar 1 whose description contains "Displays Menu") key code 125 key code 125 keystroke return delay 5 end tell end tell return input end run 
4
  • I see that, and mention it in my question. However, it does not help with the goal of being able to connect without being present. It is unrealistic to try to always leave the computer with the finder in-focus. Commented Dec 19, 2020 at 23:56
  • If you bring the Finder to the front with a script you don’t need to leave it in focus. The Finder is always open while you are logged in the the computer, unless it has crashed. If you also need to open the Finder > Services menu that may be possible using AppleScript. By connecting without being present, do you mean you want to do this with something other than a keystroke on your Mac Mini, e.g. with an action on the Apple TV or an iPad/iPhone? I doubt very much that that would be possible. Commented Dec 20, 2020 at 1:26
  • So, I will be at an apple TV holding a wireless keyboard connected to the Mac. I press the key combination, and the mac connects to the apple tv. No 'clicking' is possible, because for that I would need to carry the wireless keyboard to the mac and use the mouse... Commented Dec 22, 2020 at 21:47
  • I'm sorry, I should be clear: this does not work. It simply play the present 'alert' sound. In my conversation with support today I brought that up. This is apparently as intended. The only way to make the finder focal is 'by hand' with the mouse or keyboard, and Automator will not. Commented Dec 23, 2020 at 21:25
0

There appears to be no path forward. In a conversation with Apple Support I was eventually told that 1) Automator is incompatible with many security features of MacOS, and would never support such an action and 2) that no third party or command line utility could accomplish this. It was suggested that I might develop such a tool using Swift, but that it too would likely not function unless it was focal.

Apple, therefore, does not at present allow access of the Airplay function in a way that supports even basic automation.

3
  • This is still true as of July 2022. Commented Jul 10, 2022 at 14:06
  • This is still true as of May 2024. Commented May 25, 2024 at 0:07
  • I thought you now use windows and android so you could change the OS more easily? Commented Jun 6 at 12:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.