AppleScript to connect Cisco Anyconnect plus PIN generated in MobilePASS
You know those moments when you need to connect to a corporate VPN and all the steps and clicks are just getting terribly annoying? Well I went through the same phase and decided to write something to make my life a tad easier.
Setup:
- Cisco Anyconnect
- After clicking on Connect you get a username and password prompt
- Password is the token generate inside MobilePASS app (that runs on the Mac and not on the phone:) )
- MobilePASS app has a token registered inside of it and synced with your employer called generically “My Token”
As this is rather a draft version nothing will block the user from interfering. As such, if you want it to work successfully then please do not click around or do any action while the script is running in order to not change the focus of the window where it is doing its magic.
Finding out elements in Apple scripts
- [Tutorial about finding element names and paths] (https://apple.stackexchange.com/questions/340942/applescript-how-can-i-get-ui-elements-names-attributes-properties-classe/340943)
- Use Accessibility Inspector and
- either the target icon of it selecting your UI element, then going to Advanced
- either select MobilePASS from the list of apps running and then again look in Advanced as you toy around
- Use Automator
- click record
- do your usual actions to connect to the VPN, get the token ID
- then from the list generated by Automator just drag into AppleScript editor
- result will be the code content plus some extras added by Automator (special function for delay when running a command would be such an example)
If you have a non-button element that you need to click (like the token inside MobilePASS) then you will notice that sometimes with just a click action in an Applescript it will work, sometimes not…annoying, right? The solution is to use “select” instead.
The script itself
You can export it from Applescript Editor as an App and then you just run it simply by double clicking :) the lazy way. DO NOT FORGET TO SET THE PIN, second row below.
delay 1
set pin to "mama3150"
tell application "MobilePASS"
activate
end tell
delay 1
repeat until application "MobilePASS" is running
delay 1
end repeat
tell application "System Events"
repeat until (window 1 of process "MobilePASS" exists)
delay 1
end repeat
end tell
tell application "System Events"
tell table 1 of scroll area 1 of window "MobilePASS" of application process "MobilePASS"
select row 1
end tell
keystroke pin
click button "Copy Passcode" of window "MobilePASS" of application process "MobilePASS"
click button "List" of window "MobilePASS" of application process "MobilePASS"
end tell
tell application "Cisco AnyConnect Secure Mobility Client"
activate
end tell
repeat until application "Cisco AnyConnect Secure Mobility Client" is running
delay 1
end repeat
tell application "System Events"
repeat until (window 1 of process "Cisco AnyConnect Secure Mobility Client" exists)
delay 1
end repeat
tell process "Cisco AnyConnect Secure Mobility Client"
keystroke return
end tell
repeat until (window 3 of process "Cisco AnyConnect Secure Mobility Client" exists)
delay 1
end repeat
tell process "Cisco AnyConnect Secure Mobility Client"
keystroke "v" using {command down}
delay 1
keystroke return
end tell
repeat until (window 3 of process "Cisco AnyConnect Secure Mobility Client" exists)
delay 1
end repeat
click button "Accept" of window "Cisco AnyConnect - Banner" of application process "Cisco AnyConnect Secure Mobility Client"
end tell