Tag Archives: AppleScript

Automatically reconnect to VPN when disconnected

Since Austria introduced data retention some months ago I always try to be connected with the VPN of my university. Therefore, I use the VPN tool, built into Mac OS X Lion. However, whenever I restart my notebook or even if I only close it for some seconds, OS X looses the connection and I have to manually activate it again.

Today it bothered me enough that I started to search if there is a way to automatically reconnect to a VPN when the connection is lost. On Ask Different I found a small AppleScript which does exactly what I want.

First of all you need to open AppleScript Editor (you can find it in the /Applications/Utilities directory or simply open it using Spotlight) and paste the following script:

on idle
	tell application "System Events"
		tell current location of network preferences
			set myConnection to the service "TU Wien VPN"
			if myConnection is not null then
				if current configuration of myConnection is not connected then
					connect myConnection
				end if
			end if
		end tell
		return 30
	end tell
end idle

There is one variable you have to change and one you can change. First of all you need to change TU Wien VPN (line 4) to the name of your VPN connection. And you can change the interval the scripts waits until it checks again if the connection is lost. I configured the script to wait 30 seconds, but you can change the value if you want (line 11).

AppleScript Editor with VPN Reconnect Script

Next you need to save the script as application. You can do this by in the Save As dialog where you also have to set the option that the script stays open. Save the script somewhere where you can find it again. I choose the /Applications folder.

AppleScript Editor Save As Dialog for VPN Reconnect Script

Find the newly created application in Finder, right-click it and select Show Package Contents. Navigate in the Contents folder and open Info.plist in some text editor (right click, Open With, Other). For example, you could use TextEdit. Scroll down to the bottom of the file and before the last insert:

VPN Reconnect Script Info.plist File

You can now start the script with a double click. If you want to automatically start the script every time you start your Mac you can add it to the startup items. To do this open the System Preferences, select your user and click on Login Items. You can add the script by clicking on the plus button.

Source: Ask Different user iskra