Skip to content

Instantly share code, notes, and snippets.

@iband
Created March 31, 2011 10:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iband/896162 to your computer and use it in GitHub Desktop.
Save iband/896162 to your computer and use it in GitHub Desktop.
Apple Script to enable VPN autoconnect on MacOS X
on idle
tell application "System Events"
tell current location of network preferences
set myConnection to the service "VPN" #rename as your VPN connection called
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 #specify every 120 sec it checks if connected
end tell
end idle
#Save as Application and check "Stay open"
#To hide app icon from Dock:
#Click "Show Package content" from context menu of the saved application,
#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.
#Add the application to the Login items in your System Preferences.
#Done.
@badesemowo
Copy link

badesemowo commented Oct 6, 2020

I know this issue is old. I am posting my answer here because it took me a long while to find the solution and this page comes up on google. I hope it helps someone.

The Problem:
The issue was I got this error.

=====
Can’t get «class svce» "MY VPN" of «class locc» of «class netp» of application "System Events".

System Events got an error: Can’t get service "My VPN" of current location of network preferences. (-1728)

=====

The Reason:
I am using IKEv2 services. It seems the Apple Script (which I found all over the internet) fails to work with this. You can find details in the link below.

The solution:
Timac wrote a script to solve this problem. I simply downloaded the precompiled app here.
https://blog.timac.org/2018/0719-vpnstatus/

If you are interested in the source code that makes it work, you can find it on GitHub here: https://github.com/Timac/VPNStatus

You can also dig deeper into the problem if you are that sort of person on that link. It gives details.

If you are simply looking for a solution to Auto Connect your VPN, the VPNstatus app worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment