Resetting Variables/Properties between runs...

My script is writing some HTML code to a text document, but every time I run it I end up with some extra code at the end of the intended document that is unnecessary. It’s valid code that is properly placed inside the document but is duplicated at the end of the document. It isn’t always the same text and it isn’t always the same length of text so I’m wondering what I might be missing.

Right now I do the following:

set html_code to html_code & “ some data start…some data end”

write html_code to “index.html”


Again, everything works fine except that I end up with some duplicated text from “some data…some data end” after the flag in the document.

I’ve set up properties to reset all of my variables but still no luck. But still no luck.

Any insights? Is Applescript holding onto the data in cache between instances of running the script?

Assuming you’re using the normal 'open for access" command, you need to reset the EOF (End of File) marker so that the data in the file is correctly overwritten.

For example, if the file contains the text:

1234567890

and you write to file “ABCD”, the file ends up as

ABCD567890

because you’ve just written 4 characters without resetting the end of file.

you have two options, one is to set the EOF before you write new data:

set outputFile to open for access file ":path:to:filename" with write permission
set EOF outputFile to 0 -- reset the file to 0 length
write html_code to outputFile
close access outputFile

or, alternatively, set the EOF to the last byte you write:

set outputFile to open for access file ":path:to:filename" with write permission
write html_code to outputFile
set EOF outputFile to (count of characters in html_code) -- set the file length to number of chars written
close access outputFile

There’s a decent write handler on the AppleScript site. :slight_smile:

http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.11.htm