G'day scripters
I've got a requirement to save a whole years worth of hourly data.
At the moment, the data is saved daily on an hourly basis as a comma delimited array, then the totals for the 24 hours are saved, the file reset, and starts over. I need to store the days date, and the daily array, for every day of the year.
I've found that if I simply try and add each days new {date,array} to the end of the file, it won't work; I have to read the whole of the saved array, add to it, and then write the whole thing back to the file, otherwise getting the array as a list only reads the first days entry.
Am I doing the only thing possible (which is slow), or is there a method of simply adding to the file on a daily basis, without reading/adding/saving the whole array. I would much prefer to stay saving and reading as a list, it's faster than my previous method of saving individual items as a long comma delimited array (but I'd like it faster still).
My present testing script is below, it takes a minute to run. If I could simply add to the file each day, it would take 3 seconds. It's not much time each day, but it's frustrating having to do it this way.
I've attached the appropriate desktop folder and file so anyone interested can try the script out.
Regards
Santa
BTW: I've deliberately left a name extension off the file name, as under Mountain Lion, this locks the blank file, so information can't be altered except by scripting.
property MailManagerFolderPath : ((path to desktop) & "Mail Manager Folder") as text
property PathToDesktop : path to desktop
property MailManagerDesktopFolder : "Mail Manager Folder"
property YearlyTally : ""
property DailyTally : ""
property theZeroedDate : date
set theZeroedDate to (current date) - (time to GMT) - days
set time of theZeroedDate to 0
set theZeroedDate to date ("1-1-2012")
set c to current date
set DailyTally to my ReadFile3(" RunningTally")
repeat 365 times
set the time of theZeroedDate to 0
my mainloop()
set theZeroedDate to theZeroedDate + days
end repeat
set YearlyTally to my ReadFile2("2012 RunningTally")
say (current date) - c
on mainloop()
my writeYearlyTally("2012 RunningTally", theZeroedDate, DailyTally)
end mainloop
on writeYearlyTally(FileName, writeitem1, writeitem2)
set TheFileName to PathToDesktop & MailManagerDesktopFolder & ":" & FileName as text
try
set tempString to my ReadFile2(FileName)
set end of tempString to {writeitem1, writeitem2, return}
set fRef to (open for access file TheFileName with write permission)
set eof fRef to 0
write tempString as list to fRef
close access fRef
on error errmsg
try
close access fRef
end try
display dialog errmsg
end try
end writeYearlyTally
on ReadFile2(TheFileName)
try
set TheFileNameTemp to (MailManagerFolderPath & ":" & TheFileName) as text
set WholeList to read file TheFileNameTemp as list
return WholeList
on error errmsg
return {}
end try
end ReadFile2
on ReadFile3(TheFileName)
try
set TheFileNameTemp to (MailManagerFolderPath & ":" & TheFileName) as text
set WholeList to read file TheFileNameTemp using delimiter ","
return WholeList
on error errmsg
return {}
end try
end ReadFile3