Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zored
Last active August 30, 2022 12:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zored/4a4b4bab855e29167abd3db9fa475bba to your computer and use it in GitHub Desktop.
Save zored/4a4b4bab855e29167abd3db9fa475bba to your computer and use it in GitHub Desktop.
Toggle TunnelBear from macOS console. Enable or disable TunnelBear VPN with CLI. Compatable with TunnelBear 3.8.0. Based on SH and AppleScript.
#/usr/bin/env bash
# Include this file in `~/.bash_profile` with `source /path/to/tunnelbear-toggle.sh`.
# Toggle TunnelBear activity.
#
# Example usage:
# `tunnelbear-toggle`
# `tunnelbear-toggle 1`
tunnelbear-toggle ()
{
local enable=${1:-''} expectedState='';
case ${enable} in
'1')
expectedState="Disconnected"
;;
'0')
expectedState="Connected"
;;
'')
expectedState=''
;;
*)
echo 'Enable argument must be empty / 0 / 1.' &> 2;
return 1
;;
esac;
osascript <<AS
set expectedState to "$expectedState"
tell application "System Events"
tell process "TunnelBear"
set toggleAppButton to (menu bar item 1 of menu bar 2)
click toggleAppButton
set mainWindow to (window "Window")
tell mainWindow
set toggleConnectionElement to (UI element 2)
set state to (value of static text 0)
if ((expectedState is "") or (state is expectedState)) then
click toggleConnectionElement
click toggleAppButton
else
log "Expected state is: '" & expectedState & "', but got: '" & state & "'."
click toggleAppButton
error number 1
end if
end tell
end tell
end tell
AS
}
@fbjorn
Copy link

fbjorn commented Jun 27, 2019

Mistyped "tunnelblick" with "tunnelbear" and ended up here 😆
If anyone is looking for tunnelBLICK solution then it's just easy (in case of 1 VPN configuration at least):

tell application "Tunnelblick"
	try
		set confName to get name of first configuration whose state = "EXITING"
		connect confName
		return "VPN on"
	on error
		disconnect all
		return "VPN off"
	end try
end tell

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