I have a problem with certain characters when reading a file as:
set fileRef to (open for access file theFilePath)
set theText to read fileRef as text
close access fileRef
The problem is reading the characters: left & right brackets ‘[ ]’ and comma.
These characters “sometimes” inserts a line-feed (ASCII 10) while reading these characters.
I’m using OSX 10.2.6
The file read was exported from FileMaker Pro as ‘tab separated text’
Got any ideas why & how I can get rid of the extra line-feeds?
Thanks,
Steven
Here is background info:
The first 16 bytes of the file viewed via Path Finder’s Hex table:
00000000 61 61 61 5b 62 62 62 5d 0a 63 63 63 0a 64 64 64 aaa[bbb].ccc.ddd
Applying this AppleScript to the ‘theText’ [above]
set asASCII to “”
repeat with i from 1 to 16
set asASCII to asASCII & (ASCII number of character i of theText) & ", "
end repeat
I don’t think the problem is with the read file command.
If you look at your Path Finder’s output:
There is a hex 0a in the middle, which corresponds to decimal 10, or the Line Feed character. Therefore the line feed is in the file before read file gets to it.
Indeed, if you run the code with a static string you get the result you expect:
set theText to "aaa.[bbb].ccc.ddd"
set asASCII to ""
repeat with i from 1 to 16
set asASCII to asASCII & (ASCII number of character i of theText) & ", "
end repeat
--> "97, 97, 97, 46, 91, 98, 98, 98, 93, 46, 99, 99, 99, 46, 100, 100, "
Is the field in FileMaker a repeating field? Are there any other peculiarities about the field? Have you checked the field itself to make sure there’s no extraneous characters in it?
I’m not sure but I think there might be a couple of cases where it’s necessary to open a file for read operations. If this is so, I offer it only for the sake of clarity.
[for double integer] -- the number of bytes to read from current position; if omitted, read until the end of the file
[from double integer] -- starting from this position; if omitted, start at last position read from
If you need the script to remember where you are in reading a file then I guess you could open it, read in from x, close it, etc. but you could also just keep track of a variable:
I like this better because I don’t have to add try blocks to my scripts that remember to close any open documents (in case of errors or crashes) since I don’t open the documents unless I’m writing to them in the first place.
Jon
[This script was automatically tagged for color coded syntax by Script to Markup Code]
Actually, you don’t need to keep track yourself… AppleScript will do this for you.
When you ‘open for access’, AppleScript automatically keeps track of a pointer to the current position in the file. As you read, the pointer automatically increments to match the amount of data read.
Consider the following:
-- create a dummy file so we have something to work with:
set theFile to open for access file "Macintosh HD:private:var:tmp:tempfile" with write permission
set eof theFile to 0
write "aaaabbbbccccddddeeeeffff" to theFile
close access theFile
-- now read it in in chunks
set theFile to open for access "Macintosh HD:private:var:tmp:tempfile"
repeat
try
set sometext to read theFile for 4
display dialog sometext
on error
-- end of file?
exit repeat
end try
end repeat
You’ll note each subsequent ‘display dialog’ displays the next 4 characters read from the file, without you having to keep track of it yourself.
Hi
I perhaps will say a great silly thing, but why read several times the same file whereas we can memorize the text in a variable (unless the text is too long).
Then, it is enough to cut out it with a simple loop, for example:
set Txt to read file (choose file of type "TEXT")
--> "aaaabbbbccccddddeeeeffff"
set {Nbr, Long, Lst} to {4, (length of Txt), {}}
repeat with Bcl from 1 to Long by Nbr
set Fin to (Bcl + Nbr - 1)
if Fin > Long then set Fin to Long
set end of Lst to text Bcl thru Fin of Txt
end repeat
return Lst --> {"aaaa", "bbbb", "cccc", "dddd", "eeee", "ffff"}
Camelot, that was my whole point: I don’t want to use “open for access” because I don’t want the overhead of open files in case of mishaps.
Fredo, yes, you are right you could do that–but if the file is particularly large, you may not want the overhead of passing around a huge variable when all you really want is just a small part of it.