Is there a code that I can just copy an paste into Terminal or Automator? If not what is the best Application to download?
You can do that very easily using ControlPlane, a free (open-source) application:
ControlPlane, a fork of MarcoPolo, brings context and location sensitive awareness to OS X. With ControlPlane you can intelligently reconfigure your Mac or perform any number of actions based on input from a wide variety of evidence sources [...]
It's very easy to set-up. You define a "context", select a rule (or multiple rules):
And link an action to it: