I realize you may have found a happy solution at this point, but I was having a similar problem to solve in getting my Target Display Mode to work the I feel that it should. If I plug my Macbook Air into my 27" imac, I want my imac to do two things:
1) Hit Command+F2
2) Release the Bluetooth Keyboard and Mouse/Trackpad so that my macbook can pair them
I came across a github project by a developer named Duane Fields. Being a developer, I think he just coughed and built the solution. 🙂
Anyways, you'd have to download Xcode from the app store (large program, but useful) and the source code from github. If you have never used xcode, this might take a bit longer (as it did for me) to figure out how to install the dependencies correctly. In the end, I think it is worthwhile because the app works pretty well. It isn't perfect, but it solves an annoying problem for me.
https://github.com/duanefields/VirtualKVM
After you compile the VirtualKVM application, you could add it as a startup item (Users & Groups) so you wouldn't have to run it manually.
- Your macbook is running. (No app required here)
- Your 27" iMac is running, and so is VirtualKVM.
- You connect your MB to your iMac via the Thunderbolt cable
- VirtualKVM recognizes the Thunderbolt Connection and starts Target Display Mode (Command+F2)
- VirtualKVM disables bluetooth (No option to select specific devices; bummer)
- Your MB, if BT is on and previously paired with the BT Keyboard/Mouse, will pair with the devices (might have to push the power device, maybe)
- You are now in Target Display Mode with your Bluetooth Keyboard/Mouse paired to the Macbook (YaY!)
- Exiting Target Display Mode: (how I do it)
- Disable bluetooth or Disconnect the Keyboard/Mouse from the Macbook (your applescript might be useful here)
- Disconnect the Thunderbolt cable from one of the machines, or both if you want to.
- VirtualKVM (on the iMac) will end the Target Display Mode and turn bluetooth back on.
- After a second or two, your iMac and Macbook displays will return to their primary roles.
- A few seconds later (10-20 maybe?) your iMac should recognize the now free bluetooth keyboard/mouse and pair with them (might have to press the power button on your mouse/trackpad)
- Both systems should now be operating independently; can do it again just for giggles if you'd like.
As I said, it's not perfect but it does take some of the frustration out of the problem. If someone were to ask, he might even take the app a few steps further and include a feature to poll the Bluetooth Devices and disconnect specific items. That would be pretty cool.
Well, I hope that helped someone but I think it's a much cleaner solution than my Automator/Applescript fix I was trying to do when I came across this forum post.
Aloha!
Jeremy