Since it appears that Applescript can’t load files larger than 32k (may be wrong on the true limit), I need to add some code into my script that will check the log file I’m loading and, if it’s larger than, say, 28k, trim it down to 10k and save it back out.
Since I already have the file and path stored in fileTarget, I thought I would start by running “get info for file fileTarget”. The results screen shows the file properties as expected except instead of showing “size:19572” I see “size:1.9572E+4”
How do I change this so I can get the size of the file in “normal” bytes.
Also, any pointers on how to grab the last 10k of a file?
I see jj has already posted the two most pertinent answers to this query. Apologies for the overlap here.
I’m not sure that there is any file size limit in AppleScript per se. A couple of my scripts regularly handle text files as long as 72K. In Mac OS 8 and 9, there is a 32K limit on the amount of script text that can be loaded into Script Editor.
The scientific notation, of course, only affects the display of the number, not its actual value. If you don’t need to look at it, you don’t need to do anything. With numbers up to about 535000000 (522460K), you can simply coerce them to integer and they’ll be shown in long notation anyway. (The OS 9.2.2 Standard Additions dictionary says that the ‘size’ of a file information record is an integer anyway, but in fact it’s either a long integer or a real.):
5.35E+8 as integer
--> 535000000
For larger numbers, you could try my numToStr script, which returns any AppleScript number as a string in long notation.
However, note that the size of a file may actually be more than the length of the text it contains. Getting the file’s eof may be a better guide to the length of the text.
You can in fact use negative indices with the ‘read’ command, rather as you can with text commands:
-- If the contents of the log file > 28K
if (get eof file "Path:to:my:log file") > 28672 then
set fref to open for access file "Path:to:my:log file" with write permission
try
-- Read the last 10K
set last10K to read fref from -10240
-- Adjust the text here to ensure that
-- last10K begins with a whole log entry
-- 'Empty' the file
set eof fref to 0
-- Write the shortened text back to it
write last10K to fref
on error msg
display dialog msg
end try
close access fref
end if
As far as I remember, you could read more text simply increasing the memory partition of your applet (eg, 2MB memory if you are planning read 2MB of text?).
However, it was better reading and manipulating the text in chunks, so you don’t find some auxiliar problems.
If the target app has a keyboard command for quitting, you could probably use an osax to simulate the keystrokes. To see what’s available, choose “Scripting Additions” from the “Select A Section” menu at the top of this page.