Skip to content

Instantly share code, notes, and snippets.

@brozikcz
Last active April 12, 2024 11:36
Show Gist options
  • Save brozikcz/7c08366243ab953a4e883826d616b5d1 to your computer and use it in GitHub Desktop.
Save brozikcz/7c08366243ab953a4e883826d616b5d1 to your computer and use it in GitHub Desktop.
It's the apple script for the Cisco Secure Client app with the M$ SSO + authenticator
global ciscoClientMainWindowName
global ciscoClientBannerWindowName
global ciscoWindowTitle
global keychainItemName
global userPassword
set userLoginName to "user"
#set userPassword to "pass" #use the keychain item, the item and account name are the same `cisco_vpn`
set keychainItemName to "cisco_vpn"
set ciscoClientMainWindowName to "Cisco Secure Client"
set ciscoWindowTitle to "Cisco Secure Client - Login"
set ciscoClientBannerWindowName to "Cisco Secure Client - Banner" #be aware of size of dash
set formUser to "Sign in"
set formPassword to "Enter password"
set formSuccess to "Stay signed in"
set lang to user locale of (get system info)
considering case
if lang contains "cs_CZ" then
set ciscoWindowTitle to "Cisco Secure Client – přihlášení"
set ciscoClientBannerWindowName to "Cisco Secure Client – banner"
set formUser to "Přihlásit se"
set formPassword to "Zadat heslo"
set formSuccess to "Zůstat přihlášen"
end if
if lang contains "sk_SK" then #TODO Martas dopln :D
set ciscoWindowTitle to "Cisco Secure Client – přihlášení"
set formUser to "Přihlásit se"
set formPassword to "Zadat heslo"
set formSuccess to "Zůstat přihlášen"
end if
end considering
on getPassword()
set isPasswordSet to true
try
userPassword
on error message
set isPasswordSet to false
end try
if isPasswordSet then
return userPassword
end if
do shell script "security 2>&1 >/dev/null find-generic-password -gl " & quoted form of keychainItemName & " | awk '{print $2}'"
if result is not "SecKeychainSearchCopyNext:" then
return (text 2 thru -2 of result)
else
display alert ("No " & keychainItemName & " keychain found!") ¬
message ("Press OK to set up a " & keychainItemName & " keychain, or fill up the `userPassword` field.") as critical ¬
buttons {"Cancel", "OK"} ¬
default button ("OK") ¬
cancel button "Cancel"
end if
return missing value
end getPassword
on waitForTextInWindow(searchText)
set startTime to (get current date)
tell application "System Events"
tell process ciscoClientMainWindowName
tell window ciscoWindowTitle
set duration to (get current date) - startTime
repeat until duration > 30
set duration to (get current date) - startTime
try
set uiElem to ((value of static text of UI element of group 2 of UI element 1 of scroll area 1 of group 1 of group 1) as string)
#log uiElem
#log (uiElem contains searchText)
if (uiElem contains searchText) then
exit repeat
end if
end try
delay 0.5
end repeat
if duration > 30 then
display alert ("The title " & searchText & " not found!") ¬
message ("The script was canceled") as critical ¬
buttons {"OK"} ¬
default button ("OK")
error number -128
end if
end tell
end tell
end tell
end waitForTextInWindow
on waitForWindow(windowName)
tell application "System Events"
set startTime to (get current date)
tell process ciscoClientMainWindowName
repeat until window windowName exists
delay 0.1
set duration to (get current date) - startTime
if duration > 30 then
display alert ("The window " & windowName & " not found!") ¬
message ("The script was canceled") as critical ¬
buttons {"OK"} ¬
default button ("OK")
error number -128
end if
end repeat
end tell
end tell
end waitForWindow
activate application ciscoClientMainWindowName
tell application "System Events"
tell process ciscoClientMainWindowName
tell menu bar item 1 of menu bar 2 # show the window app
click menu item 3 of menu 1
end tell
my waitForWindow(ciscoClientMainWindowName)
tell process "Cisco Secure Client"
keystroke return
end tell
my waitForWindow(ciscoWindowTitle)
my waitForTextInWindow(formUser)
tell window ciscoWindowTitle
tell text field 1
keystroke userLoginName
key code 36
end tell
end tell
my waitForTextInWindow(formPassword)
tell window ciscoWindowTitle
tell text field 1
keystroke my getPassword()
end tell
key code 36
end tell
my waitForTextInWindow(formSuccess)
tell window ciscoWindowTitle
key code 36
end tell
set allWindows to ""
set startTime to (get current date)
repeat until allWindows contains ciscoClientBannerWindowName
set duration to (get current date) - startTime
if duration > 30 then
display alert ("The window " & ciscoClientBannerWindowName & " not found!") ¬
message ("The script was canceled") as critical ¬
buttons {"OK"} ¬
default button ("OK")
error number -128
end if
delay 0.1
set allWindows to (get name of every window) as string
end repeat
delay 0.5
tell window ciscoWindowTitle
key code 36
end tell
end tell
end tell
@brozikcz
Copy link
Author

brozikcz commented Apr 12, 2024

How to use it?

You have two options:

  • Turn it into an application
  • Keep it as a script

Let's start with the first option: TURN IT INTO AN APPLICATION

  1. Open the script source code file Cisco-secure-client.scpt with Script Editor (which is the default program used).
  2. On the top menu, select File, then Export...
  3. In the pop-up window, make sure to select Application for File Format:, and Sign to Run Locally for Code Sign:
  4. (BONUS) You want to use a different icon from the stock one? Follow the steps below to extract the icon from another app and use it:

Once you have your application compiled and ready:

  1. Open Security & Privacy System Preferences, go to Privacy, then Accessibility.
  2. Enable the application you just created to access Accessibility
  3. (BONUS) Add the application to /Users/[yourhostname]/Applications and then to the Dock
  4. Run the application again to close the connection

Now for the second option: KEEP IT AS A SCRIPT

  1. Open the script source code file Cisco-secure-client.scpt with Script Editor (which is the default program used).
  2. On the top menu, select Script Editor, then click Preferences
  3. In the pop-up window, make sure to select Show Script menu in menu bar for Script Menu:
  4. Save the script in your user's Scripts folder (/Users/[yourhostname]/Library/Scripts)

Once you have saved the script inside your user's script folder:

  1. Open Security & Privacy System Preferences, go to Privacy, then Accessibility.
  2. Enable Script Menu
  3. (OPTIONAL) Enable Script Editor. This is required if you want to run the script from the Script Editor.
  4. Select the scroll looking icon in the top menu and click on the script you saved.

credits for README: https://github.com/communikein/anyconnect_autoconnect/blob/master/README.md

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