– ±--------±--------±--------±--------±--------±--------±--------±--------+
(*
Filename : simple_date.scpt
Author : Bill Hernandez
Location : Plano, Texas
Updated - Thursday, July 16, 2009 ( 8:51 PM )
Note :
I decided to write a more useful demo using the date, time, error handler, etc.
This is an example on how to get the date and time with one line
and a simple way to use the date while writing to the error log.
All my errors use something like :
set this_error to "[4753] Error: " & error_number & ". " & error_message & return
The numbers start with an arbitrary 4 digit number and represent the exact location
in the code where the error occurred. When you read the log, it pinpoints exactly
where the error took place in the code.
Every time an "on error" is used I increment the number in its brackets. In fact
I have a script that reads the page and auto sequences them, once I provide
the dialog a start number. That way I don't get error location number duplication.
In a large script it really comes in handy.
This error is not very helpful, since the error could be anywhere :
"Error: -2753. The variable some_undefined_variable is not defined."
Once we add an error position [4xxx], we don't have to worry about line number, etc.
This is more useful : shows the error date, error location, error number, and error message
[2009.07.16](09:33PM) [4753] Error: -2753. The variable some_undefined_variable is not defined.
[2009.07.16](09:33PM) [4754] Error: -1708. «script» doesn't understand the some_undefined_function message.
Hope this is helpful...
*)
– ±--------±--------±--------±--------±--------±--------±--------±--------+
property divider : “--------------------------------------------------------------”
global path2me, parent_dirpath, this_date, path_delim, project_name, project_path, project_logname, log_dirpath, log_filepath, default_timeout
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on run
if (init_globals()) then
display dialog “Today is …” default answer this_date
—> 2009.07.16
try
set x to some_undefined_variable -- generate an error
on error error_message number error_number
set this_error to "[4753] Error: " & error_number & ". " & error_message & return
my send2log(this_error)
end try
try
set x to my some_undefined_function() -- generate another error
on error error_message number error_number
set this_error to "[4754] Error: " & error_number & ". " & error_message & return
my send2log(this_error)
end try
end if
my show_me("All Done...", "")
my open_log()
end run
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on get_date()
return (do shell script “date +\[%Y.%m.%d\]\(%I:%M%p\)”)
end get_date
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on send2log(message)
try
do shell script (("echo " & quote & this_date & quote & space & quoted form of message) & " >> " & log_filepath)
on error error_message number error_number
set this_error to quote & this_date & quote & space & "[4752] Error: " & error_number & ". " & error_message & return
tell application (path to frontmost application as text)
display dialog this_error
end tell
end try
end send2log
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on init_globals()
try
set this_date to my get_date()
set default_timeout to 3 – no of seconds for display dialog to give up
set project_name to “simple_date.scpt”
set project_logname to “simple_date.log”
set path_delim to "/"
set prefs_dirpath to (do shell script "echo ~/as_storage/as_prefs")
set log_dirpath to (do shell script "echo ~/as_storage/as_logs")
set log_filepath to log_dirpath & path_delim & project_logname
set path2me to POSIX path of (path to me as string)
set parent_dirpath to do shell script ("dirname " & quoted form of path2me)
-- project_path could be different from path2me but for this demo, I will set them both the same
set project_path to path2me
my check_log()
return true
on error
return false
end try
end init_globals
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on check_log()
try
– check to see if the log file exists, if it doesn’t an error will be generated
– and we will create the log directories, and the log file
set f to alias (POSIX file (log_filepath))
on error error_message number error_number
set s to “”
set s to s & divider & return
set s to s & "project name : " & project_name & return
set s to s & "project date : " & this_date & return
set s to s & "project path : " & project_path & return
set s to s & "project log : " & log_filepath & return & divider
do shell script ("mkdir -p " & quoted form of prefs_dirpath)
do shell script ("mkdir -p " & quoted form of log_dirpath)
do shell script (("echo " & quoted form of s) & " > " & log_filepath)
set this_error to "[4751] Error: " & error_number & ". " & error_message & return & return & "Creating a new log..." & return & return
tell application (path to frontmost application as text)
display dialog this_error
end tell
end try
end check_log
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on open_log()
do shell script "open " & log_filepath
tell application “Console”
activate
end tell
end open_log
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on show_me(msg, time_out)
if (time_out is “”) then
set time_out to default_timeout
end if
display dialog (msg) buttons {“OK”} default button {“OK”} giving up after time_out
end show_me
– ±--------±--------±--------±--------±--------±--------±--------±--------+