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

查看:283
本文介绍了加快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应用没有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).

不幸的是我的code是极其缓慢(≈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.

我用下面的code:

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

我已经尝试添加忽略了应用程序的响应,如建议在不同的线程(<一个href=\"http://stackoverflow.com/questions/16365281/is-applescript-ui-scripting-very-slow-in-general-or-is-it-my-script-or-somethi\">link),但是这并没有帮助。

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

推荐答案

我有,我创建了一个脚本来连接/通过的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

该脚本工作正常,但有一个问题,即要执行后等待5到6秒点击BT之上。我修改了code以下,现在正在精绝没有任何延迟。

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

学习基本上是确定这是造成延误的点击如下图所示包围在忽略了应用程序的响应块仅该行。就我而言,这是点击BT 之后执行正想进入等待模式,5至6秒。

The learning basically was to 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

然后我也只好杀了系统事件,并再次使用以下命令启动。

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
tell application "System Events" to tell process "SystemUIServer"

这解决了延迟问题。

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

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