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

AppleScript separating one text file into many at Paragraph break

I have a textfile that is seperates each section of text by two returns. How could I write an Applescript to make each of these paragraphs into a seperate document? The text I am starting with is:

"The text does not state explicitly that the festival in question is the fall vintage feast, but as all commentators recognize, its vineyard setting undoubtedly indicates such." Oesterley, 142 {Ackerman, 1998, #1950@277n3}


Despite her patently absurd explanation for the parallels between the Benjamites and the Ephraimites (one that doesn't take social status into account!), she does do a good job of saying that the location of Shiloh, would have always been known. Or perhaps not. It could have possibly been written in a day before the prominence of Shiloh (or the ark which then retroactively made Shiloh famous). {Ackerman, 1998, #1950@255}


Reasons to see 1 Sam as a Pilgrimage feast (or connected with the grape harvest), miyyāmîm yāmîmâ (found only in Judges 21:19; 1 Sam 1:3; 2:19 and only elsewhere in Exod 13:10 and Judge 11:40). Hanna and Elqana "go up" (a term she associates with pilgrimages). Large amounts of food (not exactly mentioned in the Judges text). {Ackerman, 1998, #1950@258}


"The Shiloh festival described in 1 Sam 1:1-2:10, moreover, seems to involve the same copious amounts of eating and drinking that are elsewhere, in Judge 9:27, for example, associated with the fall vintage celebration. As I stressed in chapter 2, the priest Eli's query to Hannah as he watchers her pray—asks if she were drunk—especially indicates this, for Eli naturally assumes that Hannah, as a celebrant at the feast of the new wine, would have been drinking liberally." {Ackerman, 1998, #1950@258}

(etc).

Posted on Jun 6, 2011 4:32 AM

Reply
9 replies

Jun 6, 2011 10:05 AM in response to echindod

There are different ways to do this in AppleScript. The easiest, maybe, would be as follows:


set filePath to "/path/to/textfile.txt"

set txtPs to every paragraph of (read filePath)


set basePath to text 1 thru -5 of filePath

repeat with i from 1 to count of txtPs

set fp to open for access (basePath & " " & i & ".txt") with write permission

set eof of fpto 0

write item i of txtPs to fp


close accessfp

end repeat


of course, with the particular format of the text you'll end up with a set of blank documents,but you can just discard those. the issue is that the visual paragraph format is actually 'linefeed-space-linefeed', and I suspect you copied this of the web because it looks like the space is actually a character. you could tweak the script so that it discarded the empties docs itself, but unless you do this a lot, tweaking it is probably more trouble than tossing out the extras.

Jul 4, 2011 2:38 PM in response to twtwtw

I'm sorry to revive an old post, but I need to do something similar.


I have a text file full of a collection of articles I've pulled from a database and I want to split them and put them into separate files, where the text file is titled "prefix_suffix" and the individual articles would need to be titled "prefix_a[number of article]_suffix" (the "a" before the number is intended to indicate that it's an article).


It's possible to tell that an article has finished because there's a copyright notice at the bottom ("All Rights Reserved"), so every time the script bumps into that string, it would have to put the text between that string and the previous one into a separate file.


Do you think this is possible? If so, how would I do that?


I did find an old .app that was supposed to do that, but it seems that it uses deprecated code, so it won't work on my mac...


Thanks a lot!

Jul 4, 2011 7:39 PM in response to greenleaf330

Next time please start a separate thread for a new problem. it makes it easier for others who might be searching for help through the archives.


This script should do it (I haven't tested it, but it's really straightforward):


set f to choose file with prompt "Choose the file to parse."

set fold to (choose folder with prompt "Choose a folder to store the files in.") as text


tell application "Finder"

set fName to name of f

end tell


set fp to open for accessf

set bigText to read fp

close accessfp


set parsingText to "All Rights Reserved"

set parsedList to tid(bigText, parsingText)

-- the following line requires that the file name be exactly in the form "<prefix>_<suffix>" with only one underscore

set {prefix, suffix} to tid(fName, "_")

repeat with i from 1 to count of parsedList

set newFName to prefix & "_a[" & i & "]_" & suffix

set fp to open for access (fold & newFName) with write permission


-- uncomment the following line if you need to overwrite old files, otherwise it will append


-- set EOF of fp to 0


write ((itemi of parsedList & parsingText) as text) tofp


close accessfp

end repeat


on tid(input, delim)


-- a subroutine to handle text item delimiters. Useful tool, but so danged wordy.--

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

if class of input is list then

set output to input as text

else

set output to text items of input

end if

set my text item delimiters to oldTID

return output

end tid

Jul 5, 2011 12:00 AM in response to greenleaf330

sure: delete line 2 and replace the finder tell block in lines 4-6 with the following:


tell application "Finder"

set fName to name of f


-- the following line assumes that the file name has a three character extension, like '.txt'

set foldName to text 1 thru -5 of fName

set fold to (makenewfolderat (path todocuments folderfromuser domain) with properties {name:foldName}) as text

end tell

That creates a new folder in your documents folder with the same name as the original file (minus the extension) and stores it in the same variable 'fold'. You might need to tweak it a little - change the path to a different location, change things if it has a different file extension - but you can probably figure that out.

AppleScript separating one text file into many at Paragraph break

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