Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Created July 3, 2010 19:05
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save iloveitaly/462760 to your computer and use it in GitHub Desktop.
Save iloveitaly/462760 to your computer and use it in GitHub Desktop.
Configure / create VPN connections via applescript
-- Author: Michael Bianco <http://mabblog.com/>
-- Some help from: http://discussions.info.apple.com/message.jspa?messageID=10363317
on create_vpn_service(vpn_name)
tell application "System Preferences"
reveal pane "Network"
activate
tell application "System Events"
tell process "System Preferences"
tell window 1
click button "Add Service"
tell sheet 1
-- set location type
click pop up button 1
click menu item "VPN" of menu 1 of pop up button 1
delay 1
-- set connection type
click pop up button 2
click menu item "PPTP" of menu 1 of pop up button 2
delay 1
-- set name of the service
-- for some reason the standard 'set value of text field 1' would not work
set value of attribute "AXValue" of text field 1 to vpn_name
click button "Create"
end tell
click button "Apply"
end tell
end tell
end tell
quit
end tell
delay 2
end create_vpn_service
on update_vpn_settings(vpn_name, vpn_address, vpn_username, vpn_password)
tell application "System Preferences"
reveal pane "Network"
activate
tell application "System Events"
tell process "System Preferences"
tell window 1
-- select the specified row in the service list
repeat with r in rows of table 1 of scroll area 1
if (value of attribute "AXValue" of static text 1 of r as string) contains vpn_name then
select r
end if
end repeat
-- set the address & username / account name
-- note that this is vpn specific
tell group 1
set focused of text field 1 to true
keystroke " "
set value of text field 1 to vpn_address
set value of text field 2 to vpn_username
click button "Authentication Settings…"
end tell
-- open up the auth panel and set the login password
tell sheet 1
set focused of text field 1 to true
set value of text field 1 to vpn_password
click button "Ok"
end tell
click button "Apply"
end tell
end tell
end tell
quit
end tell
end update_vpn_settings
on vpn_exists(vpn_name)
tell application "System Events"
try
service vpn_name of network preferences
return true
on error
return false
end try
end tell
end vpn_exists
on vpn_status(vpn_name)
tell application "System Events"
return connected of configuration of service vpn_name of network preferences
end tell
end vpn_status
on toggle_vpn_status(vpn_name)
try
tell application "System Events"
set s to service vpn_name of network preferences
if connected of configuration of s then
disconnect s
else
connect s
end if
end tell
on error errorMessage
return "Error: error toggling vpn connection" & errorMessage
end try
end toggle_vpn_status
@kebot
Copy link

kebot commented May 4, 2013

Just fork this script and have some modify:
https://gist.github.com/kebot/5517680

Fix the problem when not using English as the default language.(for example the button "Apply" will be "应用" in Chinese, so I change it to number 7)

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