Re: Creating new file etc (re-wrote due to large bug)
Re: Creating new file etc (re-wrote due to large bug)
- Subject: Re: Creating new file etc (re-wrote due to large bug)
- From: Bill Hernandez <email@hidden>
- Date: Fri, 13 Apr 2007 17:56:27 +0000
On Apr 13, 2007, at 12:22 AM, Bill Hernandez wrote:
Gregory,
I found a huge bug in my code this morning so I rewrote the whole
thing and added a number of handy functions...
IT was late and I didn't test this portion, but this morning I did,
and sure enough, it was buggy...
SO I DECIDED TO RE-WRITETHE WHOLE THING...
Please read the notes below at [ on script_notes( ) ]...
THIS WAS BUGGY
if ((offset of "/" in path2check) > 0) then
if (first character of path2check is "~") then
set path2check to ((path to home folder from user domain) &
(characters 2 thru -1 of path2check)) as string
end if
set f_posixPath to path2check
end if
THIS FIXES THE way to trap for paths like "~/Documents/2007 Sales/
Projections/Just The Facts.txt"
if ((offset of "/" in path2check) > 0) then
if (first character of path2check is "~") then
-- for paths like "~/Documents/2007 Sales/Projections/Just The
Facts.txt"
try
-- IF home_dir_posix is defined, use it otherwise use the long
method after "display dialog w_error"
set path2check to (home_dir_posix & (characters 3 thru -1 of
path2check)) as string
on error error_message number error_number
-- IF home_dir_posix is not defined handle the error
set w_error to "Error (4321) : " & error_number & ". " &
error_message
set aErrors to aErrors & w_error & return
display dialog w_error
set path2check to (POSIX path of (path to home folder from user
domain) & (characters 3 thru -1 of path2check)) as string
end try
end if
set f_posixPath to quoted form of path2check -- This takes care of
spaces in the path
end if
-- IF the path is something like "HardDisk:Users:David:Documents:
2007 Sales:Projections:Just The Facts.txt"
-- IT skips over it and leaves it alone
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_title()
(*
Author : Bill Hernandez
ProjectNo : 4302
ProjectName : TEST_WRITE
FileName : test_write.txt
FilePath : ~/user_scripting/user/400_AS_Studio/current_projects/
function_names/MAIN_SCPT/test_write.txt
Version : 1.0.0
VersionDate : [ 2007_04_13 ]
Created : Friday, April 13, 2007 ( 12:49 AM )
Updated : Friday, April 13, 2007 ( 2:32 PM )
*)
end script_title
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_notes()
(*
( 1 ) I had originally written the particular write_to_file
(this_data, this_file, append_data)
handler you were using and had posted it in one of my messages,
but apparently it had
been modified by the time you got it, and it was buggy,
So I completely rewrote it and renamed it to bh_file_Write
(this_data, obj, append_data).
I dont use it anymore, since I now do all my writing via the do
shell script command, which
is much faster and I combine with a bh_data_CkDirs(...) function
that creates any folders
that do not exist prior to writing
( 1 ) THIS CORRECTS SOME BUGS with handler bh_file_Write(this_data,
obj, append_data)
where obj can be {text path, posix path (short, or long), alias
file}
( 2 ) Adds a handler to format dates like this --> Friday, April
13, 2007 (11:45 AM)
( 3 ) Separates the main code, from the test code, so you can
completely trun off the demo
part by setting the bh_debug_me property to false
( 4 ) Adds a handler that shows how to scroll the window for any
app that accepst a Cmd-UpArrow,
Cmd_DnArrow
( 5 ) Provides some reusable handlers
*)
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()
(*
on script_title()
on script_notes()
on script_about_this_framework()
on script_function_names()
on script_misc_related()
on bh_init_Paths()
on bh_init_Globals()
on bh_date_GetFullDate(theDate)
on bh_file_Write(this_data, path2check, append_data)
on bh_info_GetUserName()
on bh_msg_AllDone(msg, show_default_msg_also)
on bh_msg_ShowNormalMessage(str, howLong, theApp)
on bh_test_Write2File_Using_Multiple_References()
on bh_window_Scroll(theApp, whichWay)
on run
on main(which_choice)
*)
end script_function_names
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_misc_related()
(*
*)
end script_misc_related
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- [1020] SYSTEM PATHS FOR ANY PROJECT
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
property aErrors : {}
property bh_debug_me : true
property bh_debug_msg_stays_open : 10
property bh_text_delim : ":"
property bh_posix_delim : "/"
property bh_project_no : 4302
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- [1022] OBJECT REFERENCES (USED WITH APPLESCRIPT COMMANDS)
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
property startup_disk : missing value
property desktop_dir : (path to desktop from user domain)
property documents_dir : (path to documents folder from user domain)
property home_dir : (path to home folder from user domain)
property preferences_dir : (path to preferences folder from user domain)
property temp_dir : (path to temporary items folder from user domain)
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- [1024] TEXT PATHS (USED WITH APPLESCRIPT COMMANDS)
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
property startup_disk_text : missing value
property desktop_dir_text : (desktop_dir as text)
property documents_dir_text : (documents_dir as text)
property home_dir_text : (home_dir as text)
property preferences_dir_text : (preferences_dir as text)
property temp_dir_text : (temp_dir as text)
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- [1028] POSIX PATHS (USED WITH "DO SHELL SCRIPT" COMMANDS)
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
property startup_disk_posix : missing value
property desktop_dir_posix : (POSIX path of desktop_dir)
property documents_dir_posix : (POSIX path of documents_dir)
property home_dir_posix : (POSIX path of home_dir)
property preferences_dir_posix : (POSIX path of preferences_dir)
property temp_dir_posix : (POSIX path of temp_dir)
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- [1030] PATHS AND PROPERTIES FOR THIS PROJECT
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_init_Paths()
set returnValue to false
try
tell application "Finder"
set startup_disk to startup disk --> Alias Reference to the hard disk
end tell
set startup_disk_text to startup_disk as text -->"Hard_Drive_49:"
set startup_disk_posix to POSIX path of startup_disk_text --> "/"
set returnValue to true
end try
return returnValue
end bh_init_Paths
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_init_Globals()
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- INITIALIZE ANY GLOBALS THAT NEED TO BE SET AT RUNTIME HERE
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
set returnValue to false
try
set aErrors to {}
set returnValue to true
end try
return returnValue
end bh_init_Globals
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_file_Write(this_data, path2check, append_data)
(*
path2check can be a text path, an alias file reference, or a posix
path
*)
set f_posixPath to ""
try
set found_valid_choice to true
set which_class to class of path2check
if (which_class = alias) then
set path2check to (path2check as text)
else if ((which_class = string) or (which_class = Unicode text)) then
if ((offset of "/" in path2check) > 0) then
if (first character of path2check is "~") then
-- for paths like "~/Documents/2007 Sales/Projections/Just The
Facts.txt"
try
-- IF home_dir_posix is defined, use it otherwise use the long
method after "display dialog w_error"
set path2check to (home_dir_posix & (characters 3 thru -1 of
path2check)) as string
on error error_message number error_number
-- IF home_dir_posix is not defined handle the error
set w_error to "Error (4321) : " & error_number & ". " &
error_message
set aErrors to aErrors & w_error & return
display dialog w_error
set path2check to (POSIX path of (path to home folder from user
domain) & (characters 3 thru -1 of path2check)) as string
end try
end if
set f_posixPath to quoted form of path2check -- This takes care
of spaces in the path
end if
-- IF the path is something like "HardDisk:Users:David:Documents:
2007 Sales:Projections:Just The Facts.txt"
-- IT skips over it and leaves it alone
else
set found_valid_choice to false
end if
if (found_valid_choice) then
(*
set the target_file to the this_file as text
-- the variable (this_file) is undefined, you defined this_file
outside the scope of the function
*)
if (f_posixPath = "") then
set the open_target_file to open for access file path2check with
write permission
else
set the open_target_file to open for access POSIX file path2check
with write permission
end if
if append_data is false then
set eof of the open_target_file to 0
end if
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
else
set w_error to "Error (4322) : UnKnown class of path2check"
set aErrors to aErrors & w_error & return & path2check
display dialog w_error
return false
end if
on error error_message number error_number
set w_error to "Error (4323) : " & error_number & ". " & error_message
set aErrors to aErrors & w_error & return
display dialog w_error
try
if (f_posixPath = "") then
close access file path2check
else
close access POSIX file path2check
end if
end try
return false
end try
end bh_file_Write
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_date_GetFullDate(theDate)
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_info_GetUserName()
try
set username to (last word of home_dir_text)
on error error_message number error_number
set w_error to "Error (4324) : " & error_number & ". " & error_message
set aErrors to aErrors & w_error & return
display dialog w_error
set username to (last word of (path to current user folder as text))
end try
return username
end bh_info_GetUserName
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_window_Scroll(theApp, whichWay)
if (whichWay = "up") then
set theNo to 30
else
set theNo to 31
end if
activate application theApp
tell application "System Events"
tell process theApp
-- insert GUI Scripting statements here
keystroke (ASCII character theNo) using {command down} -- down
arrow key
end tell
end tell
end bh_window_Scroll
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_msg_AllDone(msg, show_default_msg_also)
set username to my bh_info_GetUserName()
set full_date to my bh_date_GetFullDate(current date)
set def_msg to "All done, so long " & username & "..." & return &
return
if (msg = "") then
set str to full_date & return & return & def_msg
else
if (show_default_msg_also) then
set str to full_date & return & return & msg & return & return &
def_msg
else
set str to full_date & return & return & msg & return & return
end if
end if
my bh_msg_ShowNormalMessage(str, 0, "me")
end bh_msg_AllDone
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_msg_ShowNormalMessage(str, howLong, theApp)
if ((theApp = "") or (theApp = "me") 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_msg_ShowNormalMessage
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- ON RUN -- KEEP ON RUN SIMPLE
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on run
tell me
if (bh_init_Paths()) then
if (bh_init_Globals()) then
main("some_choice")
if (bh_debug_me) then
bh_test_Write2File_Using_Multiple_References()
end if
set show_default_msg_also to true
my bh_msg_AllDone("", show_default_msg_also)
end if
end if
end tell
end run
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- FUNCTION MAIN
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on main(which_choice)
copy (the clipboard) to Clip_Board
--in the finished script I would set this_book to Clip_Board
(*
set Bookmaker_Folder to "home:Documents:ReferenceWorks Bookmaker:"
-- in the contest of a text path "home:Documents:ReferenceWorks
Bookmaker:"
-- the word home has no meaning, the OS has no clue what you are
referring to...
*)
try
set Bookmaker_Folder to (home_dir_text & "Documents:ReferenceWorks
Bookmaker:")
set Bookmaker_dir to alias Bookmaker_Folder
on error
set Bookmaker_Folder to (home_dir_text & "Documents:")
end try
set this_Book to "Once upon a time in Silicon Valley... Hello"
(*
set this_file to (((Bookmaker_Folder) as text) & "New Book")
-- it is already text
*)
set this_file to (Bookmaker_Folder & "New_Book.txt")
my bh_file_Write(this_Book, this_file, false) -- last parm is
append_doc <-- {true, false}
end main
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on bh_test_Write2File_Using_Multiple_References()
try
set theFileName to "TEST_" & bh_project_no & ".txt"
on error error_message number error_number
set w_error to "Error (4325) : " & error_number & ". " & error_message
set aErrors to aErrors & w_error & return
display dialog w_error
set theFileName to "TEST.txt"
end try
set username to my bh_info_GetUserName()
set full_date to my bh_date_GetFullDate(current date)
-- +---------+---------+---------+---------+---------+---------+
set do_append to " >> "
set do_overwrite to " > "
set qf_str to quoted form of ("Modified by : " & username & return
& full_date & return & return)
set qf_path to quoted form of (desktop_dir_posix & theFileName)
set runCmd to "echo " & qf_str & do_append & qf_path
do shell script runCmd
-- +---------+---------+---------+---------+---------+---------+
set aFilePaths to {}
copy ("~/Desktop/" & theFileName) to the end of aFilePaths --> short
posix path to desktop <[ "~/Desktop/" ]>
copy (desktop_dir_posix & theFileName) to the end of aFilePaths -->
normal posix path to desktop <[ "/Users/yourShortName/Desktop/" ]>
copy (desktop_dir_text & theFileName) to the end of aFilePaths -->
normal text path to desktop <
[ "HardDisk:Users:yourShortName:Desktop:" ]>
-- Notice the alias is last since it was already created
-- if you werent sure you could do something like this
try
copy (desktop_dir_text & theFileName) as alias to the end of
aFilePaths --> normal alias path to desktop <[ alias
"HardDisk:Users:yourShortName:Desktop:" ]>
on error
copy (desktop_dir_text & theFileName) to the end of aFilePaths -->
normal text path to desktop <
[ "HardDisk:Users:yourShortName:Desktop:" ]>
end try
set append_doc to true
repeat with thisPath in aFilePaths
set which_class to get class of thisPath
if (which_class = alias) then
set str to "The path passed to <[ my bh_file_Write(str, file_ref,
append_doc) ]> was :" & "alias " & (thisPath as string) & return &
return
else
set str to "The path passed to <[ my bh_file_Write(str, file_ref,
append_doc) ]> was :" & (thisPath as string) & return & return
end if
my bh_file_Write(str, thisPath, append_doc)
end repeat
set str to
"----------------------------------------------------------------------"
& return
my bh_file_Write(str, thisPath, append_doc)
try
tell application "Finder" to open desktop_dir
on error error_message number error_number
set w_error to "Error (4325) : " & error_number & ". " & error_message
set aErrors to aErrors & w_error & return
display dialog w_error
open (path to desktop from user domain)
end try
try
tell application "TextEdit"
open file (desktop_dir_text & theFileName)
set properties of window 1 to {bounds:{20, 30, 820, 630}}
end tell
set str to "( 1 ) Notice the TextEdit Window" & return & return &
"This shows the beginning of the document..."
my bh_msg_ShowNormalMessage(str, 0, "TextEdit")
my bh_window_Scroll("TextEdit", "down") -- TELL THE WINDOW TO
SCROLL {"up", "down"}
set str to "( 2 ) Notice the TextEdit Window" & return & return &
"We have scrolled to the end of the document..."
my bh_msg_ShowNormalMessage(str, 0, "TextEdit")
my bh_window_Scroll("TextEdit", "up") -- TELL THE WINDOW TO SCROLL
{"up", "down"}
set str to "( 3 ) Notice the TextEdit Window" & return & return &
"Now we have scrolled back to the beginning of the document..."
my bh_msg_ShowNormalMessage(str, 0, "TextEdit")
on error error_message number error_number
set w_error to "Error (4326) : " & error_number & ". " & error_message
set aErrors to aErrors & w_error & return
display dialog w_error
tell application "TextEdit" to open file ((path to desktop from
user domain) as text) & theFileName
end try
end bh_test_Write2File_Using_Multiple_References
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
_______________________________________________
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