Reading and writing file problem

Ok, I am creating a program that helps a preference file. Now of course, you don’t edit the whole thing, just parts. And not all of these parts are standard “0,1” or “true, false”, some are to the users preference, like the a characters name, which leads me to my problem.
This program is designed to help the user out by displaying specific information from the file, and then being able to edit that specific part. I have heard about the “write ‘something’ to thefile starting at 200”, but since the user can provide information of their own, it will mess up the 200-byte point in the file. So is there any way that I can fix this, like start writing at string “myserver=”?

Thanks
~Balthamos

There’s no way to do it in quite the way you describe, not least of which is that you can’t guarantee that the data you’re writing is exactly the same size (number of characters) as what was previously there, meaning you’ll either have extraneous characters or, even worse, will overwrite following values.

The easier solution would be to read the entire file in as a list (using ‘paragraphs of’…), change the appropriate values, then write all the data back (overwriting the entire file contents).

Alternatively, if all your users will be under Mac OS X, an even better way would be to use the OS-level ‘defaults’ command which writes XML-formatted preferences file, just like you see in your ~/Library/Preferences file. Using this standard format means users are able to edit the file using other XML-compatible utilities, and ‘defaults’ takes care of writing the correct bits of data to the right place in the file.

So say I was to go about rewriting the 5th line. would it be kinda like this? I have figured out how to read, but not write to the specific paragraph


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Not quite, but close.

To change the fifth line of the file:


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Note, you should add some error checking to make sure you read in the data correctly, and there’s at least the number of items you expect (you can’t set the fifth item of a 3-item list, for example).

Ok that code sorta works, here is what I put.

Now this code works exactly as it should, save one thing. It screws up the lines. Real bad. It just smashes everything together and does that till it gets to the line i edited , then It gives 2 lines their own row, and then continues smashing. I don’t know what happened but whatever it was, I really messed it up.


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code

You omitted the part of the code that deals with text item delimiters.

This is required in order to convert the AppleScript list into a return-delimited text block that matches the original file format.

Try re-copying my script, changing the file path and line number as appropriate.

Additionally, just to be safe, you should change the line:

set item 231 of currentPrefs to “ServerName=” & “test”

to:

set item 231 of currentPrefs to (“ServerName=” & “test” ) as text

Just to make sure it gets added as a singly text item and not a list-within-a-list.

Works perfect now. I thought the TIDs were for imputing multiple strings, so I disregarded it. Anyway, thanks for all your help!

~Balthamos