提高applescript击键速度

问题描述:

我正在使用applescript自动执行一些浏览器活动。我将它与语音识别联系在一起,以便我可以制作一些自定义的语音命令。提高applescript击键速度

一个这样的命令是“我想回家”,当听到时,我的浏览器拉上相关的巴士时间表。鉴于公共交通系统使用PHP和GET,URL变得相当长。因此,苹果脚本中的keystroke myURL需要一段时间才能执行(类似1-2秒)。虽然我可以忍受1-2秒的时间,但我真的不想。

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

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

打开位置在加标定义,不应该在告诉声明包围。

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 

要定位铬:

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 
+0

虽然这是正确的(+1),但我更喜欢可与Chrome兼容的解决方案(我的浏览器 - 请参阅编辑) – inspectorG4dget 2013-03-05 15:43:56

+0

上面列出了三个正确的命令。 – adayzdone 2013-03-05 16:48:07

我确定你可以脚本浏览器直接打开URL,而不是输入键盘输入(在AppleScript中伪装输入事件应该被认为是最后一个采取)。

您可以用open location标准加入做到这一点:

open location "http://google.com" 
+0

没问题。使用File-> Open Dictionary ...;它非常有用。 – nneonneo 2013-03-05 15:35:38

+0

我尝试过'Open Dictionary'。不幸的是,谷歌浏览器的吸引力如此之少,我不得不求助于键盘 – inspectorG4dget 2013-03-05 15:37:25

+0

正如你所提到的,“开放位置”是苹果标准的一个标准补充。因此,我想知道为什么你在Finder tell block中有这个命令?您不需要Finder执行该命令,因此只需使用打开的位置线。 – regulus6633 2013-03-05 23:01:04

解决这个另一种方式是把文本剪贴板,然后粘贴它。你可以先保存剪贴板的内容,然后放回去,这样你就不会丢失已经存在的内容。这种方法适用于其他情况,当你想输入大量的文本,而不仅仅是像其他答案的浏览器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