Applescript - 来自变量的击键文本

时间:2017-09-10 13:31:01

标签: applescript

我有一个包含一些文字的文件。我能够将这个文本读成一个变量。我想使用keystroke命令将此文本发送到应用程序。当包含keystroke的行运行时,它会生成一个对话框,其中包含错误'语法错误 - 无法获取击键“..文件内容..”'

on run {input, parameters}
    set inputfile to "/path/to/textfile.txt"
    set fileText to read inputfile
    keystroke fileText
end run

如何将文件内容作为击键发送?

1 个答案:

答案 0 :(得分:1)

keystroke 命令系统事件 Process Suite 的一部分,因此在使用keystroke命令,它必须来自系统事件

示例:

tell application "System Events" to keystroke fileText

然而,那就是说,您需要先将焦点设置到您想要输入的位置,例如:

tell application "TextEdit" to activate
delay 1

例如,您的代码将是:

on run {input, parameters}
    set inputfile to "/path/to/textfile.txt"
    set fileText to read inputfile
    tell application "TextEdit" to activate
    delay 1
    tell application "System Events" to keystroke fileText
end run
相关问题