Add to the start and end of files

I have a TV Guide I’m trying to format into an XML document, and I’m having trouble writing a script…
I have a guide formatted like this:
6:00: TOASTED TV [G]
7:00: SCOOTER: SECRET AGENT [C,CC]
7:30: TOTALLY WILD [C,CC]
8:00: YAKKITY YAK [C]

I need to add this to the start of the document:

<?xml version="1.0" encoding="utf-8"?> BTVGuide http://about:blank BTVGuide Handmade http://blogs.law.harvard.edu/tech/rss en-us *Todays Date* *Todays Date*

And then add each individual line TV show in like this:

6:00 TOASTED TV [G] Sat, 14 Jan 2006 17:10:59 +0400 600-toasted-tv-g

And then finish it off with:

I tryed with Text Edit, but couldn’t figure out how to do it, so I tryed doing it with “Tex-Edit” a much more scriptable version of Text Edit. This is as far as I got:
tell application “Tex-Edit Plus”
if exists window 1 then tell window 1
repeat with x from 1 to (number of lines)
copy “” to before line x
copy “” to after line x
end repeat
end tell
end tell

But that just adds to the start of every line.
If anyone could post a script, or give me a point in the right direction, that’d be great.

Hi,

You don’t need a text editor to do this.

Try this script:

set myText to "Text of document."
set myFile to ((path to desktop as text) & "Read This.txt" as text)
WriteToFile(myFile, myText, true)

--handler to write text to text file with flag to clear text before
on WriteToFile(theFile, theText, clearFLag)
	try
		open for access file theFile with write permission
	end try
	if clearFLag is true then
		set eof of file theFile to 0
	end if
	write theText & return to file theFile starting at eof
	try
		close access file theFile
	end try
end WriteToFile

WriteToFile is a handler (or sub-routine) that takes a file path, text string and true/false value and writes the text to the file at the file path. If the file doesn’t exist yet it is created. If the flag is true it will clear the text before writing, otherwise the text is added to the end of the file.

You could build up your XML document in a text variable and write it to file using the handler.

set myStartText to "Some stuff "
set myMiddleText to "to stick "
set myEndText to "together."

set myFirstTry to myStartText & myMiddleText & myEndText
display dialog myFirstTry

set mySecondTry to myStartText & return & myMiddleText & return & myEndText
display dialog mySecondTry

To get the current date as text in the right format try:

set myNow to do shell script "date '+%a, %d %b %Y %T %z'"
display dialog myNow
--> Sat, 14 Jan 2006 09:46:48 +0000

Best wishes

John M

Thanks for the reply, but I don’t understand how to use it in my case. I have 30 or so lines in a document, and I need to add content infront of it, and after it, as well as inbetween the lines. Could you clarify a bit more?

Okay, it just clicked, I understand now :cool:

I’ll post the finished script soon!

Hi,

You have a text document with the listings and you want to write them as a XML document to another file.

Is this the text you want written? When asked choose your source file:

set timeStamp to "*NOW*" -- Use the date from my last post
set myItems to "<?xml version=\"1.0\" encoding=\"utf-8\"?>" & return & "<rss version=\"2.0\">" & return & "<channel>" & return & "<title>BTVGuide</title>" & return & "<link>http://about:blank</link>" & return & "<description>BTVGuide</description>" & return & "<generator>Handmade</generator>" & return & "<docs>http://blogs.law.harvard.edu/tech/rss</docs>" & return & "<language>en-us</language>" & return & "<pubDate>" & timeStamp & "</pubDate>" & return & "<lastBuildDate>" & timeStamp & "</lastBuildDate>" & return
set myText to read (choose file)
set myParagraphs to paragraphs of myText
repeat with myParagraph in myParagraphs
	set myItems to myItems & "<item>" & return & "<title>" & myParagraph & "</title>" & return & "<pubDate>" & timeStamp & "</pubDate>" & return & "<guid isPermaLink=\"false\">600-toasted-tv-g</guid>" & return & "</item>" & return
end repeat
set myItems to myItems & "</channel>" & return & "</rss>"

display alert "The text to write to file" message myItems
-- Use the WriteToFile handler to write myItems to file. 

John M

(Sorry, I didn’t see your last post when I posted this)

Okay, thanks a lot. I was just finishing up my script, but mine was much longer and did it all in a roundabout way. I found out the guid stuff wasn’t nescessary so I removed that. Here’s the completed script. Thanks a lot!

set timeStamp to (do shell script "date '+%a, %d %b %Y %T %z'")

set myItems to "<?xml version=\"1.0\" encoding=\"utf-8\"?>" & return & "<rss version=\"2.0\">" & return & "<channel>" & return & "<title>BTVGuide</title>" & return & "<link>http://about:blank</link>" & return & "<description>BTVGuide</description>" & return & "<generator>Handmade</generator>" & return & "<docs>http://blogs.law.harvard.edu/tech/rss</docs>" & return & "<language>en-us</language>" & return & "<pubDate>" & timeStamp & "</pubDate>" & return & "<lastBuildDate>" & timeStamp & "</lastBuildDate>" & return
set myText to read (choose file)
set myParagraphs to paragraphs of myText
repeat with myParagraph in myParagraphs
	set myItems to myItems & "<item>" & return & "<title>" & myParagraph & "</title>" & return & "<pubDate>" & timeStamp & "</pubDate>" & return & return & "</item>" & return
end repeat
set myItems to myItems & "</channel>" & return & "</rss>"

display alert "The text to write to file" message myItems
set myText to myItems
set myFile to ((path to desktop as text) & "RSSFeed.txt" as text)
WriteToFile(myFile, myText, true)

--handler to write text to text file with flag to clear text before
on WriteToFile(theFile, theText, clearFLag)
	try
		open for access file theFile with write permission
	end try
	if clearFLag is true then
		set eof of file theFile to 0
	end if
	write theText & return to file theFile starting at eof
	try
		close access file theFile
	end try
end WriteToFile


:cool: