If you happen to use a VPN on your personal or professional Mac on a daily basis, you might want your Mac to automatically connect to your chosen VPN when it boots up. That way you are eliminating the need of manually launching the VPN interface and making a couple of clicks to connect to it.
While there’s no built-in option on Macs to automatically connect to a VPN when the Mac boots up, you can make use of an AppleScript to do it. What this script does is ask you to input the name of the VPN you would like to connect to, and then it runs every time your Mac starts to get you connected to your chosen VPN.
Here’s how you can go about doing that on your Mac.
Creating an Application from AppleScript for Connecting to VPN
In this section, you’ll create an application that contains AppleScript to help you establish a connection to the VPN. Once the app has been created, you’ll move on to placing it in the login items so that it automatically launches each time you start up your Mac.
1. Launch Script Editor on your Mac by searching for it in the Launchpad.
2. When the Script Editor launches, click on the “File” menu on the top and select the option that says “New.” It will let you create a new script in the app.
3. You should now have an empty panel on your screen where you can write your script. What you need to do now is copy the following script and paste it onto that empty panel.
In the following script, make sure to replace “MYVPNServer” with your own VPN.
on idle tell application "System Events" tell current location of network preferences set myVPN to the service "MYVPNServer" if myVPN is not null then if current configuration of myVPN is not connected then connect myVPN end if end if end tell return 60 end tell end idle
4. Your script is now ready to be saved as an application. To save it, click on the “File” menu on the top and select the option that says “Save…”
5. On the following screen you will be asked to enter the script details such as the name and the script type.
In the name field you can enter any meaningful name.
As for the location of the app, you can save it wherever you want, but make sure to choose a location where you can find it easily.
From the “File Format” drop-down menu, select “Application” as you are saving the script as an application on your Mac. Also, select the box that says “Stay open after run handler.”
Click on “Save” to save the application on your Mac.
The script has been successfully saved as an application – now what you need to do is add it to the login items so that it launches whenever your Mac boots up as described below.
Adding the VPN App to the Login Items
1. Click on the Apple logo in the top-left corner of your screen and select the option that says “System Preferences…” to be taken to the System Settings panel for your Mac.
2. When the System Preferences panel launches, click on the option that says “Users & Groups” to go to the users panel.
3. Click on your account name in the left panel, and then click on the “Login Items” tab in the right panel.
Click on the “+” (plus) sign as shown below to add a new app to the login items list.
4. On the following screen you need to navigate to the location where you saved the application and click on it, and then click the button that says “Add.”
5. You should see the app appearing in the list which indicates the app will automatically launch whenever your Mac boots up, and then the app will connect to the specified VPN.
If you’d like to save some time connecting to a VPN of your choice, then you can use the above AppleScript that automatically connects to your chosen VPN without requiring any interaction from your side.