提高AppleScript击键速度

时间:2013-03-05 15:21:27

标签: performance applescript keystrokes

我正在使用applescript自动执行某些浏览器活动。我把它与语音识别联系起来,这样我就可以制作一些自定义语音命令。

其中一个命令是“我想回家”,听到这个命令后,在我的浏览器上提取相关的公交时刻表。鉴于公共交通系统使用PHP和GET,URL变得非常长。因此,applescript中的keystroke myURL需要一段时间才能执行(类似于1-2秒)。虽然我可以忍受1-2秒的失利,但我真的不愿意。

考虑到这一点,是否可以更快地发送这些击键?我相信我在某处读到使用key code比使用keystroke更快,但对于我的生活,我无法弄清楚我在哪里阅读。

编辑:我选择的浏览器是谷歌浏览器,我找不到网址挂钩。这就是为什么我不得不求助于使用击键。因此,我更喜欢适用于Chrome over Safari的答案

3 个答案:

答案 0 :(得分:2)

解决此问题的另一种方法是将文本放入剪贴板,然后粘贴它。您可以先保存剪贴板内容并在之后将其放回原处,这样您就不会丢失已存在的内容。当您想要输入大量文本时,此方法适用于其他情况,而不仅仅是针对其他答案的浏览器URL。

    set clipboard_contents to (the clipboard)
    set the clipboard to "Some long string that would take a while with the keystroke method"
    tell application "System Events" to keystroke "v" using command down
    delay 0.2 -- needed because otherwise the next command can run before the paste occurs
    set the clipboard to clipboard_contents

答案 1 :(得分:1)

我非常确定您可以编写浏览器脚本直接打开URL,而不是键入键盘(AppleScript中的伪造输入事件应该被认为是最后的手段)。

您可以使用open location标准添加:

执行此操作
open location "http://google.com"

答案 2 :(得分:0)

开放地点在标准增加中定义,不应包含在陈述声明中。

open location "http://google.com"

如果您想定位特定浏览器,可以使用:

tell application "Safari"
    if not (exists document 1) then reopen
    set URL of document 1 to "http://google.com"
end tell

定位Chrome:

tell application "Google Chrome"
    if not (exists window 1) then reopen
    set URL of active tab of window 1 to "https://www.google.com/"
end tell