Looks like no oneā€™s replied in a while. To start the conversation again, simply ask a new question.

Deleting paragraphs in TextEdit

Hi there,


I'm having some issues deleting certain paragraphs in a document. I want to delete every paragraph which doesn't start with '{'. I have done this so far:



tell application "TextEdit"


activate

open "/Users/John/Desktop/map.rtf"

set this_text to the text of document 1

end tell


set the paragraph_list to every paragraph of this_text

repeat with i from 1 to the count of paragraphs of this_text

set this_paragraph to itemi of the paragraph_list

try

if first character of this_paragraph is not "{" then


deleteitemi of paragraph_list

end if

on error --If there isn't any character


deleteitemi of paragraph_list

end try

end repeat



I don't know what I am doing wrong. Thanks in advance.

Posted on Jan 6, 2013 2:22 AM

Reply
27 replies

Jan 6, 2013 5:03 AM in response to xaruqui

I'm not sure either, but this seems to work:


set input_paragraph_list to every paragraph of this_text

set output_paragraph_list to {}


repeat with this_paragraph in input_paragraph_list

try

if first character of this_paragraph is "{" then

set end of output_paragraph_list to (this_paragraph as string)

end if

on error --If there isn't any character

end try

end repeat


set my text item delimiters to return

set the_result to (output_paragraph_list as string)


I expect it could be improved.


Jeremy

Jan 6, 2013 7:44 AM in response to xaruqui

The main reason why your script is not working is that you can't delete an item of a list using ā€œdeleteā€.


set aList to {"abc", "def", "ghi"}

deleteitem 2 of aList

--> error "Canā€™t make \"def\" into type reference." number -1700 from "def" toreference


However, you could have made a new list as follows:


set the paragraph_list to every paragraph of this_text

set new_list to {}

repeat with i from 1 to the count of paragraphs of this_text

set this_paragraph to itemi of the paragraph_list

try

if first character of this_paragraph is "{" then

copy this_paragraph to the end of new_list

end if

end try

end repeat

new_list


Message was edited by: Pierre L.

Jan 6, 2013 11:16 AM in response to xaruqui

Well, you just rocked my world with:


tellapplication "TextEdit"

open"/Users/John/Desktop/map.rtf"

delete (paragraphs of front document whose character 1 is not "{")

end tell


But it only works with small files, I'm dealing with more than 20MB text files and TextEdit just crashes. I've also tried the second option with the same result. TextEdit crashes while Applescript is trying to:



get every paragraph of document 1


So a "delay" inside repeat subroutine doesn't work at all.


Any other not-so-hard option? Is it possible to use a pointer to deal with paragraph by paragraph inside the document without using lists? (I mean, one by one)


Thank you

Jan 6, 2013 12:03 PM in response to xaruqui

TextEdit has never (to my knowledge) been capable of handling huge files.


Out of curiosity, are these actually rich text files or are they plain text files that are merely saved as rich text? AppleScript is limited in its rich text handling abilities; if they are plain text files it opens a lot more doors.

Jan 6, 2013 7:14 PM in response to xaruqui

Try this:


set theNewText to ""

tell application "TextEdit"

set theText to text of front document

repeat with k from 1 to (count paragraphs of theText)

set thisParagraph to paragraphk of theText

if (length of thisParagraph > 0) Ā¬

and (character 1 of thisParagraph is "{") then

set theNewText to theNewText & thisParagraph & return

end if

end repeat

end tell

theNewText


Message was edited by: Pierre L. (faster version)

Jan 7, 2013 1:04 PM in response to xaruqui

Thanks again to you, folks! I'm working with plain text, no rtf needed. I'll try it as you say. Meanwhile I tested Pierre's script and it works perfectly, but I'm disappointed with MY Applescripting ability.


I tried to add a condition to the IF but I failed. I want to filter theNewText even more, with:


set theNewText to ""

tell application "TextEdit"

set my text item delimiters to return

set theText to text of front document

repeat with k from 1 to (count paragraphs of theText)

set thisParagraph to paragraphk of theText


--set Pertany to offset of "\"tileUserId\":\"0\"" in thisParagraph

if (length of thisParagraph > 0) Ā¬

and (character 1 of thisParagraph is "{") Ā¬

and (offset of "\"Water\":\"0\"" in thisParagraph is 0) then

set theNewText to theNewText & thisParagraph & return

end if

end repeat

--theNewText

makenewparagraphat the end of front documentwith datatheNewText

save front document

end tell


I'm not sure what's happening. Thanks.

Jan 7, 2013 1:25 PM in response to xaruqui

because I'm the one who raised the plain text point, I'm going to through this non-TextEdit version into the pool. you can see how to structure the IF statement as well if you want to use your version.


set filePath to "/users/yourname/path/to/fileToMangle"


set originalTextParas to paragraphs of (readfilePath)


set newTextParas to {}

repeat with thisPara in originalTextParas

if length of thisPara > 0 and thisPara begins with "{" and thisPara does not contain "\"Water\":\"0\"" then

set end of newTextParas to thisPara

end if

end repeat


set {oldTID, my text item delimiters} to {my text item delimiters, return}

set newText to newTextParas as text

set my text item delimiters to oldTID



set fp to open for accessfilePath with write permission

writenewTexttofp

close accessfp

Jan 7, 2013 1:27 PM in response to xaruqui

Three questions:


1. What's the reason of the ā€œset my text item delimiters to returnā€ line of code?


2. Could you state in plain words what you want to obtain with ā€œoffset of "\"Water\":\"0\"" in thisParagraph is 0ā€?


3. Do you really want to add the new text to the end of your already extremely long initial text, or rather to the end of a new front document?

Jan 7, 2013 7:53 PM in response to xaruqui

Hello


I doubt it is practical to edit 20MB text file by AppleScripting TextEdit.app.


Here's a version to use textutil(1) and perl(1) via 'do shell script' command.

textutil(1) is used to convert rtf to txt as is needed and Perl does the edit.

It will keep only paragraphs starting with { and containing "Word":"0".

It is assumed that text is in UTF-8.


set infile to "/Users/John/Desktop/map.rtf"
set outfile to "/Users/John/Desktop/map_processed.txt"

set sh to "textutil -convert txt -stdout " & infile's quoted form & " \\
| perl -CSD -e 'while (<>) { print if / ^{ .* \"Water\":\"0\" /ox; }' > " & outfile's quoted form

do shell script sh


Regards,

H

Jan 7, 2013 8:08 PM in response to Hiroto

Oops


I misread your intention.

Here's the code to keep paragraphs starting with { and NOT containing "Water":"0".


set infile to "/Users/John/Desktop/map.rtf"
set outfile to "/Users/John/Desktop/map_processed.txt"

set sh to "textutil -convert txt -stdout " & infile's quoted form & " \\
| perl -CSD -e 'while (<>) { print if /^{/o && !/\"Water\":\"0\"/o; }' > " & outfile's quoted form

do shell script sh


Regards,

H

Jan 8, 2013 9:21 AM in response to xaruqui

Hi there!


About the reason of ā€œset my text item delimiters to returnā€, well, forget about that. It's a test I did when It didn't work and I forgot to delete it as I pasted it here.


In plain words, I want to delete a paragraph if it contais the words "Water":"0" That's why I tried to use 'if offset=0', Am I wrong? (again? šŸ˜€)


About adding the result to the same document, that's not what I'm intending to do, it's just because I do the tests in a tiny sample document to make it work. Once it works as I expect, I run it with a real document to another one. I started doing this after 5 TextEdit hangs...


Hiroto, thanks! it works like a charm with huge text documents BUT (there is always a but) It doesn't filter "Water":"0". Is it possible because it's inside a string without spaces? Example:


"Wood":"45","Water":"0","Stone":"1220","Fire":"9","Hammer":"355"


Thanks a lot, guys.

Jan 8, 2013 10:12 AM in response to xaruqui

In plain words, I want to delete a paragraph if it contais the words "Water":"0" That's why I tried to use 'if offset=0', Am I wrong? (again? šŸ˜€)


You were nearly right! See below:


set theNewText to ""

tell application "TextEdit"

set theText to text of front document

repeat with k from 1 to (count paragraphs of theText)

set thisParagraph to paragraphk of theText

if (length of thisParagraph > 0) Ā¬

and ((offset of "\"Water\":\"0\"" in thisParagraph) is 0) Ā¬

and (character 1 of thisParagraph is "{") then

set theNewText to theNewText & thisParagraph & return

end if

end repeat

makenewdocumentat front with properties {text:theNewText}

activate

end tell

Deleting paragraphs in TextEdit

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.