November 11, 2010
by Fardjad
One good thing about OS X is the AppleScript. It’s simple, powerful and useful. You can simply automate your tasks with it and make your life easier. A few month ago I wrote a VPN Auto Connect Script to keep my VPN connection alive and now I would like to share it
Open your desired Editor or use OS X AppleScript Editor(Application/Utilities/AppleScript Editor) and Paste the following code (note that you should change “VPN” to your connection name at line 4):
on idle
tell application “System Events”
tell current location of network preferences
set myConnection to the service “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 120
end tell
end idle
Save the script as Application and check Stay Open:

Save Script as Application
And now, when you open VPN Auto Connect, It automatically dials the connection every 120 sec.
When you open the Application, it’s icon will be appeared on the dock which is a bit annoying. To solve this problem we’ll make it an agent:
Right-Click on the Application Icon and click Show Package Contents.
Open Contents/Info.plist (you will need XCode or another PList Editor to edit Info.plist file). Click Add Item, select “Application is agent (UIElement)” and check it:

Application is agent (UIElement)
Save the file and now your Application package should be ran with no icons on the dock.
You may want to add it to Login Items to make it open automatically at login. There are a few ways to do that but the most simple way is to drag the application into the dock, Right-Click on it and select Options/Open at Login, and finally Drag it off to remove it from the dock.
You can also download my script here. (Note that it only works if your connection name is VPN so you should edit vpn-auto-connect.app/Contents/Resources/Scripts/main.scpt to set the connection name).
That’s all folks
hope you liked it.