How to replace a paragraph found by a whose query?

Bruce Horrocks <[email protected]> Mon, 24 Jul 2023 23:11:16 +0100
Newsgroups alt.comp.lang.applescript
Message-ID <[email protected]>
I recently came across some late 1980's vintage HP LaserJet spool files 
which have PCL1 escape sequences embedded in them. The codes are 
relatively simple - underline etc - so I thought it would be simple 
enough to open them as text files in Pages and use an AppleScript to 
reformat them as Pages documents (using a fixed width font to maintain 
the old-style look etc).

In the original files, a typical header and first line of following text 
looks like:

   <ESC>&dDIntroduction<ESC>&d@
   This is the introduction...

where <ESC>&dD turns on underlining and <ESC>&d@ turns it off again.

Pages strips the escape characters when loading the file so the content 
in Pages becomes:

   &dDIntroduction&d@
   This is the introduction...

Every line ends with a cr+lf which Pages converts into paragraph markers 
so each paragraph is a single line / each single line is a Pages paragraph.

The following AppleScript finds the correct header lines and the 'set 
temp...' line correctly obtains the heading text minus the printer 
control codes at the beginning and end.


tell application "Pages"
   tell front document
     tell body text
       repeat with p in (every paragraph where it starts with "&dD")
         set temp to (characters 4 thru -4 of p) as text
         set p to temp -- does nothing!
       end repeat
     end tell
   end tell
end tell


What I can't get to work is the "set p to temp" line which fails 
silently - nothing changes in Pages but ScriptEditor reports no error. 
If it's not obvious, what I want to do is replace the line with the 
trimmed line so as to remove the PCL codes from the beginning and end.

All the examples I can find on the Web seem to iterate through all the 
paragraphs numerically so there is always an explicit paragraph number 
to use. I can't seem to find an example of how to reference paragraphs 
returned by a 'whose' filter.

Version info: MacOS Ventura 13.4.1

-- 
Bruce Horrocks
Surrey, England