Cotten
Cotten

Reputation: 9067

keystroke string with new line in it

Consider this

set cannedResponse to "Hello,
Thanks for your support request!
We'll take a look at the issue and get back to you as fast as possible"

tell application "System Events" to keystroke cannedResponse

It prints the text but without the return characters. How could I get them too?

Upvotes: 2

Views: 4249

Answers (5)

BreadEagles
BreadEagles

Reputation: 101

Turns out clipboard supports multi-line text, and paste is a lot faster than keystroke.

set the clipboard to "Hello,
Thanks for your support request!
We'll take a look at the issue and get back to you as fast as possible."

tell application "System Events" to key code 9 using command down

Upvotes: -1

Simon White
Simon White

Reputation: 736

Even though you can’t see them, your variable “cannedResponse” contains line feeds. The “keystroke” command doesn’t know anything about line feeds — it only knows about presses of the keyboard. So if you want a line feed, you get one by simply pressing the return key after each line.

So what you want to do is something like this:

set theCannedResponseLines to {"Hello,", "Thanks for your support request!", "We’ll take a look at the issue and get back to you as fast as possible."}
repeat with theLoopNumber from 1 to the count of items in theCannedResponseLines
    tell application "System Events"
        keystroke (item theLoopNumber of theCannedResponseLines)
        keystroke return
    end tell
end repeat

The lines that you want to type are stored as items in a list. The repeat loop then types each line and presses return afterwards, in exactly the same way that you would type them yourself. You can add or remove lines from the list as necessary and the script will still work because the repeat loop is counting the lines.

Upvotes: 0

Dude named Ben
Dude named Ben

Reputation: 547

For something simple strings like the example you gave, you could do this....

set cannedResponse to "Hello," & return & "
Thanks for your support request!" & return & "
We'll take a look at the issue and get back to you as fast as possible"

tell application "System Events" to keystroke cannedResponse

or better yet...

set crlf to return & linefeed

set cannedResponse to "Hello," & crlf & crlf & "
Thanks for your support request! " & crlf & crlf & "
We'll take a look at the issue and get back to you as fast as possible" as text

tell application "System Events" to keystroke cannedResponse

Line feeds HTH

Upvotes: 3

jackjr300
jackjr300

Reputation: 7191

Because the text contains linefeed character, keystroke linefeed & linefeed print nothing.

keystroke return & return print two blank lines.

You must replace linefeed character

set cannedResponse to "Hello,
Thanks for your support request!
We'll take a look at the issue and get back to you as fast as possible"

set t to do shell script "tr '\\n' '\\r' <<<" & quoted form of cannedResponse -- this replace all linefeed character by return character
tell application "System Events" to keystroke t

Upvotes: 0

regulus6633
regulus6633

Reputation: 19032

Try this...

set cannedResponse to "Hello,
Thanks for your support request!
We'll take a look at the issue and get back to you as fast as possible"

set theList to paragraphs of cannedResponse
set listCount to count of theList
repeat with i from 1 to listCount
    tell application "System Events"
        keystroke item i of theList
        if i is not listCount then keystroke return
    end tell
end repeat

Upvotes: 7

Related Questions