Re: Appending text?
Re: Appending text?
- Subject: Re: Appending text?
- From: Bill Hernandez <email@hidden>
- Date: Wed, 16 May 2007 20:09:33 -0500
On May 16, 2007, at 10:25 AM, Gary Franks wrote:
Wow Bill.
I'd like to think someday I could write such a script. It will be a
while.
I will study it extensively and appreciate it very much.
G
Don't be too impressed, I am not a very good AppleScript person at
all. It's only in the last few months that I have really trying to
learn applescript and applescript studio. I've dabbled for a while
but it's syntax has always gotten the better of me...
Anyway there were some poor parts in the script I sent. It was fairly
late, and I didn't check it over as well as I should have prior to
hitting the send button...
I cleaned it up some and hopefully here is a better way to do this...
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_title()
(*
Author : Bill Hernandez
ProjectNo : 4317
ProjectName : concatenate_text_files
FileName : concatenate_text_files.txt
FilePath : ~/scripting/completed/
concatenate_text_files.txt
Version : 1.0.0
VersionDate : [ 2007_05_15 ]
Created : 5/15/07 ( 10:02 PM )
Updated : Tuesday, May 15, 2007 ( 10:30 PM )
*)
end script_title
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
property p_aErrors : {}
property p_show_errors : true --THIS SHOULD BE SET TO FALSE
property p_type : "TEXT"
property p_creator : "R*ch"
property p_divider : "-- +---------+---------+---------+---------
+---------+---------+---------+---------+"
property p_include_header : true --THIS SHOULD BE SET TO TRUE
property p_convert_newlines_to_carriage_returns : true --THIS SHOULD
BE SET TO TRUE
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on run
set output_FileName to "myNewFile.txt"
set input_FolderPath to (choose folder with prompt "Select the
Source Folder")
set output_FolderPath to (choose folder with prompt "Select a
Destination Folder")
set prompt to "Save the merged files to the following document
(target file) :" & return & return
set prompt to prompt & "Options : " & return & return
set prompt to prompt & "( 1 ) Cancel and do nothing." & return
set prompt to prompt & "( 2 ) OverWrite the target file if it
exists, with the source documents" & return
set prompt to prompt & "( 3 ) Append the source documents to the
target file if it exists" & return
-- INSTEAD OF USING THE STANDARD "Cancel" BUTTON, BY USING "_____
Cancel _____", ALL
-- THREE BUTTONS WILL BE APPROXIMATELY THE SAME SIZE WHEN
DISPLAYED, INSTEAD OF
-- HAVING TWO LARGE BUTTONS, AND ONE SMALL ONE. THE ADVANTAGE OF
USING THE
-- STANDARD "Cancel" BUTTON IS THAT IF THE USER CLICKS ON IT, AN
ERROR -128 IS GENERATED
-- AND THAT CAN BE HANDLED EASILY, I PREFER SOME SORT OF SYMMETRY
IN THE BUTTONS
set b1 to "_____ Cancel _____"
set b2 to "OverWrite target file..."
set b3 to "Append target file..."
set aResults to display dialog prompt default answer
output_FileName buttons {b1, b2, b3} default button {b3}
set bChoice to button returned of aResults
if (bChoice is not equal to b1) then
set str to text returned of aResults
if (str is not equal to "") then
set output_FileName to str
end if
set output_FilePath to output_FolderPath & output_FileName as
text
-- I HAD INCLUDED THE WHOLE "on run" IN A [tell application
"Finder"] BLOCK WHICH WAS BAD
-- AND I DIDN'T CATCH IT, AND I HAD A NESTED [tell application
"BBEdit"] INSIDE OF THAT
-- WHICH WAS VERY POOR ON MY PART, BUT IT WAS VERY LATE WHEN I
TRIED TO REWRITE
-- WHAT YOU HAD, AND I DIDN'T TRY TO CHANGE THE TRACK YOU WERE
ON TOO MUCH
tell application "Finder"
-- I try to define lists of items with a beginning char "a"
which tells me it is an array of elements
-- such as aFiles, aSelection, aItems, aList ...
-- make sure the files are of the correct type, before you
try to copy them
set aFileList to (every file of input_FolderPath ¬
whose ¬
file type = p_type or ¬
creator type = p_creator or ¬
name extension = "txt") as alias list
(*
try
-- This is fine because you set the properties when
you create the file
-- the try block handles the error generated when you
try to create a file that
-- already exists, the next time you run the script .
You can skip this [ try ... end try] block
-- but then you would have to set the type and
creator after calling the [... open for access ...] line
-- and you could get the properties of the file and
see if they had been set or not
-- I decided to get rid of this [make ...] line and
handle it at the bottom, although I like this with a
-- try statement it's a lot easier than setting the
type and creator the way I did it at the bottom
-- make new file with properties
{name:output_FileName, creator type:p_creator, file type:p_type} at
output_FolderPath
end try
*)
end tell
try
set output_FileRef to open for access output_FilePath with
write permission
if (bChoice is equal to b2) then
set eof of output_FileRef to 0 -- overwrite the output file
end if
repeat with currFileRef in aFileList
set f_info to "" -- Initialize each time thru the loop
if (p_include_header) then
tell application "Finder" to set f_props to
properties of currFileRef
-- I did this in multiple steps so it makes easier
for you to modify if you wish
set f_info to f_info & p_divider & return
set f_info to f_info & "fileName : " & name of
f_props & return
set f_info to f_info & "modified : " & modification
date of f_props & return
set f_info to f_info & "textPath : " & (currFileRef
as text) & return
set f_info to f_info & "posixPath : " & (POSIX path
of currFileRef) & return
set f_info to f_info & p_divider & return
end if
if (p_convert_newlines_to_carriage_returns) then
-- convert newlines back to carriage returns to
prevent chaos in the output
-- split the document into its list of paragraphs
using the newlines as text item delimiters
-- convert the list of paragraphs into a string using
the return as text item delimiters
-- reset the text item delimiters to default value of ""
set AppleScript's text item delimiters to "\\n" --
newline character
set aParagraphList to paragraphs of (read
currFileRef) & return -- read the contents of the file and convert it
into a list of paragraphs
set AppleScript's text item delimiters to return --
return character
set f_contents to aParagraphList as string -- convert
the list of paragraphs back into one string
set AppleScript's text item delimiters to ""
else
set f_contents to (read currFileRef) & return
end if
-- by setting f_info to "" at the beginning of the
repeat loop, I can write to disk
-- once instead of twice by combining (f_info & f_contents)
write (f_info & f_contents) to output_FileRef starting
at eof
end repeat
close access output_FileRef
tell application "Finder"
-- The first time through, Set the type and creator. I
could have read the properties and done this a little cleaner
-- but I am probably just confusing you more. This tell
block is needed to replace the [try ...make ... end try] block at the
top
set output_FileRef to output_FilePath as alias
if (((file type of output_FileRef) is not equal to
p_type) and ((creator type of output_FileRef) is not equal to
p_creator)) then
set file type of output_FileRef to p_type
set creator type of output_FileRef to p_creator
end if
end tell
tell application "BBEdit"
activate
open file output_FilePath
display dialog "All Done..." buttons {"OK"} default
button {"OK"} giving up after 2
end tell
on error
close access file output_FilePath
end try
end if
end run
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
(*
-- I GOT RID OF THIS WHOLE [TRY ... ON ERROR ... END TRY] BLOCK
-- IT IS REALLY NOT NEEDED. I WAS JUST TRYING TO SHOW YOU AN ERROR
-- HANDLER TO DEAL WITH THE [make ...] LINE
-- this entire [try ... on error ... end try] block can be
replaced with the small [try ... end try] block
-- but I left it in since you originally had it in your code, and
tried to show how to handle the error
-- that would take place once the file had been created, and the
every time you ran it thereafter
try
-- After the first time you try to run this routine it will
generate an error
-- because the file will aready exist, so this will take care
of that little problem
tell application "Finder"
make new file with properties {name:output_FileName,
creator type:p_creator, file type:p_type} at output_FolderPath
end tell
on error error_message number error_number
if (p_show_errors) then
-- If you have a large piece of code that contains many "on
error" handlers the "Error (4389) : " just
-- helps me find where in the code the error occurred,
rather than just an error message that is harder
-- to find. Each error trap gets a different arbitrary
number...
set s to ""
set s to s & "After the first time you try to run this
routine it will generate an error because "
set s to s & "the file will aready exist, so this will take
care of that little problem." & return & return
set s to s & "If you have a large piece of code that
contains many \"on error\" handlers the "
set s to s & "\"Error (4389) : \" just helps me find where
in the code the error occurred, rather "
set s to s & "than just a generic error message that is
harder to find. Each error trap gets "
set s to s & "a different arbitrary number..." & return &
return
set s to s & "Picture the error message below, without some
sort of reference, to help you locate "
set s to s & "where in the code the error took place (as
you get farther along you will have more, "
set s to s & "and more \"on error\" handlers)..." & return
& return
set s to s & "After you have seen this message once, change
the line containing "
set s to s & "\"property p_show_errors : true\" to
\"property p_show_errors : false\""
set s to s & " ---------------------------------------" &
return & return
set w_error to s & "Error (4389) : " & error_number & ". "
& error_message & " \"" & output_FilePath & "\""
set p_aErrors to p_aErrors & w_error & return &
output_FilePath
display dialog w_error
-- FILE ALREADY EXISTS, DO NOTHING
end if
end try
*)
_______________________________________________
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