Hello Morley Chalmers,
I don't have BBEdit 6.5.3 and not sure how the rest of your script is written. Maybe you're using BBEdit's own commands to search and replace text directly in its front window?
Anyway, the code I posted does not work in such way. You have to get the (whole) text of target document first, then process it by the given handler and finally set the (whole) text of target document to the result.
Something like the following code, which will replace the contents of style elements with string ' REPLACED '. If you want to replace both tags and contents, use the line currently commented out. Also if you're replacing 'script' elements, use "<script" and "</script>" instead of "<style" and "</style>".
(I borrowed the part to get and set the text of front window from red_menace's code, for I don't know how to script BBEdit. And again, please copy the code from this web page, not from subscribed email text because I escaped some characters to prevent fora software from intervening.)
Good luck,
Hiroto
--SCRIPT
local t, x, y, z
tell application "BBEdit 6.5.3"
set t to text of window 1
set {x, y, z} to {"<style", "</style>", "<style REPLACED </style>"} -- to replace contents only
--set {x, y, z} to {"<style", "</style>", "REPLACED"} -- to replace both tags and contents
set t to my replaceBlocks({x, y}, z, t)
set text of window 1 to t
end tell
on replaceBlocks({x, y}, z, t)
(*
string x, y : block start tag, block end tag
string z : replacing string for each block "x..y"
string t : source text
return string : replaced string -- [1]
[1] This handler does not support nested blocks.
(Only the inner most block will be replaced if nested.)
*)
script o
property tt : {}
property uu : {}
property rr : {}
property astid : a reference to AppleScript's text item delimiters
try
set astid0 to astid's contents
set astid's contents to {x}
set tt to t's text items
set end of my rr to my tt's item 1
set astid's contents to {y}
repeat with i from 2 to count my tt
set uu to my tt's item i's text items
if (count my uu) = 1 then -- y not found after x in this segment
set end of my rr to x & my tt's item i
else
set end of my rr to z & my uu's rest
end if
end repeat
set astid's contents to astid0
on error errs number errn
set astid's contents to astid0
error "replaceBlocks(): " & errs number errn
end try
return my rr's item 1 & my rr's rest
end script
tell o to run
end replaceBlocks
--END OF SCRIPT
Message was edited by: Hiroto (minor correction to typo and wording)