Applescript,遇到此脚本的if和subprocesss问题

时间:2013-08-01 23:58:22

标签: macos applescript vpn kill

基本上,当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

这就是失败的原因。我尝试的一切都有些不对劲。并且非常感谢校正/指导/建议/帮助。

1 个答案:

答案 0 :(得分:1)

我会在“系统事件”之外移动“do shell script”行告诉代码块。像这样的东西会起作用......

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