AppleScript - 在每个段落末尾添加换行符的最佳方法是什么?
AppleScript - What's the best way to add a line break at the end of each paragraph?
我想在下面的斜体文本(较大文档的片段)中用额外的换行符分隔每个段落。我有两种失败的方法。我的第一个方法在第 2 段的末尾抛出异常,但它成功地在每个段落的末尾插入了不同的字符,例如“*”:
文本:
事实是疼痛本身很重要;病态的有时,房地产投资的导火索,恨我有些恐惧,那不是悲伤湖,也不是。我说的是我生命中的篮球。每个人都不得不把箭放在门厅里。 Duis pulvinar at nibh 但说。即使它不是蛋白质元素。病态和悲伤,但土地的车辆。 Morbi、笑声和排球运动员。
宣传孕妇怀孕很重要。明天,我什至不化妆。但品味始终是纯粹的足球。它也很柔软,不值任何足球表演的价格。推拿当床妆,地球的时间很重要,诊所的经费。梅塞纳斯的功课不应该是轻松射箭,而是喝到最大的笑容。一如既往,枕头里没有玄关。哪怕是现在很多惧怕elifend和pure football的成员。每个人都说没有异想天开,Euismod 床前庭或。孕期不宜多愁,淡妆淡淡。但是他想恨
第一种方法:
set my_file to (choose file with prompt "Choose a text file")
tell application "Pages"
activate
set my_doc to open my_file
tell my_doc to tell the body text
set last character of every paragraph to "
"
end tell
end tell
我的第二种方法通过分隔段落部分起作用,但它不会在每个段落的末尾恰好插入 return 字符。似乎在第一段末尾插入一个换行符会导致文档的更下方发生更改,从而使计算机无法运行(我的猜测和基本解释)。
第二种方法
set my_file to (choose file with prompt "Choose a text file")
tell application "Pages"
activate
set my_doc to open my_file
tell my_doc to tell the body text
set (every character where it is the "
") to (return & return)
end tell
end tell
你能帮我调整任何一个代码以产生预期的结果吗?
由于添加段落会改变索引,一个解决方案是通过将计数加倍并按 2 秒索引来补偿,例如
tell my_doc to tell the body text
repeat with num from 1 to (count paragraphs) * 2 by 2
set paragraph num to paragraph num & linefeed
end repeat
end tell
是的,插入一个字符会更改后续字符的索引。所以以相反的顺序插入字符,从末尾开始。
使用matt提到的方法,我已经发布了我的示例代码。如果需要重构,也许有人可以发表评论?
我更喜欢 matt 的方法,因为如果其他人(或将来我自己)查看代码,它更容易理解。
set my_file to (choose file with prompt "Choose a text file")
tell application "Pages"
activate
set my_doc to open my_file
tell my_doc to tell the body text
set paragraph_list to (every paragraph)
set number_of_paragraphs to (length of paragraph_list)
repeat with n from number_of_paragraphs to 1 by -1
set last character of paragraph n to (last character of paragraph n & linefeed)
end repeat
end tell
end tell
我想在下面的斜体文本(较大文档的片段)中用额外的换行符分隔每个段落。我有两种失败的方法。我的第一个方法在第 2 段的末尾抛出异常,但它成功地在每个段落的末尾插入了不同的字符,例如“*”:
文本:
事实是疼痛本身很重要;病态的有时,房地产投资的导火索,恨我有些恐惧,那不是悲伤湖,也不是。我说的是我生命中的篮球。每个人都不得不把箭放在门厅里。 Duis pulvinar at nibh 但说。即使它不是蛋白质元素。病态和悲伤,但土地的车辆。 Morbi、笑声和排球运动员。
宣传孕妇怀孕很重要。明天,我什至不化妆。但品味始终是纯粹的足球。它也很柔软,不值任何足球表演的价格。推拿当床妆,地球的时间很重要,诊所的经费。梅塞纳斯的功课不应该是轻松射箭,而是喝到最大的笑容。一如既往,枕头里没有玄关。哪怕是现在很多惧怕elifend和pure football的成员。每个人都说没有异想天开,Euismod 床前庭或。孕期不宜多愁,淡妆淡淡。但是他想恨
第一种方法:
set my_file to (choose file with prompt "Choose a text file")
tell application "Pages"
activate
set my_doc to open my_file
tell my_doc to tell the body text
set last character of every paragraph to "
"
end tell
end tell
我的第二种方法通过分隔段落部分起作用,但它不会在每个段落的末尾恰好插入 return 字符。似乎在第一段末尾插入一个换行符会导致文档的更下方发生更改,从而使计算机无法运行(我的猜测和基本解释)。
第二种方法
set my_file to (choose file with prompt "Choose a text file")
tell application "Pages"
activate
set my_doc to open my_file
tell my_doc to tell the body text
set (every character where it is the "
") to (return & return)
end tell
end tell
你能帮我调整任何一个代码以产生预期的结果吗?
由于添加段落会改变索引,一个解决方案是通过将计数加倍并按 2 秒索引来补偿,例如
tell my_doc to tell the body text
repeat with num from 1 to (count paragraphs) * 2 by 2
set paragraph num to paragraph num & linefeed
end repeat
end tell
是的,插入一个字符会更改后续字符的索引。所以以相反的顺序插入字符,从末尾开始。
使用matt提到的方法,我已经发布了我的示例代码。如果需要重构,也许有人可以发表评论?
我更喜欢 matt 的方法,因为如果其他人(或将来我自己)查看代码,它更容易理解。
set my_file to (choose file with prompt "Choose a text file")
tell application "Pages"
activate
set my_doc to open my_file
tell my_doc to tell the body text
set paragraph_list to (every paragraph)
set number_of_paragraphs to (length of paragraph_list)
repeat with n from number_of_paragraphs to 1 by -1
set last character of paragraph n to (last character of paragraph n & linefeed)
end repeat
end tell
end tell