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.

2
  • What happens when you try to run the Automator Quick Action as a keyboard shortcut? 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. Dec 19, 2020 at 23:57

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. 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. 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... 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. 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.

1
  • This is still true as of July 2022. Jul 10, 2022 at 14:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .