Skip to content

Instantly share code, notes, and snippets.

@christophervigliotti
Last active April 20, 2018 13:45
Show Gist options
  • Save christophervigliotti/6f0ead278210dd1aaf6f to your computer and use it in GitHub Desktop.
Save christophervigliotti/6f0ead278210dd1aaf6f to your computer and use it in GitHub Desktop.
An AppleScript whammy that ensures that application appName is only running when vpn vpnName is connected
------------------------------------------------
--Main Routine
------------------------------------------------
on idle
--Script Variables
set appName to "appName"
set vpnName to "vpnName"
set waitTIme to 0 as integer
--Main Script Logic
if isVPNConnected(vpnName) then
startApplication(appName)
else
stopApplication(appName)
connectVPNConnection(vpnName)
end if
return waitTIme
end idle
------------------------------------------------
--Sub Routine - Determines if specified vpn is connected
------------------------------------------------
on isVPNConnected(vpnName)
--Init return value to default
set isConnected to false
tell application "System Events"
tell current location of network preferences
set vpnConnection to the service vpnName
set isConnected to current configuration of vpnConnection is connected
end tell
end tell
return isConnected
end isVPNConnected
------------------------------------------------
--Sub Routine - Attempts to connect to the specified VPN
------------------------------------------------
on connectVPNConnection(vpnName)
tell application "System Events"
tell current location of network preferences
set vpnConnection to the service vpnName
if vpnConnection is not null then
connect vpnConnection
end if
end tell
end tell
end connectVPNConnection
------------------------------------------------
--Sub Routine - Starts an application if it is not already running
------------------------------------------------
on startApplication(appName)
if appIsRunning(appName) is false then
tell application appName to activate
end if
end startApplication
------------------------------------------------
--Sub Routine - Stop an application if it is running
------------------------------------------------
on stopApplication(appName)
if appIsRunning(appName) then
tell application appName to quit
end if
end stopApplication
------------------------------------------------
--Sub Routine - Determines if specified app is currently running
------------------------------------------------
on appIsRunning(appName)
set isRunning to false
tell application "System Events"
set isRunning to (name of processes) contains appName
end tell
return isRunning
end appIsRunning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment