@@ -170,8 +170,8 @@ function _lazy_connect_mojave() {
170170 tell application "System Events"
171171 tell process "System Preferences"
172172 tell window 1
173+ delay 1
173174 repeat with r in rows of table 1 of scroll area 1
174-
175175 if (value of attribute "AXValue" of static text 1 of r as string) is equal to "$osx_vpn_name " then
176176 select r
177177 end if
@@ -206,6 +206,103 @@ function _lazy_connect_mojave() {
206206EOF
207207}
208208
209+ function _lazy_connect_ventura(){
210+ vpn_name=$1
211+ osx_vpn_name=" ${vpn_name/ Connect / } "
212+
213+ _lazy_connect_get_totp $2
214+ local autofill=$3
215+
216+ if [ -z " $password " ]; then
217+ case $TOTP_MODE in
218+ oathtool)
219+ echo " Error: Unable to generate otp using oathtool."
220+ return 1
221+ ;;
222+ yubikey)
223+ echo " Error: No YubiKey found."
224+ return 1
225+ ;;
226+ esac
227+ elif [ " $autofill " == " false" ]; then
228+ echo -n " $password " | pbcopy
229+ fi
230+
231+ osascript -l JavaScript > /dev/null << -EOF
232+ function findGroupIndex(vpnName, areas) {
233+ let idx = -1
234+ for (let i = 0; i< areas.groups.length; i++) {
235+ let g = areas.groups[i]
236+ if (g.staticTexts[0].value() === vpnName) {
237+ idx = i
238+ break
239+ }
240+ }
241+ return idx
242+ }
243+ function connectVpn(vpnName, password, autofill) {
244+ // Reveal System Settings
245+ const app = Application("System Settings")
246+ app.reveal()
247+ const settings = Application("System Events").applicationProcesses.byName("System Settings")
248+
249+ // Focus on it
250+ settings.frontmost = true
251+
252+ // Wait for System Settings window appearing
253+ delay(0.5)
254+
255+ // Reveal VPN panel
256+ settings.menuBars[0].menuBarItems["View"].menus["View"].menuItems["VPN"].click()
257+
258+ // Wait for panel switching
259+ delay(0.5)
260+
261+ // check the vpn name
262+ const areas = settings.windows[0].groups[0].splitterGroups[0].groups[1].groups[0].scrollAreas[0]
263+ let idx = findGroupIndex(vpnName, areas)
264+
265+ let checkbox = areas.groups[idx].checkboxes[0]
266+ if (idx > -1 && checkbox.value() === 0) {
267+ const button = areas.groups[idx].buttons[0]
268+ if (button !== null) {
269+ button.click()
270+ }
271+
272+ delay(0.5)
273+
274+ const sheet = settings.windows[0].sheets[0]
275+ const dialogArea = sheet.groups[0].splitterGroups[0].groups[1]
276+ const authArea = dialogArea.scrollAreas[0].groups[2]
277+
278+ const passwordField = authArea.textFields[1]
279+ passwordField.focused = true
280+ passwordField.value = password
281+ passwordField.focused = false
282+
283+ try {
284+ let ok = dialogArea.buttons[2]
285+ if (ok.enabled()) {
286+ ok.click()
287+ } else {
288+ throw "Could not OK"
289+ }
290+ } catch (e) {
291+ let cancel = dialogArea.buttons[1]
292+ if (cancel.enabled()) {
293+ cancel.click()
294+ }
295+ }
296+
297+ delay(0.5)
298+ checkbox.click()
299+ }
300+ app.quit()
301+ }
302+ connectVpn("$osx_vpn_name ", "$password ", "$autofill ")
303+ EOF
304+ }
305+
209306version_lte () {
210307 [ " $1 " = " ` echo -e " $1 \n$2 " | sort -V | head -n1` " ]
211308}
@@ -261,11 +358,14 @@ function lazy-connect() {
261358 fzf --height=10 --ansi --reverse --query " $* " --select-1)
262359
263360 mac_version=$( sw_vers -productVersion)
361+ is_less_than_ventura=$( version_lt $mac_version 13.0 && echo " yes" || echo " no" )
264362 is_less_than_mojave=$( version_lt $mac_version 10.14 && echo " yes" || echo " no" )
265363 if [ $is_less_than_mojave = " yes" ]; then
266364 [ -z " $vpn_name " ] || _lazy_connect " $vpn_name " " $secret " " $autofill "
267- else
365+ elif [ $is_less_than_ventura = " yes " ] ; then
268366 [ -z " $vpn_name " ] || _lazy_connect_mojave " $vpn_name " " $secret " " $autofill "
367+ else
368+ [ -z " $vpn_name " ] || _lazy_connect_ventura " $vpn_name " " $secret " " $autofill "
269369 fi
270370
271371}
0 commit comments