Creating a log file in TextEdit ....

Hi,

At every cycle of an idle handler, I want it to write to a log file to see the progression of variables. I already do it with another app called “Indigo” but I would like to do it with TextEdit.

What is the simplest start point for doing this ?

Regards.

Robert

Actually, I believe that the simplest is to use do shell script and echo:


set log_Path to (path to documents folder as text) & "ScriptLog.txt"
set log_String to  --Code to build log string
do shell script "echo " & log_String & " >> " & (quoted form of (POSIX path of log_Path)) 

Just set something like this up as a handler and call it whenever you want something logged. The >> appends whatever your log_String is to what is already there in the file.

Good luck,

This is great … Thank you Craig :):):):slight_smile:

Another variant I use. My troubleshooting logs can be kinda complex and I wanted them to be easier to make human-readable…and turn on and off at will (so I can leave logging routines in, but turned off, on finished scripts).

Script handliy creates a generic Log folder if needed, and a new file if one doesn’t exist (else appends existing one). Only drawback is it isn’t self-cleaning, but I always manually trim/clear my troubleshooting logs. Also returns whether it ran correctly or not (useful for troubleshooting as well).

First, declare a property to globally turn it on and off (on, in this example):

-- debugging on?
property g_debug : true

Then, properties for log location:

--basic file path and names
property g_home_folder_path : path to home folder
property g_log_file_name : "My Script's Log.txt"

Then this handler:

--Log Entry Generation
-- With help from StephanK of MacScripter
-- http://bbs.applescript.net/viewtopic.php?pid=76607#p76607
--
on logMe(log_string, indent_level)
	if g_debug is true then --allows turning the debugger on and off so my logMe's can be left in final version
		set log_target to (g_home_folder_path & "Library:Logs:" & g_log_file_name) as text
		try
			set log_file_ref to open for access file log_target with write permission
			repeat indent_level times
				write tab to log_file_ref starting at eof
			end repeat
			write ((log_string as text) & return) to log_file_ref starting at eof
			close access log_file_ref
			return true
		on error
			try
				close access file log_target
			end try
			return false
		end try
	end if
end logMe

Then typical handler calls:

No indent:

my logMe("Sometext here", 0)

Indent 1 tab:

my logMe("Whoa!", 1)

Indent two tabs and use variable:

my logMe("Sometext here: " & someVariable, 2)

Thank you. Kevin …