Differentiating between "A1", "A12", etc. - my solution
Differentiating between "A1", "A12", etc. - my solution
- Subject: Differentiating between "A1", "A12", etc. - my solution
- From: "Wadson, David" <email@hidden>
- Date: Fri, 11 Jan 2002 08:17:07 -0500
Well, I got a couple of solutions to the problem but unfortunately none of
them are simple and painless. I guess I wasn't on the wrong track while
trying to come up with a solution myself - no matter what it will involve
some convoluted coding and jumping through hoops to do it.
All this was further complicated by how to work a solution into the working
code that I already had. After getting frustrated by the increasing number
of nested if/then, try/on error, repeat while, blocks that I had, I decided
the easiest approach was to add leading zeros to the files before I try to
do anything with them. By reading in characters 2 and 3 of the filename and
trying to convert it to a number, I could determine if I needed to insert a
leading zero for the single digit numbers.
set filesToMerge to every file of folderToMerge whose file type is "PDF "
set totalNumberOfFiles to count of filesToMerge
repeat with i from 1 to totalNumberOfFiles
set DocumentName to name of item i of filesToMerge
set DocumentPrefix to character 1 of DocumentName
set DocumentNumber to characters 2 thru 3 of DocumentName as string
try
set DocumentNumber to DocumentNumber as number
on error
set characterCount to count of characters in DocumentName
set restOfFilename to characters 4 thru characterCount of DocumentName
as string
set DocumentNumber to "0" & (characters 2 thru 3 of DocumentName) as
string
set DocumentNewName to DocumentPrefix & DocumentNumber & restOfFilename
as string
set name of (item i of filesToMerge) to DocumentNewName
end try
end repeat
The loop processes the entire folder very quickly and works like a charm.
Later in the script, where I want to step through the files in numerical
order, I used a routine of this nature:
set pageNumbers to {"01", "02", "03", "04", "05", "06", "07", "08", "09",
"10", "11", "12", "13", "14"}
repeat with i from 1 to numberOfFiles
set currentPageNumber to text item i of pageNumbers
set documentToInsert to every file of folderToMerge whose name starts with
currentPageNumber as alias
-- do whatever you need to do with the file here...
end repeat
Instead of having the list of two-digit page numbers, I could have added a
routine to add a leading zero to the variable "i' if necessary, but it
seemed easier to look it up in the list.
The solution wasn't as bad as I thought it would be, once I figured out that
adding the leading zeros was the easiest approach. Granted, being able to
count on the user of the script to have named every file a specific way
would be the easiest but it's never the most reliable. But hopefully now I
won't have to worry about that...we'll see how the script does when thrown
into action...
I'm still cleaning up the final code to remove some redundant steps, but if
anyone is interested in some code for merging a folder of separate PDF files
into one document, let me know.