【问题标题】:Applescript, having trouble with if and subprocesses of this scriptApplescript,此脚本的 if 和子进程有问题
【发布时间】:2013-08-03 02:48:49
【问题描述】:

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

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

【问题讨论】:

    标签: macos applescript vpn kill


    【解决方案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
    

    【讨论】:

    • 哦,好吧。我看到了它是如何工作的以及一切。有什么理由我的方式是错误的(说到 syntaX)?
    • 另外,返回是否涵盖整个脚本。因为当我空闲运行它时,它在 vpn 断开连接时没有做任何事情。
    • 之所以分开代码是因为告诉终端执行“do shell script”语句效率低下。返回值告诉“空闲”处理程序多久执行一次。所以真的0秒没有意义。当您使用 0 时,applescript 将使用默认值 30 秒。您可能希望将其更改为更合理的内容。我不能保证你的代码,所以我不能说它为什么不起作用......我只是在帮助你解决关于 if 语句的问题......虽然也许是因为我刚刚的“30秒”的东西解释...也许你没有等待足够长的时间。
    • 关于您的原始语法,这是错误的。您会希望所有“if application”代码都在“if current configuration”语句中。因此,您需要将该 if 语句的“do shell script”部分放在下一行,并在最后一个“if application”行之后放置一个“end if”。
    • 我的意思是说...分离代码的原因是因为告诉系统事件执行“执行 shell 脚本”语句效率低下。 “do shell script”命令是一个applescript命令,而不是系统事件命令,所以没有理由将该代码放在系统事件告诉代码块中。 1)它效率低下,2)它可能会导致一些意外错误,因为系统事件不知道该命令。
    猜你喜欢
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多