Skip to content

Instantly share code, notes, and snippets.

@kineticac
Last active October 6, 2022 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kineticac/ca2d2c2b071c564abc78d8d67f0f0e4f to your computer and use it in GitHub Desktop.
Save kineticac/ca2d2c2b071c564abc78d8d67f0f0e4f to your computer and use it in GitHub Desktop.
Using iMac as a target display for MacBook
do shell script "ssh username@IPaddress '~/Dropbox/code/target-display/targetdisplay.sh'"
# This script lives on the machine that will be used as the Target Display.
# Tested on a 2012 iMac running Catalina, and theoretically should work on other
# machines.
# This script is basically sending the Command + F2 (or Command + Fn + F2) keypress
# that will send the Target Display machine into a target display mode. Helpful
# if you don't have an Apple keyboard attached, or want to execute Target Display
# from another machine via SSH, such as in the following scripts in this gist.
osascript -e 'tell application "System Events" to key code 144 using command down'
@kineticac
Copy link
Author

Setup

  1. Create targetdisplay.sh such that it can be accessed on the Target Display machine. I put it in a dropbox folder for easy access.
  2. Create SSH access on the Target Display machine so you can execute the targetdisplay.sh through SSH.
  3. Remove authentication requirements by adding the local machine's public key to the authorized_keys file of the Target Display machine.
  4. Create an AppleScript on the local machine to execute the Target Display mode on the Target Display machine.

SSH Access on Target Display machine

  1. System Preferences -> Sharing -> Remote Access
  2. Create a user+password, remember the password for now.
  3. Setup SSH keys on the local machine, if you haven't already. It should be here: ~/.ssh/id_dsa.pub. If not, in the local machine's terminal, run ssh-keygen, and do not password protect it, just hit enter when it prompts you (this is slightly not as safe, so if someone gets access to it somehow, they can access your keys without a password, but with a password, you'll be prompted everytime and the AppleScript won't work).
  4. Add the local machine's ssh key to the authorized keys for the Target Display machine: cat ~/.ssh/id_dsa.pub | ssh user@remotehost 'cat >> ~/.ssh/authorized_keys', only this time you will be prompted for your ssh password from step 2. remotehost is the IP address on our local machine.
  5. Try it now to see if you can ssh into the Target Display Machine from the local machine without a password: ssh user@remotehost
  6. If all goes well, you can then create the AppleScript on the local machine, which will ssh and execute the Command+fn+f2 command, sending the Target Display machine into Target Mode. To exit, just run the AppleScript again. You can use ScriptEditor on your local machine to modify the script, or export it as an Applicaton to your Applications folder and run it that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment