Applescript 包含新行的击键字符串

Applescript 包含新行的击键字符串,applescript,Applescript,想想这个 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 它打印文本,但不包含返回字符。我怎样才能得到它们呢?试试这个 set cannedRespon

想想这个

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
它打印文本,但不包含返回字符。我怎样才能得到它们呢?

试试这个

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

由于文本包含换行符,
击键换行和换行
不打印任何内容

按键返回和返回
打印两行空行

必须替换换行符

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
将cannedResponse设置为“Hello,
感谢您的支持请求!
我们将研究这一问题,并尽快与您联系”

将t设置为执行shell脚本“tr'\\n'\\r'对于像您给出的示例这样的简单字符串,您可以这样做

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
或者更好的是

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

HTH

即使你看不到它们,你的变量“cannedResponse”也包含换行符。“keystroke”命令对换行符一无所知,它只知道键盘的按键次数。因此,如果你想要换行符,只需在每行后按回车键就可以得到

所以你要做的是这样的:

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

要键入的行存储为列表中的项目。重复循环然后键入每一行,然后按return键,方式与您自己键入的方式完全相同。您可以根据需要从列表中添加或删除行,脚本仍将工作,因为重复循环正在计算行数。

显示剪贴簿rd支持多行文本,粘贴比击键快得多

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

这样一个简单的解决方案!请注意,它也适用于紧随其后的换行符(例如
\n\n
)。