Newbie Q - on Scope

I want to

– open file
– write to file
– close file

but to extend this I want to
– open file
– in safari
– get data and format it
– write to file
– release safari
– close file

How do I get the write statement to be valid inside the Safari tell block?

Or do I have to collect all data then write it

Or do I have to release safari before writing

Or can I create a tell block around the write statement

… Or does annyone know something better?

Virtuall DC (‘:rolleyes:’)

Did you try it in your script editor? It should have worked just fine.

The “write” command is a part of the Standard Additions scripting addition. All scripting addition commands are global in scope, they are available everywhere, in and out of tell blocks.

“Release?” What language are you coming to us from? :wink:

There are long standing problems with working with the read/write commands. Assuming you have the memory, (under OS X, you almost always have the memory), the best advice is to read an entire file into a variable, process it as a string, then write it back out again.

set myFile to alias "path:to:a:file"

--	The "open for access" command isn't really needed
--	when you're just reading:
set str to read myFile

... process the string str

try -- always be careful about a file opened with write permission
	--	The "open for access" command IS needed for writing:
	set openedFile to open for access myFile with write permission
	--	If the processing of str made it smaller than it was,
	--	the contents of myFile needs to be zeroed out:
	set eof openedFile to 0
	write str to openedFile
	close access openedFile
on error e number n from f to t partial result p
		close access openedFile -- try closing it
	end try
	--	re-throw the error:
	error e number n from f to t partial result p
end try

— Yes I am a database person trying to convert old HTML.

---- And when I went back to look at my write statement, I was trying to write to the variable that was the filename not the opened file…

— And you answered some other questions I was wondering about.

…Virtual DC

I cannot get files opened outside of a tell block to be recognized within one.

tell app
open file
open app document
…loop on data
muck with data from app
write data to file
…end loop
close flie
end tell

Oh well.

Hi virtualdc,

The standard additions read/write commands are not used for reading/writing to documents opened in applications (i.e. they are not application commands). To write to an application, the app neads to be scriptable and contain commands that allow read/write.