Last active
April 8, 2024 21:14
-
-
Save prasanthj/ee380ca860306cb9bf11d8bf5f57ce46 to your computer and use it in GitHub Desktop.
Hammerspoon script for VPN auto-connect in OS X El Capitan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).