加速AppleScript UI脚本?

SBN*_*SBN 2 applescript

我正在使用NetShade作为代理服务,并认为我可以尝试自动化不同代理之间的切换,这是我的第一个AppleScript脚本的良好开端.

NetShade-app没有AppleScript支持,所以我必须使用UI脚本.经过几次尝试(以及这里的一些帖子)我设法有一个脚本,通过菜单栏项切换代理(这里是它的图片,因为我不能因为声誉限制而内联发布).

不幸的是我的代码非常慢(≈6sec),这使得它像脚本一样不切实际.第一个菜单立即打开,但子菜单和代理服务器的选择需要几秒钟.

我正在使用以下代码:

set theProxy to "Netshade US 4"
tell application "System Events" to tell process "NetShade"
    tell menu bar item 1 of menu bar 2
        click
        tell menu item "NetShade Proxy" of menu 1
            click
            tell menu item theProxy of menu 1
                click
            end tell
        end tell
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

我已经尝试添加ignoring application responses,像在不同的线程(链接)中建议,但这没有帮助.

最后我的问题是:有没有办法加快这个过程?也许甚至可以在后台完成所有这些操作而不显示菜单项?

PS:我正在运行OS X 10.9.1

Avi*_*sal 6


修复摘要

要消除延迟,您需要做两件事:

(I)识别导致延迟的点击,并将该行仅包含在ignoring application responses块中,如下所示.就我而言,click bt之后执行进入等待模式5到6秒.

  ignoring application responses
        click bt
  end ignoring
Run Code Online (Sandbox Code Playgroud)

(II)然后我还必须使用以下命令杀死系统事件并再次启动它.

  do shell script "killall System\\ Events"
  delay 0.1  
  -- Rest of the code to click stuff or send keycodes
Run Code Online (Sandbox Code Playgroud)

这解决了延迟问题.


细节

我遇到了同样的问题,我创建了一个脚本来通过AppleScript连接/断开我的蓝牙耳机.脚本如下.

tell application "System Events" to tell process "SystemUIServer"
    set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
    click bt
    tell (first menu item whose title is "SBH80") of menu of bt
        click
        tell menu 1
            if exists menu item "Disconnect" then
                click menu item "Disconnect"
            else
                click menu item "Connect"
            end if
        end tell
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

该脚本工作正常,但有一个问题,它执行上面的"点击bt"后等待5到6秒.我修改了如下代码,它现在完全正常,没有任何延迟.

tell application "System Events" to tell process "SystemUIServer"

    set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
    ignoring application responses
        click bt
    end ignoring
end tell

do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"

    tell (first menu item whose title is "SBH80") of menu of bt
        click
        tell menu 1
            if exists menu item "Disconnect" then
                click menu item "Disconnect"
            else
                click menu item "Connect"
            end if
        end tell
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)