i have a portion of a script that is supposed to strip xtag formatting tags from a text file containing obituary notices. the munge command does this but has been duplicating the last item in the text. it will strip all the tags up to and including the last obituary item, but then the original last item (with tags intact) will remain at the end of the file. how can i get rid of this? i thought checking for the EOF would do the trick. any ideas? thanks in advance.
–
set mungeList to {"@boxDeathTown:", "@mc1:", "@text1:<Bf"Humanist521BT-Roman">", "@obit:", "<Bf"Humanist521BT-Roman">", "<$f$>", "@text1:", "@boxDeathName:", "<$f$>"}
set targetFile to (choose file)
set obitText to (read file targetFile)
set endTxt to (get eof of targetFile)
set newText to (munge obitText starting at offset 0 ending at (endTxt) from mungeList to "" with repetition)
open for access file targetFile with write permission
write newText starting at 0 to file targetFile
close access file targetFile
i don’t know if this really fixes the problem, but i worked around it by using the following example. the script i need this for doesn’t write to a text file anyway, so this was just to test things. is there an easy way to replace all the original text in a text file after it’s munged?
workaroud:
set mungeList to {"@boxDeathTown:", "@mc1:", "@text1:<Bf"Humanist521BT-Roman">", "@obit:", "<Bf"Humanist521BT-Roman">", "<$f$>", "@text1:", "@boxDeathName:", "<$f$>"}
set targetFile to (choose file)
set newName to (targetFile & "_new.txt") as text
set obitText to (read file (targetFile as text) from 1 to eof)
set endTxt to (get eof of targetFile) as integer
set newText to (munge obitText from mungeList to "" with repetition)
open for access file newName with write permission
write newText to file newName
close access file newName
This problem actually has nothing to do with akua’s munge: it’s actually about how files get written to by Standard Addition’s ‘write’ command.
What you’re doing is overwriting an existing file with new content. Trouble is, your new content is shorter than what was there before, and you’ve forgotten to set the EOF [end of file] accordingly. The write command can only adjust EOF upwards: it doesn’t adjust it downwards - if you want a shorter file you have to set the EOF yourself.
e.g. If your file was originally 12 bytes long and you overwrote this from 0 with a 9-byte long string, you’d still have a 12-byte long file, NOT a 9-byte long one as you might expect:
XXXXXXXXXXXX [before]
YYYYYYYYYXXX [after]
So either you set the EOF to 9 after you’ve written your new data [not recommended], or you simply set it to 0 before you start [the standard practice]. Like this:
set mungeList to {"@boxDeathTown:", "@mc1:", "@text1:", "@obit:", "", "<$f$>", "@text1:", "@boxDeathName:", "<$f$>"}
set targetFile to (choose file)
set obitText to (read file targetFile)
set newText to munge obitText from mungeList to ""
open for access file targetFile with write permission
set eof file targetFile to 0 --[IMPORTANT]
write newText starting at 0 to file targetFile
close access file targetFile
Because ‘write’ can increase the EOF it might seem natural to expect it to decrease it too, but it doesn’t (and indeed there are very good reasons for this). But it does make this one of those counter-intuitive ‘gotchas’ that everyone trips over sooner or later. Ah well, I think that’s what’s called a “learning experience”…
HTH
has