Re: Appending text?
Re: Appending text?
- Subject: Re: Appending text?
- From: Gary Franks <email@hidden>
- Date: Wed, 16 May 2007 10:25:52 -0500
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
On May 16, 2007, at 12:34 AM, Bill Hernandez wrote:
Gary,
See if this will help...
Copy the script and save it as "concatenate_text_files.scpt"
Best Regards,
Bill Hernandez
Plano, Texas
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
on script_title()
(*
Author : Bill Hernandez
ProjectNo : 4317
ProjectName : concatenate_text_files
FileName : concatenate_text_files.txt
FilePath : ~/user_scripting/user/
600_as_user_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
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
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
-- 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
make new file with properties {name:output_FileName,
creator type:p_creator, file type:p_type} at output_FolderPath
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
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
if (p_include_header) then
set f_props to properties of currFileRef
set f_info to "" -- 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
write (f_info & return as string) to output_FileRef
starting at eof
end if
if (p_convert_newlines_to_carriage_returns) then
-- convert newlines back to carriage returns to
prevent chaos
-- split the document into its paragraphs using the
newlines as text item delimiters
-- convert the list of paragraphs to strings 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 f_paragraphs to paragraphs of (read currFileRef)
& return
set AppleScript's text item delimiters to return --
return character
set f_contents to f_paragraphs as string
set AppleScript's text item delimiters to ""
else
set f_contents to (read currFileRef) & return
end if
write f_contents to output_FileRef starting at eof
end repeat
close access output_FileRef
tell application "BBEdit"
activate
open file output_FilePath
display dialog "All Done..." buttons {"OK"} default
button {"OK"} giving up after 2
end tell
end tell
end if
end run
-- +---------+---------+---------+---------+---------+---------
+---------+---------+
On May 15, 2007, at 6:40 PM, Gary Franks wrote:
I'm a rookie. This is my 2nd script and I was beginning to get
cocky, as I completed my 1st script without help. Other than
volumes of reading materials. Now, more frustrated and less cocky,
here I am. I'm trying to write a script that will open a large
number of text files (BBEdit) from a folder and append each of
them to a new BBEdit document in another destination folder. (Many
short documents into one long document.) My script:
on run
set inputPath to (choose folder with prompt "Select the Source
Folder")
set outputPath to (choose folder with prompt "Select a
Destination Folder")
tell application "Finder"
make new file with properties {name:"myNewFile", creator
type:"R*ch", file type:"TEXT"} at outputPath
set numFiles to (count files in folder inputPath)
repeat with loopVar from 1 to numFiles
tell me
set myWritableFile to outputPath & "myNewFile" as text
set openWritableFile to open for access myWritableFile with
write permission
set mySource to read file loopVar as text
write mySource to myWritableFile as string starting at eof
close access myWritableFile
end tell
end repeat
end tell
end run
Can't seem to read the text from the files in the source folder.
I'm hung there after many failures. Appreciate any help and
appreciate this list.
G
_______________________________________________
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