PDA

View Full Version : Applescript help




ring
Aug 1, 2013, 08:40 AM
Hi. I'm trying to get a script working that basically disconnects my internet if the vpn goes out. I found a script on lifehacker that will automatically reconnect the vpn if it fails, and I took that and modified it into this.



But I noticed that it didnt work properly (nothing happened when I manually disconnected it.

on idle

tell application "System Events"

tell current location of network preferences

set myConnection to the service "BTGuard VPN"

set failsafe to the service "Wi-Fi"

if myConnection is not null then

if current configuration of myConnection is not connected then

disconnect failsafe

end if

end if

end tell

return 120

end tell



So I looked up a way to turn wifi off in apple script, and I attempted to manipulate that bit of code into the original script. I resulted with this bit of code, which also failed to work properly.

on idle
tell application "System Events"
tell current location of network preferences
set myConnection to the service "BTGuard VPN"
set status to do shell script "/usr/sbin/networksetup -getairportpower en1 | awk '{ print $4 }'"
if status = "On" then
do shell script "/usr/sbin/networksetup -setairportpower en1 off"
if myConnection is not null then
if current configuration of myConnection is not connected and status = "On" then do shell script "/usr/sbin/networksetup -setairportpower en1 off"
end if
end if
end tell
return 120
end tell
end idle



Is there a way to fix this? I'm relativley new to applescript (I used it to write an autotyper one time), so any help would be appreciated. The objective is to make it run as an always running .app (like the original lifehacker), and to have it instantly pull the plug on the internet the second the VPN drops. I will bake cookies for anyone who is willing to help/correct my thought process.


Love,
Ring :apple:



chown33
Aug 1, 2013, 11:40 AM
First, simply saying "it doesn't work" or "it failed to work properly" is too vague to diagnose. You need to apply some basic debugging principles, namely Break It Down.

Does the first do shell script run correctly? What does it return in 'status'? Be specific. Log what's in status. Post the actual output.

Does the second do shell script run correctly? Same questions: what does it return; log it; post the actual log output.


Second, what have you looked at as reference docs? Be specific. If online, post urls. If you used the 'man' command, say which man pages you read.

For example, from the online reference for networksetup:
https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man8/networksetup.8.html
The networksetup command is used to configure network settings typically configured in the System Preferences application. The networksetup command requires at least "admin" privileges to run. Most of the set commands require "root" privileges to run.
This is the same as what 'man networksetup' would show you, so either you haven't read the man page, or you missed the important detail as underlined.


Third, refer to the reference doc for the do shell script command, and look at the option for running with admin privileges. If that won't work for you, because it requires user interaction, then you need to enter your admin password into the script as well.


http://www.mikeash.com/getting_answers.html

ring
Aug 1, 2013, 01:05 PM
First, simply saying "it doesn't work" or "it failed to work properly" is too vague to diagnose. You need to apply some basic debugging principles, namely Break It Down.

Does the first do shell script run correctly? What does it return in 'status'? Be specific. Log what's in status. Post the actual output.

Does the second do shell script run correctly? Same questions: what does it return; log it; post the actual log output.


Second, what have you looked at as reference docs? Be specific. If online, post urls. If you used the 'man' command, say which man pages you read.

For example, from the online reference for networksetup:
https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man8/networksetup.8.html
The networksetup command is used to configure network settings typically configured in the System Preferences application. The networksetup command requires at least "admin" privileges to run. Most of the set commands require "root" privileges to run.
This is the same as what 'man networksetup' would show you, so either you haven't read the man page, or you missed the important detail as underlined.


Third, refer to the reference doc for the do shell script command, and look at the option for running with admin privileges. If that won't work for you, because it requires user interaction, then you need to enter your admin password into the script as well.


http://www.mikeash.com/getting_answers.html



Sorry about that. When I was messing with it, and I pressed "run", nothing came as the output. Nothing happened at all actually. The way I was testing it is I would make changes -> run app -> disconnect vpn -> wait for wifi to be killed or other changes to occur.


I guess I'm really asking is, how can I tell applescript to disconnect to wifi when the vpn connection fails. I'm not familiar with the language apart from what I picked up.


I assume, from looking at the docs I'm going to use getairportpower, but when I try that command in the Terminal.app, I get the result

networksetup -setairportpower airport off
Turning off the only airport interface found: en1


I have the below script (I am using this now) to automatically reconnect when BTGuard disconnects. Instead of disconnecting, I want it to cut off the internet completely when that happens.

This:

on idle
tell application "System Events"
tell current location of network preferences
set myConnection to the service "BTGuard VPN"
if myConnection is not null then
if current configuration of myConnection is not connected then
connect myConnection
end if
end if
end tell
return 1.0E-19
end tell
end idle




I really appreciate your time to reply and want to thank you and everybody for your time/help. It's greatly appreciated, and I apologize for my lack of understanding (this is the first real time I needed to do a serious task in AppleScript!



:apple:

kryten2
Aug 1, 2013, 02:27 PM
Sorry about that. When I was messing with it, and I pressed "run", nothing came as the output. Nothing happened at all actually. The way I was testing it is I would make changes -> run app -> disconnect vpn -> wait for wifi to be killed or other changes to occur.

For testing comment out the on idle handler and return statement from the script and then run your script.
eg

--on idle
tell application "System Events"
tell current location of network preferences
set myConnection to the service "BTGuard VPN"
set status to do shell script "/usr/sbin/networksetup -getairportpower en1 | awk '{ print $4 }'"
if status = "On" then
do shell script "/usr/sbin/networksetup -setairportpower en1 off"
if myConnection is not null then
if current configuration of myConnection is not connected and status = "On" then do shell script "/usr/sbin/networksetup -setairportpower en1 off"
end if
end if
end tell
-- return 120
end tell
--end idle

ring
Aug 1, 2013, 03:34 PM
For testing comment out the on idle handler and return statement from the script and then run your script.
eg

--on idle
tell application "System Events"
tell current location of network preferences
set myConnection to the service "BTGuard VPN"
set status to do shell script "/usr/sbin/networksetup -getairportpower en1 | awk '{ print $4 }'"
if status = "On" then
do shell script "/usr/sbin/networksetup -setairportpower en1 off"
if myConnection is not null then
if current configuration of myConnection is not connected and status = "On" then do shell script "/usr/sbin/networksetup -setairportpower en1 off"
end if
end if
end tell
-- return 120
end tell
--end idle



Thank you very much for your help! I figured everything out (not sure exactly what didn't happen properly, but it is working now.)

ring
Aug 1, 2013, 05:42 PM
for those interested the code is


on idle
tell application "System Events"
tell current location of network preferences
set myConnection to the service "BTGuard VPN"
set status to do shell script "/usr/sbin/networksetup -getairportpower en1 | awk '{ print $4 }'"
if status = "On" then
do shell script "/usr/sbin/networksetup -setairportpower en1 off"
if myConnection is not null then
if current configuration of myConnection is not connected and status = "On" then do shell script "/usr/sbin/networksetup -setairportpower en1 off"
end if
end if
end tell
-- return .000000000000000000000000000000000001
end tell
end idle