我正在使用applescript来自动化一些浏览器活动。我把它和语音识别联系起来,这样我就可以做一些定制的语音命令。
其中一个命令是“我想回家”,当听到这个命令时,会在我的浏览器上调出相关的巴士时间表。考虑到公共交通系统使用PHP和GET,URL会变得相当长。因此,applescript中的keystroke myURL需要一段时间才能执行(大约1-2秒)。虽然我可以忍受失去1-2秒,但我真的宁愿不。
考虑到这一点,是否有可能更快地发送这些击键?我相信我在某个地方读到了使用key code比使用keystroke更快,但在我的生活中,我不知道我在哪里读到的。
编辑:我选择的浏览器是Google,我找不到它的链接。这就是我不得不使用击键的原因。因此,与Safari相比,我更喜欢使用Chrome的答案
发布于 2013-03-05 15:41:00
开放位置在标准添加中定义,不应包含在tell语句中。
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发布于 2017-07-24 00:54:55
解决这一问题的另一种方法是将文本放入剪贴板,然后粘贴它。您可以先保存剪贴板内容,然后再放回去,这样就不会丢失已经存在的内容。当您想输入大量文本时,此方法适用于其他情况,而不只是针对浏览器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发布于 2013-03-05 15:25:54
我确信您可以编写浏览器脚本,直接打开URL,而不是键入击键(在AppleScript中伪造输入事件应该被认为是最后的手段)。
您可以通过open location标准添加来完成这一任务:
open location "http://google.com"https://stackoverflow.com/questions/15227490
复制相似问题