Here is a little log handler I have been working on which kind of does what you suggest. It uses display dialog to "log" events in real time if the script isn't running in an editor and optionally logs to a log file. " log" is handy for debugging in the editor and logging to a file is handy for an unattended app or an Agent which can't communicate with the user. The "Display Dialog Log" adds a handy alternative. (Sorry if this is too long to post here).
(*
Log Event Handler
Options:
-- Log to Apple Event Log. Date will not be added.
-- Display Dialog Trace. Date will not be added.
-- Log to a Log File: Either ".log" or ".txt". Date will be prepended.
Format:
logEvent(theSender,theMessage,theLogFile)
-- theSender: a text string. If "", only theMessage and Date will be logged
-- theMessage: a text string. If "", only theSender and Date will be logged
-- theLogFile: the file to write to
-- Option 1: Posix Path or HFS Path
-- Option 2: "" for skip logging to file
Globals
-- pDialogTrace, pDialogTimeOut
Trace Level controlled by calling routine eg:
if pTraceLevel > 3 then logEvent(theSender,theMessage,theLogFile)
the higher the pTraceLevel, the more detail in the trace.
pTraceLevel = 0 is no trace
*)
property pDialogTrace : true
property pDialogTimeOut : 20
property pMyScriptName : "Log Event"
-- Tests:
on run
set handlerPrefix to "logEventRunTest"
-- No Sender, No Log File
set theSender to ""
set theMessage to "No Sender"
set theLogFile to ""
logEvent(theSender, theMessage, theLogFile)
-- No Message, No Log File
set theSender to "No Message"
set theMessage to ""
set theLogFile to ""
logEvent(theSender, theMessage, theLogFile)
-- No Sender or Message, No Log File
set theSender to ""
set theMessage to ""
set theLogFile to ""
logEvent(theSender, theMessage, theLogFile)
-- Sender and Message, No Log File
set theSender to "The Sender"
set theMessage to "The Message"
set theLogFile to ""
logEvent(theSender, theMessage, theLogFile)
-- Log File Class Error
set theSender to handlerPrefix
set theMessage to "Log File Class Error"
set theLogFile to 5
logEvent(theSender, theMessage, theLogFile)
-- Posix File Specification
set theSender to handlerPrefix
set theMessage to "Posix File Specification"
set theLogFile to "/Users/sjc/testLog.txt"
logEvent(theSender, theMessage, theLogFile)
-- HFS File Specification
set theSender to handlerPrefix
set theMessage to "HFS File Specification"
set theLogFile to "MacBook Pro:Users:sjc:testLog.txt"
logEvent(theSender, theMessage, theLogFile)
end run
on logEvent(theSender, theMessage, theLogFile)
global pDialogTrace, pDialogTimeOut, pMyScriptName
set handlerPrefix to "logEvent"
-- First write to Apple Event Log
if theSender is "" and theMessage is "" then
set theText to "Empty Sender and Message"
else if theMessage is "" then
set theText to theSender
else if theSender is "" then
set theText to theMessage
else
set theText to theSender & ": " & theMessage
end if
log theText
-- Next write to the Display Dialog Trace
if pDialogTrace then
if theSender is "" and theMessage is "" then
set theText to "Empty Sender and Message"
else if theMessage is "" then
set theText to theSender
else if theSender is "" then
set theText to theMessage
else
set theText to theSender & ": " & return & return & theMessage
end if
display dialog theText with title "Trace" giving up after pDialogTimeOut
end if
-- Finally, write to the Log File
if theLogFile is "" then return
-- theLogFile must be a POSIX path or HFS Path
if class of theLogFile is not text then
set theText to (theSender & ":" & return & "logEvent Fatal Error:" & return & ¬
return & "Class of theLogFile is " & (class of theLogFile) as text) & return & ¬
return & "It must be either a POSIX or HFS path."
tell me to activate
display dialog theText with title pMyScriptName
return
end if
-- Convert path if neccessary
if the first character of theLogFile is "/" then set theLogFile to POSIX file theLogFile
-- Create the date & time stamp
set dateCurrent to current date
set textYear to (year of dateCurrent) as string
set textYear to characters 3 thru 4 of textYear
set textMonth to (month of dateCurrent as number)
if textMonth < 10 then set textMonth to "0" & textMonth as string
set textDay to (day of dateCurrent)
if textDay < 10 then set textDay to "0" & textDay
set textDate to textMonth & "/" & textDay & "/" & textYear as string
set textTimeStamp to textDate & " " & time string of dateCurrent
-- Construct the log entry
if theSender is "" and theMessage is "" then
set theText to "Empty Sender and Message"
else if theMessage is "" then
set theText to theSender
else if theSender is "" then
set theText to theMessage
else
set theText to theSender & tab & theMessage
end if
set theText to textTimeStamp & tab & theMessage & return
-- display dialog "Text to write to log:" & return & return & theText
-- Write the entry to the log file
try
set logFID to open for access theLogFile with write permission
write theText to logFID starting at eof
close access logFID
on error errMsg number errNum
set theText to theSender & ":" & return & "logEvent Fatal Error:" & return & ¬
return & "Unable to write to Log File:" & return & ¬
return & "Error " & errNum & ": " & errMsg
tell me to activate
display dialog theText with title pMyScriptName
end try
end logEvent