Event Logger for AppleScript

Is there any way for an app to create a text file and then log all events/errors, etc. into that text file? If possible, I want to do this without modifying my existing commands, preferrably just by adding some lines or code or something.

wiz:

You simply need to identify a file (using a variable is easiest), then create a handler that accepts a string, and send whatever log comments you want to the handler:

set log_file to (path to desktop as text) & "AppLog.txt"

to WriteLog(text4Log)
	set right_Now to (current date) as text
	set wri to open for access file log_file with write permission
	write (right_Now & return & text4Log & return) to wri starting at eof
	close access wri
end WriteLog

This handler will write the time on a line just before the line of log text you send it. Also, don’t forget to set the log_file as a global variable.

Good luck,

Sorry I’m new to AppleScript.

  1. How do I set log_file as a global variable?
  2. Where would I insert the code to write to the file?
  3. Would this auto log all events or do I have to specify individually what is logged?

Some examples would help :slight_smile: Thank you very much for the help, I really appreciate it!

wiz:

Well, you have come to the right place. Let me suggest a few items to get you started:

Links:
This is the first article in a series that can get you up to speed quickly just on AppleScript.
This is the initial article in a series that can teach about handlers.
This article is one of many by Ben Waldie on getting started with AS Studio & XCode.

Books:
Great introduction to all things AppleScript.
Good job of explaining the basics of AS Studio.

You should also spend some time in our Links Section.

Anyway, here is a modified version of the earlier script, showing how to utilized the handler I put together. Just run it and read the text file on your desktop:

set log_file to (path to desktop as text) & "AppLog.txt"
global log_file

WriteLog("Program Started....")

tell application "iTunes"
	set a to count every track in first library playlist
	my WriteLog("Found this number of tracks:" & (a as text))
	try
		set b to count every track in playlist "Elvis"
		my WriteLog("Found this number of Elvis tracks:" & (a as text))
	on error errMsg number errNum
		my WriteLog("Error: " & errNum & space & errMsg)
	end try
end tell

WriteLog("Program Ended...")
to WriteLog(text4Log)
	set right_Now to (current date) as text
	set wri to open for access file log_file with write permission
	write (right_Now & return & text4Log & return) to wri starting at eof
	close access wri
end WriteLog

Good luck,

Thank you very much! That works. However I’m wondering if there is a way to make an autologger like the event logger in Script Editor, so that it auto logs everything without me inserting the code where I want it to log?

Hi,

no, there is no way.
The event logger of Xcode or Script Editor is just a kind of debugging tool, which is included in the programming environment.
If you could enable and disable an event logger in every program, everybody could enable the logger and easily disassemble your code.
I guess this is not intended :wink:

One problem I noticed…
the line " my WriteLog(“Found this number of Elvis tracks:” & (a as text))"
should be " my WriteLog(“Found this number of Elvis tracks:” & (b as text))

Browser: Firefox 2.0.0.14
Operating System: Mac OS X (10.4)