Ali
Ali

Reputation: 1448

Applescript - Keystroke text from variable

I have a file that contains some text. I am able to read this text into a variable. I want to use the keystroke command to send this text to an application. When the line containing the keystroke runs, it produces dialog box that contains an error 'Syntax Error - Can't get keystroke "..content of file.." '

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

How can I send the content of a file as keystrokes?

Upvotes: 2

Views: 3214

Answers (1)

user3439894
user3439894

Reputation: 7555

The keystroke command is part of the Process Suite of System Events, therefore when using the keystroke command, it must come from System Events.

Example:

tell application "System Events" to keystroke fileText

However, that said, you need to first set focus to where you want it to be typed, e.g.:

tell application "TextEdit" to activate
delay 1

For example, your code would be:

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

Upvotes: 3

Related Questions