简体   繁体   中英

Applescript, having trouble with if and subprocesses of this script

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 Palringo"

b* ut 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 would move the "do shell script" lines outside of the "system events" tell block of code. Something like this would work...

on idle
    set vpnIsDisconnected to false
    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 set vpnIsDisconnected to true
            end if
        end tell
    end tell

    if vpnIsDisconnected 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

    return 0
end idle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM