I use a Retina MacBook pro as my primary computer & I have a 27″ iMac that I occasionally use as an external display for the additional screen real-estate.
It’s relatively straight forward to enable this using a feature called Target Display Mode, simply connect a thunderbolt cable and hit Command (⌘)-F2 on the iMac’s keyboard.
The only issue with this is iMac’s the keyboard (and mouse) are not usable, as they are still connected to the iMac. I needed to figure out a way to start target display mode and then connect the keyboard & mouse to my MacBook. After a little research I figured out I could do this over my local network with SSH and a couple of simple scripts.
How it works
To start Target Display Mode I run the command `imac start`, it’ll make sure bluetooth is enabled on the MacBook, wait a second, then SSH into the iMac and run a script on the iMac called “start.sh”. This script on the iMac executes the Command (⌘)-F2 keyboard command (with code), then switches off bluetooth on the iMac so the MacBook can take over the keyboard & mouse.
To end Target Display Mode I run the command `imac end`, it’ll make sure bluetooth is off on the MacBook, wait a second, then SSH into the iMac and run a script on the iMac called “end.sh”. This script switches on Bluetooth on the iMac, then runs the Command (⌘)-F2 command to end the Target Display mode session, the iMac will then take over the keyboard and mouse.
I have added a couple of other handy aliases, like `imac ssh` & `imac shutdown` which are both pretty self explanatory ; )
How to set it up
1. Install Blueutil on both macs
Blueutil is a handy command line utility to help us toggle bluetooth on and off.
2. Create three scripts on your iMac
Create the following scripts (start.sh, end.sh and shutdown.sh) on your iMac in a directory of your choice.
3. Create an alias on your MacBook
This handy alias / function will save you having to type in a bunch of the same commands over and over. Paste the code below into your .zshrc file on your MacBook, modify the alias code to suit your requirements, directory names & computer network name.
- As far as I know you need an official Apple keyboard connected when you’re running these scripts, even if you’re emulating the keyboard commands with code.
- You won’t be able to connect to your iMac unless it’s logged in.
- If you’re having any issues with SSH you can checkout this article or this article for some tips.
- There’s probably a way to automate this all further via the command line on the MacBook however I’ve found the script approach to be much more reliable.
- If you’re not comfortable on the command line there’s an alfred workflow that may work for you!