skilip
June 24, 2004, 1:58pm
#1
Can anybody tell me how i can find & replace paragraphs with only one, two or three characters? I’ve tried everything and it’s beginning to piss me off.
mgh
June 24, 2004, 2:28pm
#2
Replace paragraphs of fewer than three characters with… what? And in what application?
mgh
skilip
June 24, 2004, 2:32pm
#3
I want to do this only with AppleScript. I want to remove lines in a text file containing less than 3 characters. I know this can be done with BBedit but i prefer to build a standalone application.
jonn8
June 24, 2004, 3:19pm
#4
This will replace the short paragraphs with something else:
–Open this script in a new Script Editor window.
set replace_text to “This is the replacement text.”
set minimum_chars_for_replacement to 3
set the_file to “path:to:file”
set all_paragraphs to paragraphs of (read file the_file )
repeat with i from 1 to (count all_paragraphs )
if ((count (item i of all_paragraphs )) < minimum_chars_for_replacement ) then set item i of all_paragraphs to replace_text
end repeat
tell (a reference to my text item delimiters )
set {old_delim , contents } to {contents , return }
set {all_paragraphs , contents } to {“” & all_paragraphs , old_delim }
end tell
try
set f to open for access file the_file with write permission
set eof of f to 0
write all_paragraphs to f starting at eof as (class of all_paragraphs )
close access f
on error the_error
try
close access file the_file
end try
return the_error
end try
And this one will simply remove the short paragraphs entirely:
–Open this script in a new Script Editor window.
set minimum_chars_for_replacement to 4
set the_file to “path:to:file”
set all_paragraphs to paragraphs of (read file the_file )
set replace_paragraphs to {}
repeat with i from 1 to (count all_paragraphs )
if ((count (item i of all_paragraphs )) is greater than or equal to minimum_chars_for_replacement ) then set end of replace_paragraphs to item i of all_paragraphs
end repeat
tell (a reference to my text item delimiters )
set {old_delim , contents } to {contents , return }
set {replace_paragraphs , contents } to {“” & replace_paragraphs , old_delim }
end tell
try
set f to open for access file the_file with write permission
set eof of f to 0
write replace_paragraphs to f starting at eof as (class of replace_paragraphs )
close access f
on error the_error
try
close access file the_file
end try
return the_error
end try
Jon
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code ]
skilip
June 24, 2004, 6:49pm
#5
That did the job! Thanks a lot!
dant
June 24, 2004, 7:07pm
#6
tell (a reference to my text item delimiters)
set {old_delim, contents} to {contents, return}
set {replace_paragraphs, contents} to {"" & replace_paragraphs, old_delim}
end tell
That is so slick! I’m always using text item delimiters but this conciseness (concision?) is beautiful (I’m sure this is old hat to everyone else) - think I’ll steal it.