Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create a simple AppleScript logging function System
As a sometime AppleScript user, I've generally used the display dialog function to display variables when developing. I'm not an AppleScript expert by any means, and it usually takes me a lot of trial and error to get things working the way I want. The problem with all those potential display dialogs is that they interrupt the flow of the script, and you have to go around commenting them out or removing them afterwards. Yes, I know about the Event log in the Script Editor, but it's not always terribly clear at which point in the script something occurred. It's also nice to have custom messages from time to time. My idea was instead of popping up a dialog box, I would write data to an external file.

First, I wanted to make an external AppleScript file for storing commonly used functions. I've never done this before, but eventually discovered how. First create a new AppleScript and add these four lines:
on log_event(themessage)
  set theLine to (do shell script 
   "date  +'%Y-%m-%d %H:%M:%S'" as string) 
   & " " & themessage
  do shell script "echo " & theLine & 
   " >> ~/Library/Logs/AppleScript-events.log"
end log_event
Save this as Common code.scpt in your user's Library -> Scripts folder. You can close that file now and forget about it. Next take any AppleScript you have developed, or start a new one, and add this line to the top of the script:
set commonScript to load script alias 
 ((path to library folder from user domain as string) 
 & "Scripts:Common code.scpt")
Now whenever you want to record an event, you just use the following line instead of display dialog:
log_event("Put your string here") of commonScript
Apart from stopping interruptions, this has the advantage that you needn't comment out these lines afterwards (as long as it's for your own use). This is also a good way to monitor scripts which you may have running automatically via cron. You can record both successes and failures in any script you like, so if something goes wrong in your absence, then you can look back at the log (saved in your user's Library/Logs folder, and called AppleScript-events.log), and find out where and when things went awry.

There might well be a built-in method for doing things like this, but like I said I'm a hit-and-miss AppleScript developer, and I'm not aware of anything myself. I stand to be corrected...
  • Currently 2.43 / 5
  You rated: 4 / 5 (7 votes cast)

Create a simple AppleScript logging function | 6 comments | Create New Account
Click here to return to the 'Create a simple AppleScript logging function' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create a simple AppleScript logging function
Authored by: foo4thought on Dec 20, '04 11:14:46AM

You'll want to be able to turn on/off the logging. Do it by changing the log_event handler to something like this:

on log_event(themessage, yesDo)
if yesDo then
set theLine to (do shell script
"date +'%Y-%m-%d %H:%M:%S'" as string)
& " " & themessage
do shell script "echo " & theLine &
" [code]>>[/code] ~/Library/Logs/AppleScript-events.log"
end if
end log_event

Use a property or global in the calling script(s) to toggle logging on & off.
property doLogTF: false

and change the call to the handler to look like this:

on log_event(themessage, doLogTF)

[ Reply to This | # ]
Create a simple AppleScript logging function
Authored by: leenoble_uk on Dec 20, '04 11:42:29AM

Good idea.

I actually just found the built in way of doing this this morning. I thought about the possibility of using an osaxen rather than a common code script but it's beyond my capabilities. But in the process I opened the system osax file and found the debugstr command which writes a string to the console log. Doh!

I still prefer my script though because it keeps the applescript events in a separate file.

So, I said ... well, I can't actually remember exactly what I said. But it was one of the most enormously cruel and frighteningly witty put downs ever.

[ Reply to This | # ]
Toggling the logging function
Authored by: bbumgarner on Dec 20, '04 12:14:18PM

Actually, since you have defined "DoLogTF" as a property, it is therefore, global and you don't need to modify the parameters sent to the handler at all. (That is, to the best of my knowledge -- I could be wrong.) Granted this makes this routine ON or OFF for the entire script.

The advantage of your method is that you can locally turn on and off logging. The disadvantage is if you are working on large scripts it can be just as tedious to find and change each logging statement as it is finding each display dialog statement.

Don't get me wrong, I like the idea of being able to toggle the logging on and off. AND I plan on using it in my own work. I have a Applescript Studio application in the works right now that is over 700 lines long with 20-30 different handlers. This will make debugging easier.

[ Reply to This | # ]
Create a simple AppleScript logging function
Authored by: sdevore on Dec 20, '04 12:37:31PM
I actually have been using a growl ( Growl ) for some reporting when I run appleScripts in debugging mode and some that use growl all the time for notification. I have a similar script that has an optional logLevel parameter that I use so that I can filter the log messages in the console viewer. Sam D

Foolish consistency is the hobgoblin of little minds -rwe

[ Reply to This | # ]

Use the shell command "logger"
Authored by: klktrk on Dec 20, '04 03:05:49PM

Instead of writing directly to the log file, just use the logger command to write to the console and log. From the logger man file:

"LOGGER(1) BSD General Commands Manual LOGGER(1)

logger - make entries in the system log

logger [-is] [-f file] [-p pri] [-t tag] [message ...]

Logger provides a shell command interface to the syslog(3) system log


-i Log the process id of the logger process with each line.

-s Log the message to standard error, as well as the system log.

-f file Log the specified file.

-p pri Enter the message with the specified priority. The priority may
be specified numerically or as a ``facility.level'' pair. For
example, ``-p'' logs the message(s) as informational
level in the local3 facility. The default is ``user.notice.''

-t tag Mark every line in the log with the specified tag.

message Write the message to log; if not specified, and the -f flag is
not provided, standard input is logged.

The logger utility exits 0 on success, and >0 if an error occurs.

logger System rebooted

logger -p local0.notice -t HOSTIDM -f /dev/idmc

[ Reply to This | # ]
contextual menu add-in
Authored by: sinjin on Dec 22, '04 01:22:02AM
Love it. I've been wanting something like this for a long time. Should work nicely until I can justify buying Script Debugger.

Here are some tweaks that I think are helpful. This allows you to "control-click" or "right-click" in your Script Editor document to insert the line of code for logging a variable. It assumes you've copied the variable of interest to the clipboard first. Also keeps track of the variable name as well as value and adds a breaking line between runs to make reading the output a little easier.

Start off with a "templateScript.scpt", i.e. the common code to begin all your works in progress:

--Common code for logging:
set logScript to load script alias 
	((path to library folder from user domain as string) 
		& "Scripts:[AppleScript]:logger.scpt")
property logIt : true --toggle logging
if logIt then --adds breaking line to output between runs
	do shell script "echo " & "--" & " >> ~/Library/Logs/AppleScript-events.log"
end if
--Paste following after "theVar" you are observing:
--Logger("theVar", theVar , logIt) of logScript

The logging applescript itself, "logger.scpt" saved to whatever path you use in the code above:

on Logger(theVar, theVal, logIt)
	if logIt then
		set theLine to (do shell script 
			"date  +'%Y-%m-%d %H:%M:%S'" as string) 
			& " " & theVar & " " & theVal
		do shell script "echo " & theLine & 
			" >> ~/Library/Logs/AppleScript-events.log"
	end if
end Logger
Finally the contextual menu add-in, "loggerTag.scpt", that lets you take any variable you've saved to the clipboard and insert the logging call for it wherever you like. Save this script in "~/Library/Scripts/Script Editor Scripts/" then restart Script Editor.

set theVar to «class ktxt» of ((the clipboard as text) as record)
set theText to return & "Logger(\"" & theVar & 
	"\", " & theVar & " , logIt) of logScript" & return
tell application "Script Editor"
	tell front document
		set the contents of the selection to theText
	end tell
end tell
One more thing that would be nice is an applescript that will go through a document and delete the logger lines to clean up the code when you are all finished (ha! like that ever happens!). Some other day.

[ Reply to This | # ]