按键字符串,其中包含新行

时间:2014-09-20 08:32:11

标签: 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

它打印文本但没有返回字符。我怎么能得到它们呢?

5 个答案:

答案 0 :(得分:3)

试试这个......

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

答案 1 :(得分:1)

对于像你给出的例子那样的简单字符串,你可以这样做....

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

Line feeds HTH

答案 2 :(得分:0)

由于文字包含换行符,keystroke linefeed & linefeed不打印任何内容。

keystroke return & return打印两个空白行。

您必须替换换行字符

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

答案 3 :(得分:0)

即使您看不到它们,您的变量“cannedResponse”也包含换行符。 “击键”命令对换行没有任何了解 - 它只知道键盘的按键。因此,如果您想要换行,只需在每行后按回车键即可获得换行符。

所以你想要做的是这样的事情:

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

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

答案 4 :(得分:-1)

原来剪贴板支持多行文本,粘贴比按键快很多。

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