Getting an Applescript to save to it's own preferences file

Hi. I’ve been writing small (very small) IRC AppleScripts, and I’ve got to the stage where I’m going to need to have the script save certain preferences that have been set to a file.

What I’d like it to do would be to first (on load) check to see if the preferences are in ‘folder x’, if they are locatable in folder x, then it opens them and sets the properties contained within accoridngly. But if it can’t find the preferences it creates a new file with a set of default preferences. This file doesn’t have to a preferences file, I’d be quite happy with it being a simple text file that has lines of text in it.

As you’ve probably guess this is way beyond my comprehension. IRC scripting is relatively basic stuff, generally I’v eonly learnt the basics to do with it so, tryign to save something to a file doesn’t seem to register with my brain, I just can’t figure it out.

I’d very much appreciate the help. Thanks.

You can use this handler (only tested under OS X):


writeTo
writes given data to given file as given mode, and returns an alias to the file

Parameters:
f: file path, alias, posix path (the file to be written, can exist or not)
stuff: stuff to write to the file
mode: how to write the data (text, list, record, �class utf8�, C string, etc)

Example:
to writeTo(f, stuff, mode)
	set f to f as Unicode text
	if f does not contain ":" then set f to POSIX file f as Unicode text
	
	set fRef to (open for access file f with write permission)
	try
		set eof of fRef to 0
		write stuff to fRef as mode
		close access fRef
	on error msg number n
		try
			close access fRef
		end try
		error msg number n
	end try
	alias f
end writeTo

If you script is not intended for OS X perhaps you want remove the line which prays “if f does not contain…” (though to handle posix paths).

To read the preference file, simply:

read file "path:to:file.txt" as list

(Use “as list” only if you wrote the file also “as list”, or remove it if you preferences are plain text)

Okay thanks, I’ll see if I can wrap my head around it. Thanks very much.