Skip to content

Instantly share code, notes, and snippets.

@prasanthj
Last active April 8, 2024 21:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasanthj/ee380ca860306cb9bf11d8bf5f57ce46 to your computer and use it in GitHub Desktop.
Save prasanthj/ee380ca860306cb9bf11d8bf5f57ce46 to your computer and use it in GitHub Desktop.
Hammerspoon script for VPN auto-connect in OS X El Capitan
-- hot reload init.lua script
function reloadConfig(files)
doReload = false
for _,file in pairs(files) do
if file:sub(-4) == ".lua" then
doReload = true
end
end
if doReload then
hs.reload()
end
end
hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert.show("Config loaded")
-- run apple script to connect to VPN
hs.hotkey.bind({"ctrl", "cmd"}, "V", function()
ok,result,desc = hs.osascript._osascript([[
set vpn_name to "'VPN_NAME'"
tell application "System Events"
set rc to do shell script "scutil --nc status " & vpn_name
if rc starts with "Connected" then
do shell script "scutil --nc stop " & vpn_name
display notification "VPN " & vpn_name & " disconnected!"
else
-- get current clipboard contents as a string
set CurrentClipboard to the clipboard as string
-- set the clipboad to your password
set the clipboard to "VPN_PASSWORD"
do shell script "scutil --nc start " & vpn_name
delay 2
-- paste clipboard contents into password box
keystroke "v" using {command down}
-- press "Enter"
keystroke return
-- wait 2 seconds to connect
delay 2
-- now reset the clipboard to what it was before we started
set the clipboard to CurrentClipboard
display notification "VPN " & vpn_name & " connected!"
end if
end tell
]], "AppleScript")
-- print(tostring(desc))
-- hs.alert.show(tostring(ok))
-- hs.alert.show(tostring(result))
end)
@prasanthj
Copy link
Author

Replace VPN_NAME and VPN_PASSWORD with corresponding name and password for the vpn (Note: username is assumed to be saved during initial VPN setup).

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