Re: Finder - List - Question (long hopefully but good demo)
Re: Finder - List - Question (long hopefully but good demo)
- Subject: Re: Finder - List - Question (long hopefully but good demo)
- From: Bill Hernandez <email@hidden>
- Date: Wed, 11 Apr 2007 22:42:59 +0000
On Apr 10, 2007, at 9:57 PM, Oakley Masten wrote:
Any ideas?
Thanks
Oakley
(*
I wrote this for you today, and it has some fairly nice stuff in it,
for dealing with text, lists, displaying small and large messages
nicely ( automatically is the best dialog depending upon the size
of the message), some simple error handlers, etc. plus a bunch of
re-usable routines that might be handy for you...
It also has some debugging flags to test error handlers, and send
messages to the log. I have some much better stuff for handling
logging, but didn't have time to do that now.
I was going to add the ability to save the data to a plist, and
retrieve it in order to make it persistent, and editable, but I got
tired. Maybe I'll get to it sometime this week...
Most software usually provides errors in this form, which makes it
hard to figure out where it occurred. It only tells me the kind of
error it was...
An error of type -1234 has occurred.
error number -1234
You will notice that I structure my messages so that they include
something like :
property bh_project_no : 4301
set str_prompt to " [" & bh_project_no & "] (101) " & my
bh_msg_GetMessageNo(8500)
if (bh_debug_me) then -- OVERRIDE THE PREVIOUS LINE
if (bh_debug_test_error_handlers) then
set str_prompt to " [" & bh_project_no & "] (101) " & my
bh_msg_GetMessageNo(8502)
end if
end if
set str_msg to errorMsg & return & "error number " & errorNo
bh_msg_Show(str_prompt, str_msg, true)
Which gives an error message, which tells me where in my code it took
place, and also tells me the kind of error it was
[4301] (101) Ooops, ran into an unexpected error...
An error of type -1234 has occurred.
error number -1234
When I look at common log, the 4301 tells me which project was involved
and the (101) is the location within the code, Every new message
gets a 3 digit number, and at the end I do a search on "([\d]{3,3})
and sequence them
HERE ARE SOME OF THE PROPERTIES YOU CAN CHANGE
property bh_debug_me : true -- normally set to false
property bh_debug_test_error_handlers : true -- normally set to false
( true forces errors to test the error handlers)
property bh_debug_msg_stays_open : 10 -- no of seconds until dialog
automatically closes
property bh_debug_insert_clipping_and_update_page_automatically : not
bh_debug_me -- not used here
property bh_copy_messages_to_clipboard : true
property bh_send_errors_to_log : true
-- if the message is longer than, listed here, show it in a choose
from list dialog
property bh_msg_display_dialog_max_size : 200
Hope it is useful,
Best Regards,
Bill Hernandez
Plano, Texas
*)
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_title()
(*
Author : Bill Hernandez
ProjectNo : 000
ProjectName : ADD_TO_LIST
FileName : add_to_list.txt
FilePath : ~/scripting/400_AS_Studio/current_projects/
add_to_list.txt
Version : 1.0.0
VersionDate : [ 2007_04_11 ]
Created : Wednesday, April 11, 2007 ( 5:37 PM )
Updated : Wednesday, April 11, 2007 ( 5:40 PM )
*)
end script_title
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_notes()
(*
INSERT_CONTENT_DESCRIPTION_HERE
*)
end script_notes
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_about_this_framework()
(*
-- [1000] General purpose properties used with most projects
-- [2000] Properties required to load, and use any of the libraries.
-- [3000] Properties specific to this project, not needed by the
libraries.
-- [4000] ass - handlers - Application events
-- [5000] ass - handlers - plist_prefs - project generic settings
-- [5200] ass - handlers - plist_prefs - project specific settings
-- [5400] ass - handlers - plist_prefs - bbedit specific settings
-- [6000] ass - handlers - Specific to this project, will not be
added to the libraries.
-- [7000] ass - handlers - Generic, that can later be added to, and
used from the library
-- [7000] ass - handlers - Generic, that are not best run from a
library, such as the error, and log handlers
-- [7000] ass - handlers - Generic, that call interface items and
cannot easily run from a library
-- [8000] ass - handlers - Required by every project that does not
use libraries
-- [9000] ass - handlers - Required to verify the framework, and
load the libraries
*)
end script_about_this_framework
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_function_names()
(*
*)
end script_function_names
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_misc_related()
(*
*)
end script_misc_related
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- [2970] Properties required for this project
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
property bh_debug_me : true -- normally set to false
property bh_debug_test_error_handlers : true -- normally set to false
property bh_debug_msg_stays_open : 10
property bh_debug_insert_clipping_and_update_page_automatically : not
bh_debug_me
property bh_copy_messages_to_clipboard : true
property bh_send_errors_to_log : true
-- if the message is longer than, listed here, show it in a choose
from list dialog
property bh_msg_display_dialog_max_size : 200
property bh_msg_display_list_title : "Sample Text Message -> Display
List..."
property bh_msg_display_list_button_ok : "Continue..."
property bh_msg_display_list_button_cancel : "-------->"
property bh_delim_text : ":"
property bh_delim_posix : "/"
property bh_delim_paragraph : return
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
property bh_aDefaultValues : {}
property bh_default_answer : return & return & return
property bh_project_no : 4301
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on run
tell me
-- +---------+---------+---------+---------+---------+---------+
-- GET SOME VALUES INTO A LIST
-- +---------+---------+---------+---------+---------+---------+
set str_prompt to "Enter codes separated by a return:"
set str_answer to bh_default_answer -- IF THERE ARE ANY DEFAULT
VALUES LOAD THEM IN
set aList to bh_msg_Add2List(str_prompt, str_answer)
-- +---------+---------+---------+---------+---------+---------+
-- SHOW WHAT YOU JUST ENTERED
-- +---------+---------+---------+---------+---------+---------+
set str_prompt to "These are great codes..."
set obj to aList -- SET THE OBJECT TO THE VALUES ENTERED INTO THE LIST
set show_msg_in_text_field to true
set force_msg_to_display_list to false
bh_msg_ShowMessage(str_prompt, obj, show_msg_in_text_field,
force_msg_to_display_list) -- parms (prompt, string or list)
-- +---------+---------+---------+---------+---------+---------+
-- SHOW A STANDARD MESSAGE YOU JUST GRABBED
-- +---------+---------+---------+---------+---------+---------+
set str_msg to bh_msg_GetMessageNo(9200) -- GRAB A LONG MESSAGE
-- +---------+---------+---------+---------+---------+---------+
-- SHOW IT UGLY
-- +---------+---------+---------+---------+---------+---------+
set str_prompt to "This is not a good way to show a long message,
when you click OK you will se a nicer display..."
bh_msg_Show(str_prompt, str_msg, false) -- ( 3 ) parms (prompt,
msg, show_msg_in_text_field)
-- +---------+---------+---------+---------+---------+---------+
-- SHOW IT NICE
-- +---------+---------+---------+---------+---------+---------+
set str_msg to str_msg & return & return & "<[ ISN'T THIS MUCH
NICER THAT THE DISPLAY DIALOG. ]>"
set str_prompt to "A little info about \"choose from list\". ( It
is better than \"display dialog\" for displaying large messages... )"
set obj to str_msg
set show_msg_in_text_field to true
set force_msg_to_display_list to false
bh_msg_ShowMessage(str_prompt, obj, show_msg_in_text_field,
force_msg_to_display_list) -- ( 4 ) parms (string, string or list,
boolen, boolen)
-- +---------+---------+---------+---------+---------+---------+
-- FORCE SMALL MESSAGE TO LIST DISPLAY
-- +---------+---------+---------+---------+---------+---------+
bh_msg_ShowMessage("", "( 1 ) Force small message to list
display..." & return & return, false, true) -- ( 4 ) parms (string,
string or list, boolen, boolen)
bh_msg_ShowMessage("", "( 2 ) Let the message be handled
automatically..." & return & return, false, false) -- ( 4 ) parms
(string, string or list, boolen, boolen)
set str_prompt to bh_msg_GetMessageNo(9004)
bh_msg_Show(str_prompt, "All done...", false) -- ( 3 ) parms
(prompt, msg, show_msg_in_text_field)
end tell
end run
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_msg_Add2List(str_prompt, str_answer)
tell me
if (str_prompt = "") then
set str_prompt to "Enter one or more values:"
end if
set s to ""
set s to s & return & return
set s to s & "( 1 ) After each entry hit the RETURN key" & return
set s to s & "( 2 ) When done hit the ENTER key" & return
set str_prompt to str_prompt & s
set aResults to {}
try
if ((str_answer = "") or (str_answer = bh_default_answer)) then
if (bh_aDefaultValues is not {}) then
set delim to bh_delim_paragraph
set str_answer to bh_string_Join(bh_aDefaultValues, delim)
end if
end if
set aResults to paragraphs of text returned of (display dialog
str_prompt default answer str_answer)
set bh_aDefaultValues to aResults
set theNo to missing value -- set to ( 0, missing value, or some
arbitrary number to generate the error)
my bh_debug_GenerateAnError(theNo) -- theNo is not defined here so
trapping for the "if (theNo = missing value) then" will take care of
it, and establish a default value"
on error errorMsg number errorNo
if (errorNo = -128) then -- user cancelled
if (bh_debug_me) then
set str_prompt to "So, you decided to cancel..."
set str_msg to errorMsg & return & "error number " & errorNo
bh_msg_Show(str_prompt, str_msg, true)
end if
else -- real error
set str_prompt to " [" & bh_project_no & "] (101) " & my
bh_msg_GetMessageNo(8500)
if (bh_debug_me) then -- OVERRIDE THE PREVIOUS LINE
if (bh_debug_test_error_handlers) then
set str_prompt to " [" & bh_project_no & "] (101) " & my
bh_msg_GetMessageNo(8502)
end if
end if
set str_msg to errorMsg & return & "error number " & errorNo
bh_msg_Show(str_prompt, str_msg, true)
end if
end try
return aResults
end tell
end bh_msg_Add2List
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_msg_ShowMessage(str_prompt, obj, show_msg_in_text_field,
force_msg_to_display_list)
(*
CALLED :
bh_msg_ShowMessage(str_prompt, obj, show_msg_in_text_field,
force_msg_to_display_list)
str_prompt <-- ("" or a valid string)
obj <-- (list or a string)
show_msg_in_text_field <-- (true or false) -- > display dialog
str_prompt default answer obj
force_msg_to_display_list <-- (true or false)
if (class of obj is a string) and (length of obj is less than
bh_msg_display_dialog_max_size) then
bh_msg_Show(str_prompt, obj, show_msg_in_text_field) will be
called, and
obj will display in a display dialog str_prompt default answer obj
otherwise
obj will display in a choose from list dialog with no selection
required
end if
if(force_msg_to_display_list is true) then even a short message can
be forced to display in a list
*)
tell me
set error_str to ""
set obj_class to get class of obj
if ((obj_class = string) or (obj_class = Unicode text)) then
if ((length of obj > bh_msg_display_dialog_max_size) or
(force_msg_to_display_list)) then
set delim to bh_delim_paragraph
set obj to bh_string_Split(obj, delim)
set obj_class to list
if (str_prompt = "") then -- this has to go here so it passes an
"" string down to bh_msg_Show(str_prompt, obj, true)
set str_prompt to "TODAY IS : " & bh_date_GetFullDate(current
date) -- "User Message..."
end if
end if
else
if (str_prompt = "") then -- this has to go here so it passes an
"" string down to bh_msg_Show(str_prompt, obj, true)
set str_prompt to "TODAY IS : " & bh_date_GetFullDate(current
date) -- "User Message..."
end if
end if
if (obj_class is list) then
if ((count of obj) > 0) then
choose from list obj ¬
with title bh_msg_display_list_title ¬
with prompt str_prompt ¬
OK button name bh_msg_display_list_button_ok ¬
cancel button name bh_msg_display_list_button_cancel ¬
with multiple selections allowed and empty selection allowed
end if
else if ((obj_class = string) or (obj_class = Unicode text)) then
bh_msg_Show(str_prompt, obj, show_msg_in_text_field)
else
set str_prompt to " [" & bh_project_no & "] (102) " & my
bh_msg_GetMessageNo(9000)
set str_msg to my bh_msg_GetMessageNo(8501)
set error_str to (str_prompt & return & str_msg & return)
bh_log_Send(error_str & obj)
bh_msg_Show(str_prompt, str_msg, show_msg_in_text_field)
end if
bh_clip_Send(error_str & obj)
end tell
end bh_msg_ShowMessage
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_msg_Show(str_prompt, obj, show_msg_in_text_field)
tell me
set bOK to bh_msg_display_list_button_ok
set error_str to ""
if (str_prompt = "") then
set str_prompt to "TODAY IS : " & bh_date_GetFullDate(current
date) -- "User Message..."
end if
set obj_class to get class of obj
if (obj_class is list) then
if ((count of obj) > 0) then
choose from list obj ¬
with title bh_msg_display_list_title ¬
with prompt str_prompt ¬
OK button name bh_msg_display_list_button_ok ¬
cancel button name bh_msg_display_list_button_cancel ¬
with multiple selections allowed and empty selection allowed
end if
else if ((obj_class = string) or (obj_class = Unicode text)) then
if (show_msg_in_text_field) then
display dialog str_prompt default answer (obj & return & return)
buttons {bOK} default button {bOK}
else
display dialog (str_prompt & return & return & obj) buttons {bOK}
default button {bOK}
end if
else
set str_prompt to " [" & bh_project_no & "] (103) " & my
bh_msg_GetMessageNo(9000)
set str_msg to my bh_msg_GetMessageNo(8501)
set error_str to (str_prompt & return & str_msg & return)
bh_log_Send(error_str & obj)
if (show_msg_in_text_field) then
display dialog str_prompt default answer str_msg buttons {bOK}
default button {bOK}
else
display dialog (str_prompt & return & return & str_msg) buttons
{bOK} default button {bOK}
end if
-- DO NOT USE THE CALL BELOW, POSSIBLE RECOURSIVE PROBLEM IF USED
-- bh_msg_Show(str_prompt, str_msg, show_msg_in_text_field)
end if
bh_clip_Send(error_str & obj)
end tell
end bh_msg_Show
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_clip_Send(obj)
tell me
if (bh_copy_messages_to_clipboard) then
set error_str to ""
set str_result to ""
set obj_class to get class of obj
if (obj_class is list) then
if ((count of obj) > 0) then
set delim to bh_delim_paragraph
set str_result to bh_string_Join(obj, delim)
end if
else if ((obj_class is string) or (obj_class is Unicode text)) then
set str_result to obj
else
set str_prompt to " [" & bh_project_no & "] (104) " & my
bh_msg_GetMessageNo(9000)
set str_msg to my bh_msg_GetMessageNo(8501)
set error_str to (str_prompt & return & str_msg & return)
set str_result to (error_str & obj)
bh_log_Send(str_result)
bh_msg_Show(str_prompt, str_msg, show_msg_in_text_field)
end if
set the clipboard to str_result
end if
end tell
end bh_clip_Send
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_log_Send(obj)
if (bh_send_errors_to_log) then
log "--> AH, NUTS ! : " & obj
end if
end bh_log_Send
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_string_Split(str, delim)
set old_delim to get AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set aList to every text item of str
set AppleScript's text item delimiters to old_delim
return aList
end bh_string_Split
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_string_Join(aList, delim)
set str to ""
repeat with theItem in aList
set str to str & theItem & delim as string
end repeat
return str
end bh_string_Join
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_date_GetFullDate(theDate)
if ((theDate = "") or (theDate = missing value)) then
set theDate to current date
end if
set DS to date string of (theDate)
set TS to time string of (theDate)
tell application "Finder"
set TS to ((get characters 1 thru -7 of TS) & (get characters -3
thru -1 of TS)) as string
end tell
return (DS & " (" & TS & ")")
end bh_date_GetFullDate
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_msg_GetMessageNo(theNo)
tell me
-- UNEXPECTED ERROR
if (theNo = 8500) then -- my bh_msg_GetMessageNo(8500)
return "Ooops, ran into an unexpected error..."
-- UNEXPECTED CLASS
else if (theNo = 8501) then -- my bh_msg_GetMessageNo(8501)
return "Ooops, ran into an unexpected class..."
-- INTENTIONAL TEST ERROR
else if (theNo = 8502) then -- my bh_msg_GetMessageNo(8502)
return "Ooops, ran into an intentional testing error..."
-- FORMATTED DATE AND TIME
else if (theNo = 9000) then -- my bh_msg_GetMessageNo(9000)
return "Today is : " & bh_date_GetFullDate(current date) -- "Date
Message..."
-- SINGLE LINE WELCOME (ODD number is for "Welcome", EVEN number
is for "So Long")
else if (theNo = 9001) then
set username to (last word of (path to current user folder as text))
return "Welcome " & username & ": " & bh_date_GetFullDate(current
date) -- "UserName and Date Message..."
-- SINGLE LINE SO LONG (ODD number is for "Welcome", EVEN number
is for "So Long")
else if (theNo = 9002) then
set username to (last word of (path to current user folder as text))
return "So Long " & username & ": " & bh_date_GetFullDate(current
date) -- "UserName and Date Message..."
-- TWO LINE WELCOME (ODD number is for "Welcome", EVEN number is
for "So Long")
else if (theNo = 9003) then
set username to (last word of (path to current user folder as text))
return "Welcome " & username & ": " & return & bh_date_GetFullDate
(current date) -- "UserName and Date Message..."
-- TWO LINE SO LONG (ODD number is for "Welcome", EVEN number is
for "So Long")
else if (theNo = 9004) then
set username to (last word of (path to current user folder as text))
return "So Long " & username & ": " & return & bh_date_GetFullDate
(current date) -- "UserName and Date Message..."
else if (theNo = 9200) then
return "choose from list: Choose one or more items from a list
(defined in: StandardAdditions.osax)
choose from list list of number or string -- a list of numbers and/
or strings to display
[with title string] -- the dialog window title
[with prompt string] -- the prompt to be displayed in the
dialog box
[default items list of number or string] -- a list of items to
initially select (an empty list if no selection)
[OK button name string] -- the name of the OK button
[cancel button name string] -- the name of the Cancel button
[multiple selections allowed boolean] -- Allow multiple items
to be selected?
[empty selection allowed boolean] -- Can the user make no
selection and then choose OK?
Result: list of number or string -- the list of selected items"
else
return ("Ooops, ran into an unexpected error..." & return & " [" &
bh_project_no & "] (105) Invalid number called...")
end if
end tell
end bh_msg_GetMessageNo
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_debug_ShowDebugMessage(str, howLong)
if (howLong = 0) then
set howLong to bh_debug_msg_stays_open
end if
if (bh_debug_me) then
tell me
display dialog str buttons {"Continue..."} default button
{"Continue..."} giving up after howLong
end tell
end if
end bh_debug_ShowDebugMessage
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_debug_ShowNormalMessage(str, howLong, theApp)
if ((theApp = "") or (theApp = missing value)) then
tell me
if (howLong = 0) then
set howLong to bh_debug_msg_stays_open
end if
display dialog str buttons {"Continue..."} default button
{"Continue..."} giving up after howLong
end tell
else
tell application theApp
if (howLong = 0) then
set howLong to bh_debug_msg_stays_open
end if
display dialog str buttons {"Continue..."} default button
{"Continue..."} giving up after howLong
end tell
end if
end bh_debug_ShowNormalMessage
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_debug_GenerateAnError(theNo)
if ((theNo = "") or (theNo = 0) or (theNo = missing value)) then
set theNo to -1234 -- generate an arbitrary error number
end if
if (bh_debug_me) then
if (bh_debug_test_error_handlers) then
error number theNo
end if
end if
end bh_debug_GenerateAnError
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden