Skip to content

Instantly share code, notes, and snippets.

@tmanternach
Last active December 27, 2024 17:22
Show Gist options
  • Save tmanternach/cbd4c213eab8569e38d6cd021b6255e5 to your computer and use it in GitHub Desktop.
Save tmanternach/cbd4c213eab8569e38d6cd021b6255e5 to your computer and use it in GitHub Desktop.
(*
Toggle GlobalProtect VPN with AppleScript
Tested using macOS Sequoia 15.1.1 and GlobalProtect version 6.2.3-270
Written by Trevor Manternach, August 2023.
*)
tell application "System Events" to tell process "GlobalProtect"
click menu bar item 1 of menu bar 2
set statusText to name of static text 1 of window 1
if statusText is "Not Connected" then
# GlobalProtect is disconnected, so let's connect
click button "Connect" of window 1
set entireContents to entire contents of window 1
else if statusText is "Connected" then
# GlobalProtect is connected, so let's disconnect
set windowText to entire contents of window 1
repeat with theItem in windowText
if (name of theItem contains "Disconnect") then
click theItem
end if
end repeat
end if
click menu bar item 1 of menu bar 2
end tell
(*
Toggle GlobalProtect VPN with AppleScript
Tested using macOS Ventura 13.4.1 and GlobalProtect version 6.2.0-89
Written by Trevor Manternach, August 2023.
*)
tell application "System Events" to tell process "GlobalProtect"
click menu bar item 1 of menu bar 2
set statusText to name of static text 1 of window 1
if statusText is "Not Connected" then
# GlobalProtect is disconnected, so let's connect
click button "Connect" of window 1
set entireContents to entire contents of window 1
else if statusText is "Connected" then
# GlobalProtect is connected, so let's disconnect
set windowText to entire contents of window 1
repeat with theItem in windowText
if (class of theItem is button) then
if (value of attribute "AXTitle" of theItem is "Disconnect") then
# We found a Disconnect button on the main page, let's click it.
click theItem
exit repeat
else
# We did not find a Disconnect button on the main page, let's hope there is one in the Settings Menu.
click button "Global Protect Options Menu" of window 1
click menu item "Disconnect" of menu "Global Protect Options Menu" of button "Global Protect Options Menu" of window 1
exit repeat
end if
end if
end repeat
end if
click menu bar item 1 of menu bar 2
end tell
(*
Toggle GlobalProtect VPN with AppleScript
Tested using macOS 10.14 and GlobalProtect version 5.2.3-22
Tested using macOS Ventura 13.4.1 and GlobalProtect version 6.0.1-19
Written by Trevor Manternach, July 2021.
*)
tell application "System Events" to tell process "GlobalProtect"
click menu bar item 1 of menu bar 2 -- Activates the GlobalProtect "window" in the menubar
click button 2 of window 1 -- Clicks either Connect or Disconnect
click menu bar item 1 of menu bar 2 -- This will close the GlobalProtect "window" after clicking Connect/Disconnect. This is optional.
end tell
@mmacvicar
Copy link

@Mellbourn I do not use this script but I experienced the same error and noticed in my environment the Disconnect and Refresh Connection buttons lay within menu 1 of the "Global Protect Options Menu". So try changing that line to:

click menu item "Disconnect" of menu 1 of button "Global Protect Options Menu" of window 1

@Mellbourn
Copy link

@mmacvicar unfortunately, that also fails. Syntax error:
System Events got an error: Can’t get menu item "Disconnect" of menu 1 of button "Global Protect Options Menu" of window 1 of process "GlobalProtect".

@tmanternach
Copy link
Author

@mmacvicar unfortunately, that also fails. Syntax error: System Events got an error: Can’t get menu item "Disconnect" of menu 1 of button "Global Protect Options Menu" of window 1 of process "GlobalProtect".

Try this script:

tell application "System Events" to tell process "GlobalProtect"
	click menu bar item 1 of menu bar 2
	set statusText to name of static text 1 of window 1
	if statusText is "Not Connected" then
		# GlobalProtect is disconnected, so let's connect
		click button "Connect" of window 1
		set entireContents to entire contents of window 1
	else if statusText is "Connected" then
		# GlobalProtect is connected, so let's disconnect
		set windowText to entire contents of window 1
		repeat with theItem in windowText
			if (name of theItem contains "Disconnect") then
				click theItem
			end if
		end repeat
	end if
	click menu bar item 1 of menu bar 2
end tell

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