Re: Creating new file etc (re-send due to small glitch)
Re: Creating new file etc (re-send due to small glitch)
- Subject: Re: Creating new file etc (re-send due to small glitch)
- From: Bill Hernandez <email@hidden>
- Date: Fri, 13 Apr 2007 00:42:54 +0000
On Apr 13, 2007, at 12:22 AM, Bill Hernandez wrote:
Gregory,
I caught one more little Ooops so I fixed it and resent the whole
thing...
Please read the notes below...
-- YOU CAN PICK WHERE YOU WANT TO STORE STUFF
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- [1020] SYSTEM PATHS FOR ANY PROJECT
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
property aErrors : {}
property g_text_delim : ":"
property g_posix_delim : "/"
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- [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 InitPaths()
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 InitPaths
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on InitGlobals()
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- 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 InitGlobals
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on write_to_file(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
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
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 (4388) : 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 (4389) : " & 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 write_to_file
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- ON RUN
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on run
if (my InitPaths()) then
if (my InitGlobals()) then
tell me
main("some_choice")
end tell
end if
end if
end run
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
-- FUNCTION MAIN
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on main(which_choice)
tell application "Finder"
copy (the clipboard) to Clip_Board
end tell
--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 filepath to (Bookmaker_Folder & "New_Book.txt")
set aList to {"alias file", "text path", "Posix path"}
set aResults to display dialog "Please choose the type of reference
to try..." buttons {(item 1 of aList), (item 2 of aList), (item 3 of
aList)} default button {(item 1 of aList)}
set choice to button returned of aResults
if (choice = (item 1 of aList)) then
try
set this_file to alias filepath
on error
set this_file to filepath -- if it hasn't been created yet there
will be an error
end try
else if (choice = (item 2 of aList)) then
set this_file to filepath
else if (choice = (item 3 of aList)) then
set this_file to POSIX path of filepath
else
-- ERROR
end if
my write_to_file(this_Book, this_file, false)
end main
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
Hopefully this helps...
Best Regards,
Bill Hernandez
Plano, Texas
On Apr 13, 2007, at 3:15 AM, Gregory White wrote:
Dear List, I would be very grateful for any help you could give me as
I've been struggling with it for a few days.
I'm trying to create a new file in the given directory and then
replace old data with new data should that be required.
I will get that data from the clipboard as it originates in another
app. The data will be text.
Kind regards,
Greg.
Here is the script which does not work.
tell application "Finder"
copy (the clipboard) to Clip_Board
end tell
set Bookmaker_Folder to "home:Documents:ReferenceWorks Bookmaker:"
--in the finished script I would set this_book to Clip_Board
set this_Book to "Once upon a time in Silicon Valley... Hello"
set this_file to (((Bookmaker_Folder) as text) & "New Book")
my write_to_file(this_Book, this_file, false)
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the this_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
specialist.com
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
specialist.com
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
_______________________________________________
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