加速 AppleScript UI 脚本? [英] Speed up AppleScript UI scripting?

查看:32
本文介绍了加速 AppleScript UI 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using NetShade as a proxy service and thought I could try to automate the switching between the different proxies as a nice start for my first AppleScript script.

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

The NetShade-app has no AppleScript support, so I have to use UI scripting. After a few tries (and some posts here) I managed to have a script, that switches the proxies via the menu bar item (here is a picture of it, since I can't post it inline due to reputation limit).

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

Unfortunately my code is extremely slow (≈6sec), which makes it kind of impractical as a script. The first menu opens immediately, but the selection of the sub-menu and the proxy server takes several seconds.

我正在使用以下代码:

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

我已经尝试添加忽略应用程序响应,就像在另一个线程中建议的那样 (链接),但这没有帮助.

I already tried to add ignoring application responses, like suggested in a different thread (link), but that didn't help.

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

So finally my questions: Is there a way to speed the process up? Maybe even a way to do all this in the background, without showing the menu items?

P.S.:我运行的是 OS X 10.9.1

P.S.: I'm running OS X 10.9.1

推荐答案


修复摘要

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


Summary of the fix

To remove delay you need to do two things:

(I) 确定导致延迟的点击,并仅在 ignoring application response 块中包含该行,如下所示.在我的例子中,它是 click bt 之后执行进入等待模式 5 到 6 秒.

(I) Identify the click which is causing the delay and enclose only that line in the ignoring application responses block as shown below. In my case, it was click bt after which the execution was going into a wait mode for 5 to 6 seconds.

  ignoring application responses
        click bt
  end ignoring

(II) 然后我还不得不使用以下命令终止系统事件并重新启动它.

(II) I then also had to kill System Events to and start it again using the following commands.

  do shell script "killall System\\ Events"
  delay 0.1  
  -- Rest of the code to click stuff or send keycodes

这解决了延迟问题.

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

I was having the same problem where I created a script to connect/disconnect my bluetooth headset through AppleScript. The script is given below.

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

脚本运行良好,但有一个问题,在执行上面的click bt"后它会等待 5 到 6 秒.我修改了代码如下,它现在工作得很好,没有任何延迟.

The script was working fine but had a problem where it would wait for 5 to 6 seconds after executing "click bt" above. I modified the code as follows and it is working absolutely fine now without any delay.

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

这篇关于加速 AppleScript UI 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆