Skip to content

Instantly share code, notes, and snippets.

@bloatfan
Forked from iloveitaly/vpn-utilities.applescript
Created September 12, 2021 08:52
Show Gist options
  • Save bloatfan/2d4b9559898194ec58c54e9df6e4eac4 to your computer and use it in GitHub Desktop.
Save bloatfan/2d4b9559898194ec58c54e9df6e4eac4 to your computer and use it in GitHub Desktop.

Revisions

  1. @iloveitaly iloveitaly revised this gist Jul 7, 2010. 1 changed file with 26 additions and 13 deletions.
    39 changes: 26 additions & 13 deletions vpn-utilities.applescript
    Original file line number Diff line number Diff line change
    @@ -23,14 +23,19 @@ on create_vpn_service(vpn_name)

    -- 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 service_name
    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)
    @@ -51,13 +56,17 @@ on update_vpn_settings(vpn_name, vpn_address, vpn_username, vpn_password)
    -- 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
    @@ -66,6 +75,8 @@ on update_vpn_settings(vpn_name, vpn_address, vpn_username, vpn_password)
    end tell
    end tell
    end tell

    quit
    end tell
    end update_vpn_settings

    @@ -82,20 +93,22 @@ end vpn_exists

    on vpn_status(vpn_name)
    tell application "System Events"
    return connected of configuration of service vpn_name of network preferences as boolean
    return connected of configuration of service vpn_name of network preferences
    end tell
    end vpn_status

    on toggle_vpn_status(vpn_name)
    -- note that this function assumes that the vpn exists
    tell application "System Events"
    -- if kind of service = 14 then vpn

    set s to service vpn_name of network preferences
    if not connected of configuration of s as boolean then
    tell s to connect
    else
    tell s to disconnect
    end if
    end tell
    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
  2. @iloveitaly iloveitaly created this gist Jul 3, 2010.
    101 changes: 101 additions & 0 deletions vpn-utilities.applescript
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    -- 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 service_name
    click button "Create"
    end tell
    click button "Apply"
    end tell
    end tell
    end tell
    end tell
    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 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 value of text field 1 to vpn_password
    click button "Ok"
    end tell

    click button "Apply"
    end tell
    end tell
    end tell
    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 as boolean
    end tell
    end vpn_status

    on toggle_vpn_status(vpn_name)
    -- note that this function assumes that the vpn exists
    tell application "System Events"
    -- if kind of service = 14 then vpn

    set s to service vpn_name of network preferences
    if not connected of configuration of s as boolean then
    tell s to connect
    else
    tell s to disconnect
    end if
    end tell
    end toggle_vpn_status