AppleScript 中的 CR 与 LF [英] CR vs. LF in AppleScript
问题描述
我使用 AppleScript 处理从 Keynote 演讲者笔记中获取的文本.通常我会逐段浏览文本,但最近我发现了一个例子,其中使用了»软返回«(ASCII 13 aka CR)而不是真正的返回(ASCII 10 aka LF).
I use AppleScript to process text that I take from Keynote presenter notes. Usually I go through the text paragraph by paragraph, but recently I found an instance where a »soft return« (ASCII 13 aka CR) was used instead of a real return (ASCII 10 aka LF).
我现在尝试预处理文本以摆脱 CR 以支持 LF,但以下方法无缘无故失败:
I now try to pre-process the text in order to get rid of the CR in favour of LFs, but the following approach fails for no apparent reason:
on purgeCR(aText)
set my text item delimiters to {ASCII character 13}
return text items of aText as text
end purgeCR
当我使用具有 CR 和 LF 的文本提供此内容时,它们都将保持不变,我真的不知道为什么.出于某种原因,CR 不能用作文本项分隔符.
When I feed this with text that has CRs and LFs, they will all stay the same, and I don'rt really know why. for some reason, CR does not work as a text item delimiter.
另一个尝试是使用 shell:
Another attempt was to use the shell:
set z to do shell script "echo " & quoted form of a & " | tr '\r' '\n' " without altering line endings
也没有运气.
出于某种原因,CR 干扰了 AppleScript 处理文本的能力,或者我只是因为愚蠢而没有看到非常明显的东西.
For some reason, CR messes with AppleScript's ability to process text, or I'm just not seeing something very obvious, because stupid.
我有一个工作脚本可以逐个字符地遍历文本,但必须有一种更简单(更快)的方法来做到这一点!
I have a working script that goes through the text character by character, but there has to be an easier (and faster) way to do this!
推荐答案
以下例子 AppleScript 代码,在Script中运行编辑器,将回车替换为换行:
The following example AppleScript code, run in Script Editor, will replace the carriage returns with line feeds:
set foo to "This text contains\rinstead of\n which is what is expected.\rHow do I get rid of them?"
set AppleScript's text item delimiters to return
set bar to text items of foo
set AppleScript's text item delimiters to linefeed
set foo to text items of bar as text
set AppleScript's text item delimiters to {}
return foo
更新地址评论:
作为在 macOS Catalina 10.15.6 下使用 Keynote 版本 10.2 (7028.0.88) 的一个简单测试,我用一张幻灯片创建了一个新的演示文稿,这是唯一的窗口在 Keynote 中打开,而不是从默认设置中修改它.
As a simple test under macOS Catalina 10.15.6 using Keynote version 10.2 (7028.0.88), I created a new presentation with one slide and this was the only window open in Keynote, while not modifying it from its defaults.
在脚本编辑器中,我运行了以下示例 AppleScript 代码:
In Script Editor, I ran the following example AppleScript code:
set foo to "foo\rbar"
ASCII number of character 4 of foo
tell application "Keynote" to tell slide 1 of front document
set presenter notes to foo
set bar to presenter notes
end tell
ASCII number of character 4 of bar
tell application "Keynote" to tell slide 1 of front document
set foo to presenter notes
end tell
ASCII number of character 4 of foo
set AppleScript's text item delimiters to return
set bar to text items of foo
log bar
set AppleScript's text item delimiters to linefeed
set foo to text items of bar as text
log foo
set AppleScript's text item delimiters to {}
ASCII number of character 4 of foo
tell application "Keynote" to tell slide 1 of front document
set presenter notes to foo
set bar to presenter notes
end tell
ASCII number of character 4 of bar
在脚本编辑器的回复窗格中,它显示了以下内容:
In the Replies pane of Script Editor it showed the following:
tell current application
ASCII number "
"
--> 13
end tell
tell application "Keynote"
set presenter notes of slide 1 of document 1 to "foo
bar"
get presenter notes of slide 1 of document 1
--> "foo
bar"
end tell
tell current application
ASCII number "
"
--> 13
end tell
tell application "Keynote"
get presenter notes of slide 1 of document 1
--> "foo
bar"
end tell
tell current application
ASCII number "
"
--> 13
(*foo, bar*)
(*foo
bar*)
ASCII number "
"
--> 10
end tell
tell application "Keynote"
set presenter notes of slide 1 of document 1 to "foo
bar"
get presenter notes of slide 1 of document 1
--> "foo
bar"
end tell
tell current application
ASCII number "
"
--> 10
end tell
Result:
10
如您所见,回车确实被替换为换行.
As you can see, the carriage return was indeed replaced with a line feed.
如果您没有得到相同的结果,我唯一可以提出的建议是在您将 presenter notes 设置为具有 carriage 的 text 之前返回替换为换行符,首先将演示者注释设置为"
,然后设置为更正后的文本.
If you do not get the same results, the only suggestion I can make is before you set the presenter notes to the text that has had the carriage returns replaced with line feeds, first set the presenter notes to ""
, then set to the corrected text.
这篇关于AppleScript 中的 CR 与 LF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!