Start screensaver/ lock screen on screen sharing disconnect

applescriptscreen-sharingscript

Is there a way with an AppleScript or shell script to start the screensaver when you disconnect from a screen sharing session?

For example, I connect via screen sharing from Mac A to Mac B. When I disconnect my remote screen sharing session I would like to start the screen saver on Mac B thus locking that screen.

Best Answer

ScreenSharing will post several distributed notifications during a screen sharing session. It is necessary to listen to those notifications to be able to act on them. I attempted to find a command line executable that is included with Mac OS X to perform this function. Although there is a notifyutil executable in Mac OS X, it does not act as an observer to the NSDistributedNotificationCenter. I also looked at the terminal-notifier project, but it appears to only post User (GUI) notifications.

I've created a Foundation executable that can be run in the background to detect when a screensharing session ends (and start the ScreenSaverEngine as mentioned by @Lauri Ranta above). I have posted the source for this executable on GitHub. You can use Xcode to build the project, and then launch the software as you'd like (via a launchd job?)

Related Question