Created
March 27, 2019 13:14
-
-
Save lantrix/19dfd700214bf46af7e0aeb64adac391 to your computer and use it in GitHub Desktop.
Tunnelblick Applescript automation for launching a random VPN config and then services and apps - Notify with Growl
This file contains hidden or 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
tell application "System Events" | |
set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0 | |
end tell | |
if isRunning then | |
tell application id "com.Growl.GrowlHelperApp" | |
-- Make a list of all the notification types | |
-- that this script will ever send: | |
set the allNotificationsList to ¬ | |
{"VPN Connecting", "VPN Down", "VPN Connected"} | |
-- Make a list of the notifications | |
-- that will be enabled by default. | |
-- Those not enabled by default can be enabled later | |
-- in the 'Applications' tab of the Growl preferences. | |
set the enabledNotificationsList to ¬ | |
{"VPN Connecting", "VPN Down", "VPN Connected"} | |
-- Register our script with growl. | |
-- You can optionally (as here) set a default icon | |
-- for this script's notifications. | |
register as application ¬ | |
"MyVPN" all notifications allNotificationsList ¬ | |
default notifications enabledNotificationsList ¬ | |
icon of application "TunnelBlick" | |
end tell | |
end if | |
tell application "System Events" | |
tell application "Tunnelblick" | |
-- Check to see if already connected | |
set foundExistingConnection to false | |
set vpnconfigs to get name of configurations | |
display dialog "Number of configs: " & (count of vpnconfigs) | |
repeat with i from 1 to number of items in vpnconfigs | |
set thisVpnConfig to (item i of vpnconfigs) | |
set existingStatus to get (state of first configuration where name is equal to thisVpnConfig) | |
if existingStatus is equal to "CONNECTED" then | |
display dialog thisVpnConfig & "already CONNECTED" | |
set foundExistingConnection to true | |
-- Launch an App | |
tell application "myMacApp.app" to launch | |
-- Launch a Service | |
do shell script "launchctl load ~/Library/LaunchAgents/myservice.plist" | |
do shell script "launchctl start MyService" | |
-- Notify Found Existing Connection | |
tell application "Growl" | |
notify with name ¬ | |
"VPN Connected" title ¬ | |
"VPN Connected" description ¬ | |
"I have found existing VPN and opened all Apps" application name "MyVPN" | |
end tell | |
end if | |
end repeat | |
if (foundExistingConnection = false) then | |
-- Get all VPN configs | |
set vpnconfigs to get name of configurations | |
set randomItemNum to random number from 1 to count of vpnconfigs | |
set thisConfigName to get name of configuration randomItemNum | |
-- Notify Connecting | |
tell application "Growl" | |
notify with name ¬ | |
"VPN Connecting" title ¬ | |
"VPN Connecting" description ¬ | |
"Tunnelblick VPN is connecting to " & thisConfigName application name "MyVPN" | |
end tell | |
-- Connect VPN | |
connect thisConfigName | |
-- Wait for connection to come up | |
set pendingConnection to true | |
repeat while (pendingConnection is equal to true) | |
set status to get state of first configuration where name is equal to thisConfigName | |
if status is equal to "CONNECTED" then | |
set pendingConnection to false | |
end if | |
if status is equal to "EXITING" then | |
display dialog thisConfigName & "failed to connect" | |
-- Notify Connecting | |
tell application "Growl" | |
notify with name ¬ | |
"VPN Failed" title ¬ | |
"VPN Failed" description ¬ | |
"Tunnelblick VPN failed to connect" application name "MyVPN" | |
end tell | |
quit | |
end if | |
-- DEBUG: display dialog thisConfigName & ": " & status | |
delay 1 | |
end repeat | |
-- Launch an App | |
tell application "myMacApp.app" to launch | |
-- Launch a Service | |
do shell script "launchctl load ~/Library/LaunchAgents/myservice.plist" | |
do shell script "launchctl start MyService" | |
-- Notify UP | |
tell application "Growl" | |
notify with name ¬ | |
"VPN Connected" title ¬ | |
"VPN Connected" description ¬ | |
"I have connected VPN and opened all Apps" application name "MyVPN" | |
end tell | |
end if | |
end tell | |
-- Check if myMacApp is running | |
repeat | |
tell application "System Events" | |
set mymacappIsRunning to (count of (every process whose name is "myMacApp")) > 0 | |
delay 1 | |
end tell | |
-- Check if VPN is down | |
tell application "Tunnelblick" | |
set status to get state of first configuration where name is equal to thisConfigName | |
if status is not equal to "CONNECTED" then | |
if (mymacappIsRunning = true) then | |
-- Quit an App | |
tell application "myMacApp.app" to quit | |
-- Stop a Service | |
do shell script "launchctl stop MyService" | |
do shell script "launchctl unload ~/Library/LaunchAgents/myservice.plist" | |
say "WARNING VPN DISCONNECTED " using "VICTORIA" | |
-- Notify Down | |
tell application "Growl" | |
notify with name ¬ | |
"VPN Down" title ¬ | |
"VPN Down" description ¬ | |
"I have quit all Apps" application name "MyVPN" | |
end tell | |
display dialog "VPN disconnected" | |
delay 5 | |
exit repeat | |
else | |
set errorCount to 1 | |
if (errorCount > 1) then | |
-- Quit an App | |
tell application "myMacApp.app" to quit | |
-- Stop a Service | |
do shell script "launchctl stop MyService" | |
do shell script "launchctl unload ~/Library/LaunchAgents/myservice.plist" | |
-- Notify Down | |
tell application "Growl" | |
notify with name ¬ | |
"VPN Down" title ¬ | |
"VPN Down" description ¬ | |
"no further action" application name "MyVPN" | |
end tell | |
display dialog "VPN disconnected" | |
delay 5 | |
exit repeat | |
end if | |
end if | |
end if | |
end tell | |
end repeat | |
end tell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment