I recently laid my hands on a MacBook pro which I use at home and in the office. In the office I have a great monitor but I thought it would be a great idea to use my iMac as a monitor here at home. It all works just fine until you want to use your Apple wireless keyboard for your MacBook Pro.
Why would you want to do this, I hear you say? For many tasks, like designing in Sketch, I prefer to have only one big monitor instead of the laptop being open as well. To enable target display mode I have to make sure the keyboard is paired to the iMac and then press Command + F2. I can of course switch the keyboard off and then pair it to my laptop but this is a nuisance.
Especially if I decide to take a break and answer some emails from bed. I unplug the thunderbolt cable, answer some emails and come back. I can’t just plug the cable in and resume work because the iMac needs to be switched into target display mode with the keyboard, which is still paired to the laptop. Fiddlesticks.
Sharing The Keyboard
There is a great article by Guto Foletto about sharing the keyboard between devices. The first bash script will disable bluetooth on the local machine (MacBook), disable it on the iMac via SSH and then re-enable it on the MacBook. This will force the keyboard to pair with the laptop. The second bash scripts reverses the process. This is fine, but I realized that 99% of the time I don’t need all that. I am mostly using the iMac as a monitor at the moment so I am fine with having my wireless keyboard and trackpad paired with the laptop all the time.
Enabling Target Display Mode Remotely With SSH
What I really need is a way to switch on target display mode from my laptop. After some research I figured out that the surefire way of doing this from the iMac is to run an apple script from bash. It’s a pretty simple one-liner, here goes:
osascript -e 'tell application "System Events" to key code 144 using command down'
I saved this to my home folder on the iMac into a file named
tdm.sh. From my iMac I can now run
bash ~/tdm.sh and get it to switch to target display mode. Do all I need to do is ssh into the iMac from the laptop and run this script. Here’s the command for that:
ssh [email protected] "bash ~/tdm.sh"
If you’re new to all this there are two unknowns here. First of all, how do you know what your username and remote device name is – ie:
[email protected]. Then there’s the problem of SSH-ing without a password.
To solve the first one go to System Preferences and Sharing. You should see the message “Computers on your local network can access your computer at: computername.local” somewhere near the top. Your username is your actual username, I’m sure you know that.
As a side note, you may have to go to System Preferences and Sharing in your iMac as well. Make sure remote login on the left is checked.
To SSH into your iMac without a password you’ll need to use an SSH key. You don’t need to understand the process, it’s easy to copy paste, here are the steps. Issue the commands on each line separately.
# when you issue the following command you'll be asked some questions, keep hitting enter until done. ssh-keygen scp ~/.ssh/id_rsa.pub [email protected]:~/ ssh [email protected] mkdir .ssh cat id_rsa.pub >> ~/.ssh/authorized_keys rm -f id_rsa.pub exit
Once you’re done you can issue the
ssh [email protected] "bash ~/tdm.sh" command to toggle target display mode. You can also save it to a file and run it as a script. Again, this takes too long for my taste so I went a step further.
Enabling Target Display Mode From Alfred App
To make this happen I basically use the scripts we’ve been looking at so far with Alfred. I created a workflow from the “Keyword To Script” template which is in the “Essentials” category. I made the keyword “tdm” without an argument and used our SSH line for the script:
ssh [email protected] "bash ~/tdm.sh"
All done. I can now bring up Alfred with Command Space, type tdm, press enter and toggle target display mode remotely. Awesome.