My AppleScript needs to save to txt-file log few times a day some data. What is best way to do this?
This is some code I used to log errors in a batch process. I deleted out all the specific stuff so you can see the basic outline. You should be able to pull what you need from here:
global TotalCount
global TotalNotProcessed
global ErrorLogFile
global FileVersion
tell application "Finder"
activate
set chosenFolder to (choose folder with prompt "Process all files in what folder??")
set TotalCount to 0
set TotalNotProcessed to 0
set FileVersion to "_1"
set VerNum to 1
repeat while (exists file ("ErrorLog.log" & FileVersion))
set VerNum to VerNum + 1
set FileVersion to ("_" & VerNum)
end repeat
set ErrorLogFile to (make new file at desktop with properties {name:("ErrorLog.log" & FileVersion)})
set ErrorLogFile to ErrorLogFile as text
my folderprocess(chosenFolder)
activate
display dialog ("Total files processed: " & TotalCount & return & "Number of errors: " & TotalNotProcessed) buttons "OK" default button "OK" giving up after (3 * days)
end tell
on folderprocess(chosenFolder)
--repeat to go through all the files in the folder
try
--Do some stuff. I deleted all my specific code to give you the basic log file part
on error
set TotalNotProcessed to TotalNotProcessed + 1
display dialog ("Error") buttons " " giving up after 3
try
set the OpenFile to open for access file ErrorLogFile with write permission
write ("ERROR" & return) to the OpenFile starting at eof
close access file ErrorLogFile
on error
try
close access file ErrorLogFile
set the OpenFile to open for access file ErrorLogFile with write permission
write ("ERROR" & return) to the OpenFile starting at eof
close access file ErrorLogFile
on error
try
close access file ErrorLogFile
display dialog ("Problem writing error log! Log may not be accurate!") buttons " " giving up after 5
end try
end try
end try
end try
set TotalCount to TotalCount + 1
--end repeat
end folderprocess