Screen Sharing via SSH With A Single Click
This tip uses AppleScript and shell script to create an AppleScript application that launches an ssh secured Screen Sharing (VNC client/server, introduced in OS X 10.5) to your remote machine.
Please see the updated, simpler version
Note: there has to be more an elegant way of doing this, using either AppleScript or shell script, but because I'm not perfectly familiar with either of them I'll be using a combination. However, if you know how to combine these (or already use only one of them) please, please do drop me a note or comment on this article.
First, make sure you have Screen Sharing enabled (System Preferences -> Sharing -> Screen Sharing).
Second, make sure you can use passwordless ssh login.
Third, create a shell script that contains the following:
ps axww | grep [s]sh | grep -q "[5]920:127.0.0.1:5900"
if [ $? != 0 ] ; then
# ssh port forwarding not running, so start over
ssh -N -f -L 5920:127.0.0.1:5900 username@remotehost
fi
/Users/username/location/Launch_Screen_Sharing.app
replace username (e.g. johndoe), location (e.g. Scripts) and remotehost (e.g. mymachine.homeip.net) with your details. Save this shell script to the location, giving it a .command suffix (e.g. ssh_and_share.command), and making it executable.
We'll now create the file Launch_Screen_Sharing.app that was references in the last line of the shell script. Open Script Editor (in Application -> Utilities -> AppleScript). Type in the following:
tell application "Finder"
open location "vnc://localhost:5920"
end tell
Save this as an application in the same folder as the previous file.
You can now launch the ssh_and_share.command, either from command line, or dragging the file to your Dock and executing it from there. However, because this is a shell script it uses Terminal and opens an extra Terminal window. We'll get rid of it.
Again, open Script Editor and type in:
do shell script "/Users/username/location/ssh_and_share.command"
changing username and location, and save it in your location. Now drag this file to your Dock, and voilà! No more Terminal.
As said, if you know how to do all this using only AppleScript or only shell scripting, please let me know.