1

基本上,我编写了一个脚本来在 vpn 断开连接时终止网络(截至目前,wifi 将尝试找出以太网)。我想添加一个也杀死应用程序的故障保护,但是当我添加这段代码时

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"

对于脚本,我一直无法运行它。老实说,我不确定在这种情况下应该如何使用 ifs。

我希望它执行以下操作

- 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 我不太确定该怎么做/我所做的一切都失败了。这是我的代码。*

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

这就是失败的原因。我所尝试的一切都有问题。更正/指导/建议/帮助将不胜感激。

4

1 回答 1

1

我会将“执行 shell 脚本”行移到“系统事件”告诉代码块之外。像这样的东西会起作用......

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
于 2013-08-02T01:27:18.863 回答