Skip to content

Instantly share code, notes, and snippets.

@tmanternach
Last active January 23, 2024 13:03
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tmanternach/cbd4c213eab8569e38d6cd021b6255e5 to your computer and use it in GitHub Desktop.
(*
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
@WladyX
Copy link

WladyX commented Jul 17, 2022

Works great, thank you for sharing!

@tmanternach
Copy link
Author

Works great, thank you for sharing!

Appreciate the comment, cheers!

@jacksonhorton
Copy link

Awesome!

@manuelQuiroz
Copy link

Could you help me, how do I add my access credentials to this code?

@tmanternach
Copy link
Author

Are you talking about the username/password prompt that appears after you click Connect?

@manuelQuiroz
Copy link

username/password prompt that appears before click Connect

@tmanternach
Copy link
Author

tmanternach commented Nov 16, 2022

username/password prompt that appears before click Connect
You could try this. Not sure how this behaves on disconnects, though. I will keep playing and report back any improvements.

tell application "System Events" to tell process "GlobalProtect"
	click menu bar item 1 of menu bar 2
	tell application "System Events"
		keystroke tab
		delay 0.2
		keystroke "username"
		delay 0.2
		keystroke tab
		delay 0.2
		keystroke "password"
		delay 0.2
	end tell
	click button 2 of window 1
end tell

@manuelQuiroz
Copy link

awesome!!! thanks

username/password prompt that appears before click Connect
You could try this. Not sure how this behaves on disconnects, though. I will keep playing and report back and improvements.

tell application "System Events" to tell process "GlobalProtect"
	click menu bar item 1 of menu bar 2
	tell application "System Events"
		keystroke tab
		delay 0.2
		keystroke "username"
		delay 0.2
		keystroke tab
		delay 0.2
		keystroke "password"
		delay 0.2
	end tell
	click button 2 of window 1
end tell

@WladyX
Copy link

WladyX commented Jul 21, 2023

Doesn't seem to work anymore on Ventura, does anyone have an updated version?
Thanks

Later edit:
Seems sometimes it is working, other times it says:

 execution error: System Events got an error: Can’t get window 1 of process "GlobalProtect". Invalid index. (-1719)

@tmanternach
Copy link
Author

@WladyX This script still works for me on Ventura. What version of GlobalProtect do you have installed? Click the icon in the menu bar, then the "3 lines icon" and then About. I can try to test (and tweak) with whatever version you have.

That System Events error makes it look like GlobalProtect is just not running when you sometimes run the AppleScript. I'm not sure why that might be, mine runs on my Mac all the time (but isn't always connected to my VPN).

@WladyX
Copy link

WladyX commented Jul 21, 2023

I have 6.0.1-19
Same in my case, GP runs all the time, but it is connected to VPN only on demand.
Another thing i noticed on ventura, the script used to be very fast before, now on ventura it's very slow, does it behave the same for you?

Also i have a popup after i connect and i don't know how to get rid of from the apple script, do you have any pointers?
image

@tmanternach
Copy link
Author

@WladyX I have upgraded to 6.0.1-19 (on Ventura 13.4.1) and my script still works every time. Sorry I'm not more helpful there!

Also i have a popup after i connect and i don't know how to get rid of from the apple script, do you have any pointers?

I can't replicate this message, so I am unable to figure out how to click that close button using Applescript. If you are the admin of your firewall, though, I believe this App Config setting will hide these messages for all end-users: Display IPSec to SSL Fallback Notification (set to "No" to hide).

@WladyX
Copy link

WladyX commented Jul 21, 2023

I am not admin unfortunatly :)
Thank you for your support!
Seems like the script is working, just slowly.

@tmanternach
Copy link
Author

@WladyX I forgot to address that question, sorry. My script is running immediately. My VPN itself might take a couple of seconds to connect, but that happens after the AppleScript completes.

@WladyX
Copy link

WladyX commented Jul 21, 2023

for me after i launch the alfred script, it takes about 7s for the menubar GP to become visibile and another 7s for the script to click on the button.
any ideea what could cause this?
I have also tried from the script editor and it is the same, so it's not your script or alfred that is causing this.

@tmanternach
Copy link
Author

for me after i launch the alfred script, it takes about 7s for the menubar GP to become visibile and another 7s for the script to click on the button. any ideea what could cause this? I have also tried from the script editor and it is the same, so it's not your script or alfred that is causing this.

@WladyX Can you open "Script Editor" on your Mac and paste the AppleScript above into a new document; then run it right inside Script Editor and see if the slowness is still there or not?

@WladyX
Copy link

WladyX commented Jul 21, 2023

you missed my last sentence :))

@tmanternach
Copy link
Author

Dang, I need more ☕️ this morning! I am out of ideas then.

@WladyX
Copy link

WladyX commented Jul 21, 2023

cool, no worries, i'll have to see if other apple scripts are also slow, or only this one and start digging.
Thank you for your support!

@WladyX
Copy link

WladyX commented Aug 8, 2023

Update: looks like there was a MacOS, i suspect some DNS issues, long story short after a reboot all was working (fast) as expected.

@orizonlabs
Copy link

Hi,
First, this is awesome I was wondering how to manage this for a long time now. Not having much experience in applescript, this is really useful thanks!

On my side using Ventura 13.5 and GlobalProtect 6.2.0-89 it works very well for connecting, but not for disconnecting. I assume the UI has changed since there is not 'disconnect' button in place of the 'connect' button when the VPN is active. Instead I need to click on the three lines button on top right and select the last option.
I managed to activate the top right button with click button 1 of window 1 but I can't figure out how to select the last menu item with apple script. Any chance you can help?

@tmanternach
Copy link
Author

Hi, First, this is awesome I was wondering how to manage this for a long time now. Not having much experience in applescript, this is really useful thanks!

On my side using Ventura 13.5 and GlobalProtect 6.2.0-89 it works very well for connecting, but not for disconnecting. I assume the UI has changed since there is not 'disconnect' button in place of the 'connect' button when the VPN is active. Instead I need to click on the three lines button on top right and select the last option. I managed to activate the top right button with click button 1 of window 1 but I can't figure out how to select the last menu item with apple script. Any chance you can help?

Any chance you would be able to email me a couple of screenshots of your disconnect process? My email is on my github profile if you want to keep it private.

@tmanternach
Copy link
Author

@orizonlabs I've added a new applescript to this gist that might work for you. I updated to 6.2.0-89, but mine does not behave the same way as yours so I had to do a little guessing.

Please report back if this works or not. ;-)

@orizonlabs
Copy link

orizonlabs commented Aug 15, 2023

Hi!
Many thanks for your feedback.

Running the updated script does not much, and has the following output:
menu bar item 1 of menu bar 2 of application process "GlobalProtect" of application "System Events"

Here is a screenshot of the disconnect button, it's the last one which says 'Déconnecter', in French :)

https://dsc.cloud/angeswadman/globalprotect.jpg

EDIT: I was eager to answer, and didn't test it deeply enough. I translated the items menus in the script, and it works now. The only item I didn't translate was "Global Protect Options Menu".

@tmanternach
Copy link
Author

@orizonlabs Sorry for the delayed response! I came to suggest you translate those items to French, but it looks like you beat me to it! I'm glad it works for you! Come back if it ever breaks, happy to help. 🍻

@mello
Copy link

mello commented Jan 23, 2024

@tmanternach Thanks so much for posting!

In case it's helpful for anyone else, I modified this script to deal with multiple VPN connections:

tell application "System Events" to tell process "GlobalProtect"
	click menu bar item 1 of menu bar 2
	set statusText to name of static text 2 of window 1
	if statusText is "Not Connected" then
		tell pop up button "Portal" of window 1
			click
			delay 0.2
			click menu item "[NAME OF VPN]" of menu 1
		end tell
	end if
end tell

I have one of these scripts for each of my VPN connections and trigger them with an Alfred workflow using a keyword.

@tmanternach
Copy link
Author

@mello You are welcome; I'm glad it's working for you, I like the idea for multiple connections. Thanks for the kind words.

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