I want to replace the first two characters of the paragraph “para”, it works in the script that I have included below by twice replacing the first character with “”. I would like to learn a better way to do this, any advice would be appreciated.
tell application "Pages"
activate
tell front document
set a to paragraphs of body text
set paraNumber to 1 -- Initialize paragraph counter
repeat with para in a
-- Check if paragraph starts with "#"
if (text of para starts with "#") then
-- Format the paragraph
tell paragraph paraNumber of the body text
set font to "Arial Black"
set size to 14
set (the first character) to ""
set (the first character) to "" --Second instance is neccessary because i cant get it to work in one go
end tell
end if
set paraNumber to paraNumber + 1 -- Increment paragraph counter
end repeat
end tell
end tell
set characters 1 thru 2 to ""
** EDIT** seems to have problems
try this…
tell application "Pages"
activate
tell front document
set a to paragraphs of body text
set paraNumber to 1 -- Initialize paragraph counter
repeat with para in a
-- Check if paragraph starts with "#"
if (text of para starts with "#") then
-- Format the paragraph
set myText to paragraph paraNumber of the body text
tell me to set myText to text 3 thru -1 of myText
set paragraph paraNumber of the body text to myText
tell paragraph paraNumber of the body text
set font to "Arial Black"
set size to 14
end tell
end if
set paraNumber to paraNumber + 1 -- Increment paragraph counter
end repeat
end tell
end tell
To do multiple paragraphs do this…
tell application "Pages"
activate
tell front document
set a to paragraphs of body text
set paraNumber to 1 -- Initialize paragraph counter
repeat with paraNumber from 1 to count a
set para to item paraNumber of a -- Check if paragraph starts with "#"
if (text of para starts with "#") then
-- Format the paragraph
set myText to paragraph paraNumber of the body text
tell me to set myText to text 3 thru -1 of myText
set paragraph paraNumber of the body text to myText
tell paragraph paraNumber of the body text
set font to "Arial Black"
set size to 14
end tell
end if
set paraNumber to paraNumber + 1 -- Increment paragraph counter
end repeat
end tell
end tell
Thanks, that works for me!
I initially tried set characters 1 thru 2 to ""
but it removes 3 characters.
Any idea why that happens?
For me it removed 2 characters but not the 1st and 2nd character, but the 1st and 3rd instead.
Hi.
I’m seeing this too with set characters 1 thru 2 to ""
. I’m not sure why. Possibly each character in the range is set to “” individually [*], in which case the setting of the first character to “” would immediately make the second character the first and the third character the second. So it would be what was originally the third character that got set to “” as character 2
. It’s a bit surprising that it doesn’t happen when delete
is used instead:
tell application "Pages"
activate
tell body text of document 1
tell (paragraphs where it begins with "#")
-- Do these first while the paragraphs still begin with "#"!
set font to "Arial Black"
set size to 14
-- NOW delete the "#" and following character.
delete (characters 1 thru 2)
end tell
end tell
end tell
[*] EDIT: Yes. This is in fact what happens, as shown if the characters are set to, say, “X” or to “Hello” instead of to “”.