Skip to content

Instantly share code, notes, and snippets.

@jacops
Created January 1, 2021 11:38
Show Gist options
  • Save jacops/f8516d88bda6078d211f61c056862222 to your computer and use it in GitHub Desktop.
Save jacops/f8516d88bda6078d211f61c056862222 to your computer and use it in GitHub Desktop.
Apple script for connecting to desired IP of Tunnel Bear. It will automatically diconnect and connect until it gets aproperiate IP.
#!/usr/bin/env osascript
set validIPPart to "127.0"
set T1 to minutes of (current date)
set T1s to seconds of (current date)
on logMsg(msg)
log "[" & (current date) & "] - " & msg
end logMsg
on VPNConnect()
tell application "TunnelBear"
activate
tell application "System Events"
get name of every process whose name is "TunnelBear"
tell process "TunnelBear"
set mainWindow to (window "Window")
tell mainWindow
set toggleConnectionElement to (UI element 3)
set isConnected to false
if (value of static text 0) is "Connection Secured" then
set isConnected to true
my logMsg("Disconnecting...")
click toggleConnectionElement
repeat until isConnected = false
if (value of static text 0) is "Connection Unsecured" then
set isConnected to false
end if
delay 0.1
end repeat
delay 1
end if
click toggleConnectionElement
my logMsg("Connecting...")
repeat until isConnected = true
if (value of static text 0) is "Connection Secured" then
set isConnected to true
end if
delay 0.1
end repeat
end tell
end tell
end tell
end tell
end vpnConnect
set isConnectionValid to false
repeat until isConnectionValid = true
VPNConnect()
set myIP to do shell script "curl 'https://ifconfig.co'"
if myIP starts with validIPPart then
set isConnectionValid to true
end if
logMsg("Connected to: " & myIP & ".")
end repeat
set T2 to minutes of (current date)
set T2s to seconds of (current date)
set TT_ to ((T2 * 60) + T2s) - ((T1 * 60) + T1s)
logMsg("Seconds elapsed: " & TT_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment