Make sure you add the app invoking this script is in the Preferences -> Security & Privacy -> Pricavy -> Accessbility
For me, I use it with Alfred, so Alfred 3.app is in the list.
| tell application "System Events" | |
| tell application process "System Preferences" | |
| if not (window 1 exists) then | |
| tell application "System Preferences" | |
| activate | |
| end tell | |
| repeat while not (menu bar 1 exists) | |
| end repeat | |
| repeat while not (menu "System Preferences" of menu bar 1 exists) | |
| end repeat | |
| repeat while not (menu item "Network" of menu "View" of menu bar 1 exists) | |
| end repeat | |
| end if | |
| click menu item "Network" of menu "View" of menu bar 1 | |
| tell window 1 | |
| repeat while not (rows of table 1 of scroll area 1 exists) | |
| end repeat | |
| repeat with current_row in (rows of table 1 of scroll area 1) | |
| if value of static text 1 of current_row contains "{VPN partial name}" then | |
| select current_row | |
| exit repeat | |
| end if | |
| end repeat | |
| repeat with current_checkbox in (checkboxes of group 1) | |
| if name of current_checkbox is equal to "Connect on demand" then | |
| click current_checkbox | |
| exit repeat | |
| end if | |
| end repeat | |
| repeat with current_button in buttons | |
| if name of current_button is equal to "Apply" then | |
| click current_button | |
| exit repeat | |
| end if | |
| end repeat | |
| end tell | |
| end tell | |
| end tell |