Reputation: 613
I want to separate each paragraph with an extra line break in the italicised text below (snippet of larger document). I have two methods that fail. My first method throws an exception at the end of paragraph 2, but it successfully inserts a different character such as "*" at the end of each paragraph:
Text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi interdum, felis in placerat dignissim, odio mi aliquam metus, non luctus lacus erat et neque. Curabitur vitae ultrices mi. Quisque posuere erat at sagittis vestibulum. Duis pulvinar at nibh sed dictum. Etiam id lectus a nisi dapibus elementum. Morbi ac tristique turpis, at vehicula tellus. Morbi et risus ac nisl pulvinar vehicula.
Fusce mattis gravida augue, quis gravida leo scelerisque sit amet. Cras nec rutrum nisl. Sed faucibus semper purus eu commodo. Etiam mollis est magna, non pretium nulla euismod eu. Suspendisse ut lectus rutrum, tempus tellus sit amet, imperdiet orci. Maecenas congue neque sit amet leo sagittis, in bibendum risus maximus. Vestibulum ac semper nulla, in pulvinar ligula. Vestibulum vel nunc sit amet metus sodales eleifend et eu purus. Quisque dictum ultricies nulla, et euismod lectus vestibulum vel. In gravida neque sit amet nisl luctus, eu rutrum odio mollis. Sed ac odio velit.
First method:
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
My second method partly works by separating out the paragraphs but it does not insert the return character exactly at the end of each paragraph. It seems that inserting a newline character at the end of the first paragraph causes changes further down the document that throws the computer off (my guess and basic explanation).
Second method
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
Can you help me adjust either code to cause the desired outcome?
Upvotes: 1
Views: 4522
Reputation: 613
Using the method referred to by matt, I've posted my example code. Maybe someone can comment if it needs refactoring?
I prefer matt's method as it's easier to understand if someone else was to look at the code (or myself in the future).
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
Upvotes: 0
Reputation: 534950
Yes, inserting a character changes the indexes of subsequent characters. So insert the character in reverse order, starting at the end.
Upvotes: 1
Reputation: 3412
Since adding paragraphs changes the indexing, one soulution would be to just compensate for that by doubling the count and indexing by 2s, for example
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
Upvotes: 1