PDA

View Full Version : One last different AppleScript issue




ring
Aug 1, 2013, 07:42 PM
Basically, I wrote a script to kill the network (wifi as of now, will try and figure out ethernet in a bit) when the vpn disconnects. I wanted to add a failsafe that also killed applications, but when I added this bit of code

if application "iTunes" is running then do shell script "killall iTunes"
if application "uTorrent" is running then do shell script "killall uTorrent"
if application "Transmission" is running then do shell script "killall Transmission"
if application "Safari" is running then do shell script "killall Safari"
if application "Google Chrome" is running then do shell script "killall 'Google Chrome'"
if application "Palringo" is running then do shell script "killall Palringo"


to the script, I kept having trouble with running it. I'm honestly not sure how the ifs are supposed to be used in this situation.




I want it to do the following


- if myConnection is not null and
- if vpn is not connected

- kill wifi
- and also do the following "if statements":

if application "iTunes" is running then do shell script "killall iTunes"
if application "uTorrent" is running then do shell script "killall uTorrent"
if application "Transmission" is running then do shell script "killall Transmission"
if application "Safari" is running then do shell script "killall Safari"
if application "Google Chrome" is running then do shell script "killall 'Google Chrome'"
if application "Palringo" is running then do shell script "killall Palring**b**"


but I'm not really sure how to do that / everything I have done has failed. This ****is my code.




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 do shell script "/usr/sbin/networksetup -setairportpower en1 off"



if application "iTunes" is running then do shell script "killall iTunes"
if application "uTorrent" is running then do shell script "killall uTorrent"
if application "Transmission" is running then do shell script "killall Transmission"
if application "Safari" is running then do shell script "killall Safari"
if application "Google Chrome" is running then do shell script "killall 'Google Chrome'"
if application "Palringo" is running then do shell script "killall Palringo"
end if
end tell
return 0
end tell
end idle



That's what's failing. Everything I have tried has something wrong. And correction/guidance/advice/help would be GREATLY appreciated. I either get an error that is complaining about the way I phrase the if statement, or I get an error that is saying error "iTunes got an error: Can’t get running." number -1728 from «class runn»

but when I enter the following into a blank script, without the rest, they work fine and don't give that error

if application "iTunes" is running then do shell script "killall iTunes"
if application "uTorrent" is running then do shell script "killall uTorrent"
if application "Transmission" is running then do shell script "killall Transmission"
if application "Safari" is running then do shell script "killall Safari"
if application "Google Chrome" is running then do shell script "killall 'Google Chrome'"
if application "Palringo" is running then do shell script "killall Palringo"



chown33
Aug 1, 2013, 11:14 PM
The simplest solution is to remove all the 'if xx is running then' clauses, making the 'do shell script "xxx"' clauses unconditional.

The killall command does nothing if there's no program running with the name given to it. So if uTorrent isn't running, it's harmless to run "killall uTorrent".

I suggest reading the man page to killall. You can pass it multiple names, so it can kill a whole bunch of programs with one "do shell script".

see.solve.
Aug 6, 2013, 08:53 PM
Maybe the following might work?

on idle
set listAppNames to {"iTunes", "uTorrent", "Transmission", "Safari", "Google Chrome", "Palringo"}

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 do shell script "/usr/sbin/networksetup -setairportpower en1 off"
end if
end tell

set listApplicationProcessNames to name of every application process

repeat with textAppName in listAppNames
if listApplicationProcessNames contains textAppName then
do shell script "killall" & space & quoted form of textAppName
end if
end repeat
end tell
end idle

I think what might have been throwing the error was that you had your killall if statements enclosed in the 'tell current location of network preferences' tell block, and the current location of network preferences didn't quite know how to process the 'if iTunes is running' command.

dcnblues
Aug 7, 2013, 09:20 PM
Basically, I wrote a script to kill the network (wifi as of now, will try and figure out ethernet in a bit) when the vpn disconnects. I wanted to add a failsafe that also killed applications,...[/B]

I want that too. I hope you get it working, and share it. Thanks for the effort...:)