Thursday, February 13, 2014

[Mac OS X] Start VPN at login

To access GIT server, I must have VPN connected in my Mac. It's quite annoying to remember starting VPN every time log in or when the VPN lost connect. To make my life easier, I decided to use AppleScript to write a piece of script to automatically start VPN at login and check the connection. It also opens Terminal after the connection. Here is it:

on idle -- check the connection won't be dropped
    tell application "System Events"
        tell current location of network preferences
            set VPNservice to service "VPN (IPSec)" -- name of the VPN service
            if exists VPNservice then
                if current configuration of VPNservice is not connected then
                    connect VPNservice
                    delay 10 -- enter password and connect
                end if
            end if
        end tell
    end tell
end idle

tell application "System Events"
    tell current location of network preferences
        set VPNservice to service "VPN (IPSec)" -- name of the VPN service
        set isConnected to connected of current configuration of VPNservice
        if isConnected then
            -- do something
            tell application "Terminal"
                activate
                delay 1
                do script with command "ls"
            end tell
        end if
    end tell
end tell


If you are using  iTerm2, you can also replace the second part of the script with the following:

tell application "System Events"
    tell current location of network preferences
        set VPNservice to service "VPN (IPSec)" -- name of the VPN service
        set isConnected to connected of current configuration of VPNservice
        if isConnected then
            -- do something
            tell application "iTerm"
                activate
                delay 1
               
                tell the first terminal
                    tell the last session
                       
                        -- write some text
                        write text "cd ~/Projects_folder"
                        delay 1
                        write text "git branch"
                       
                    end tell
                end tell
            end tell
        end if
    end tell


Export this script as an application, make sure the 'Stay open after run handler' is checked.


Note:
For Terminal: I set a startup command in Terminal to make sure I'm working on the right branch in GIT. Input commant at Terminal --> Preference --> Settings --> Default Profile --> Shell --> Startup | Run Command:

cd ~/Projects_folder; git branch


For iTerm, you can also create a file AutoLaunch.scpt in ~/Library/Application\ Support/iTerm/ to let iTerm run some commands every time at startup. For example:

tell application "iTerm"
    activate
   
    -- make a new terminal
    set myterm to (make new terminal)
   
    -- talk to the new terminal
    tell myterm
        -- launch a default shell in a new tab in the same terminal
        launch session "Default Session"
        tell the last session
            -- write some text
            write text "cd Projects_folder"
            write text "git branch"
        end tell
    end tell
 end tell

Now let the application start at login:
  • Open 'System Preferences' --> Users & Groups --> Account_name --> Login Items.
  • Click '+' button and add the application.

When I login every time from now on, I don't have to remember to connect VPN,  open Terminal to type and check my working branch.