Hi there,
At the moment I’m trying to read a couple of files into Applescript. The thing is it appears to ignore the EOF marker for them. I think they are in the UNIX format which might be the issue. Certainly I have tried to open a test file that I generated in TextEdit and it opens ok. These other files seem to also open ok in a viewer, and are just plain text. When open in AppleScript the EOF result is something like 4.135e5 which is definately a lot bigger than the file :o. So is there a certain method to mark the files as a different format?
Just in case I’ve posted a summary of the code.
set FILE_LOC to alias (((path to desktop folder) as text) & "test")
set read_file to open for access FILE_LOC
set marker to (get eof FILE_LOC)
set output_text to (read read_file as string from 1 to marker)
parse XML ouput_text
close access read_file
Thanks.
4.135e5 is not BIG, but a real number… (=413500)
You can integer-ize it using this simple code:
set marker to marker as integer
Anyway, if you are going to read the file from first character to last character, you don’t need all such code… This is good enough:
set output_text to (read FILE_LOC)
Other ways to transform a really BIG number to integer (in text mode, of course):
- For BIG but not-incredible BIG files:
word 1 of (do shell script "wc -c " & quoted form of POSIX path of FILE_LOC)
- For any real number:
ndigits(bigRealNumber)
to ndigits(bigNumber) --> credits to Paul Berkowitz
set bigNumber to bigNumber as string
if bigNumber does not contain "E+" then return bigNumber
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"E+"}}
try
set {basicReal, powersOfTen} to {bigNumber's text item 1, (bigNumber's text item 2) as integer}
on error -- e.g. Python strings have lower-case "e", tids case-sensitive
set AppleScript's text item delimiters to {"e+"}
set {basicReal, powersOfTen} to {bigNumber's text item 1, (bigNumber's text item 2) as integer}
end try
set AppleScript's text item delimiters to {"."}
try
set {integerPart, decimalPart} to basicReal's {text item 1, text item 2}
set decimalSign to "."
on error
set AppleScript's text item delimiters to {","}
set {integerPart, decimalPart} to basicReal's {text item 1, text item 2}
set decimalSign to ","
end try
set AppleScript's text item delimiters to tids
set n to count decimalPart
if powersOfTen is greater than or equal to n then
repeat (powersOfTen - n) times
set decimalPart to decimalPart & "0"
end repeat
set bigNumber to integerPart & decimalPart
else
set bigNumber to integerPart & decimalPart
set bigNumber to text 1 thru (powersOfTen + 1) of bigNumber --& decimalSign & text (powersOfTen + 2) thru -1 of bigNumber
end if
return bigNumber
end ndigits
Cheers 8)
Thanks for the above code it has been useful in reducing my script length, however the number given is much bigger than the file size should be (it’s about 1000 characters or so) as such I think it is misreading the file. Either the end of file isn’t there or the end of file is in a different format to what the script is expecting and it passes stright over it.
Thanks again though, the code you have given will come in useful, I’ve been having a lot of stress about this script, and I haven’t even gotten to the processing part yet .
Actually you were entirely right about it being the right length. I just wasn’t accessing the files correctly.
Thanks and doh