2014-09-20 37 views
1

考慮這個擊鍵串與新線就

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 

它打印文本,但沒有返回字符。我怎麼能得到他們呢?

回答

2

嘗試......

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 
+0

這樣一個簡單的解決方案!請注意,它也適用於相互之間的換行符(例如'\ n \ n')。 – Cotten 2014-09-26 09:12:14

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 
0

對於一些簡單的字符串像你給了,你能做到這一點的例子....

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

0

即使您看不到它們,您的變量「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 

要輸入存儲爲列表中的項目的線條。重複循環然後鍵入每行,然後按回車鍵,與您自己輸入的方式完全相同。您可以根據需要添加或刪除列表中的行,並且腳本仍然可以工作,因爲重複循環正在對行進行計數。