Problem: Extra "empty" characters before and after

Hi,

I am porting an old script from OS9 to 10.3 and I have noticed that I get extra blank characters (“”) before and after strings i read from a file.

This is probably someting Apple has changed but I need to know how to get rid of them.

Example, i have a file with the following content: abc|123|def
I read the file with: set fileData to read fileName using delimiter “|”
When I try to display the contents of for example: (text item 2 of fileData) as string
I get the following error:
Can’t make {“”, “123”, “”} into a string.

There is a hidden ascii char inside the “”.

What’s up with this and can I safely just drop the first and last items in the list of my so called string?

The hidden character was the Ascii character 0 (= Null) if it’s any help.

Hm… Well, first off, your variable fileData should already be a list, so using “text item” isn’t really kosher:


    set fileData to read fileName using delimiter "|" --> should be { "abc", "123", "def" }
    item 2 of fileData --> should be "123"

I’m not sure why you are seeing what you’re seeing, but I’d like to offer an alternative: just forgo the various paramters of the ‘write’ command altogether and do all string-processing in AppleScript:


    set dataString to read theFile --> just read it all in (if the file isn't too big)

    set oldTids to AppleScript's text item delimiters -- save

    set AppleScript's text item delimiters to "|"

    set dataList to dataString's text items

    set AppleScript's text item delimiters to oldTids -- restore

    set theItem to item 2 of dataList --> "123"

It seems that the problem is that there seems to be text delimiters (Null) between every character I write to disk, so the problem is not when I read, but when i save the file.

I am using the following code to save my file:

open for access theFile with write permission
write (theData) to theFile
close access theFile

E.g. the string “abc|123” becomes …
aNULLbNULLcNULL|NULL1NULL2NULL3NULL”

…when I save it do disk.

So I tried using write (theData as string) and the NULL’s dissapeared.

Thanks for your help anyway! (It would be nice to know if things are supposed to work like this or not, though)

OH!!! Sorry, I should have realized… it’s a Unicode text problem. Before saving the string to disk, (from where ever you’re getting it from), force it to plain text:


set plain_text to AsText( unicode_text )

on AsText(str)
    --coerce Unicode or other text to styled string to plain text
    try
         return (text of ((str as string) as record)) as string
    end try
    return str -- if plain text to begin with
end AsText

The above version of AsText() is courtesy of Paul Berkowitz.

If you need the text saved and read back as Unicode, I think you should be able to do this:


set unicode_char to «data utxt8082» as Unicode text --> "?"

set unicode_text to "Hello " & unicode_char & " World" --> "Hello ? World"

set fref to open for access file "Mac HD:temp.txt" with write permission
try
	write (unicode_text) to (fref) as "utxt"
	close access fref
on error e number n from f to t partial result p
	try
		close access fref
	end try
	error e number n from f to t partial result p
end try

set savedString to read file "Mac HD:temp.txt" as "utxt" --> "Hello ? World"

That’s because, by default, 10.3.x uses Unicode as the standard text encoding. The NULL you’re seeing is the extra Unicode byte for each character.

The simplest fix is to ensure you match how you write the file with how you read it. Either write the data ‘as string’, or read it ‘as unicode text’. An even better solution is to write it out in native AppleScript data formats (e.g. a list) which preserves all object classes and properties (e.g, boolean, numerics, even record names). The drawback to this approach is that you can’t use other applications to read/manipulate the data outside of your app, but this may or may not be a problem.