Re: A Faster Script
Re: A Faster Script
- Subject: Re: A Faster Script
- From: BJ Terry <email@hidden>
- Date: Tue, 6 Apr 2004 01:36:56 -0700
I hope I understand your problem correctly (or else I've almost
certainly wasted much of my time), but I thought it might be better to
simply parse the files and render them as tab-delimited text files,
save it to disk, then manually use Excel's native importing on that
file. Hopefully this is useful to you. If I had to guess, I would
suspect this should take perhaps 5-10 minutes on large files (any files
you would consider entering by hand, I should hope). The problem with
the excel method is that it uses many Apple Events, which are slow.
This implementation is hardly tuned, but I don't have time to improve
its performance at the moment. It can be considered the most naive
solution (It also isn't very clean, but I grow weary of looking at it).
If you have any questions regarding the code, or if I solved the wrong
problem, holler back. Here's my input files, and sample output:
BJ Terry
x1.txt:
1 2
3 4
5 6
7 8
x2.txt:
9 10
11 12
13 14
15 16
x3.txt:
9 10
11 12
13 14
15 16
17 18
19 20
output.txt:
1x 1y 2x 2y 3x 3y
1 2 9 10 9 10
3 4 11 12 11 12
5 6 13 14 13 14
7 8 15 16 15 16
null null null null 17 18
null null null null 19 20
The script is just a modification of yours, so that it doesn't talk to
excel (I don't have excel, so I couldn't tackle the problem from that
angle). Note also that lines might wrap on the list server, so you'll
have to check to make sure it makes sense if it isn't compiling. Also,
it will prompt you at the end for a location to save the output file
to.
--select the folder where the file to be imported to excel reside
display dialog "In the following window select the folder containing
the files to be imported into Excel" buttons {"Cancel", "OK"} default
button 2
if the button returned of the result is "OK" then
set source_folder to choose folder
set source_folder_string to source_folder as string
else
quit
end if
set folder_files to list folder source_folder
set files_count to the count of folder_files
--get the prefix of the desired files
set queryOne to display dialog "Enter the prefix of the files to be
imported into Excel." default answer "UN" buttons {"Cancel", "OK"}
default button 2
if the button returned of the result is "OK" then
set file_prefix to text returned of queryOne
else
quit
end if
--identify all of the desired files
set filesToExamine to {}
repeat with currentFile in folder_files
if currentFile begins with file_prefix then
set the end of filesToExamine to the contents of currentFile
end if
end repeat
set aggregateList to {{}}
set largestList to 0
set currentStep to 0 --this is necessary in order correctly label the
columns and to
--propery assign the data to the appropriate column
repeat with currentFile in filesToExamine
--This functions to examine each tab-delimited file and extract the
data from it.
-- It does not, however, maintain any organizational constructs.
set currentStep to currentStep + 1
set filePath to source_folder_string & currentFile as alias
set unSortedData to read filePath using delimiter {return, tab, ASCII
character 10}
-- The first item of aggregateList is a list of 1x 1y pairs
set rowone to first item of aggregateList
set end of rowone to (currentStep as string) & "x"
set end of rowone to (currentStep as string) & "y"
set first item of aggregateList to rowone
-- Add elements from the current list, creating new elements if
necessary
-- Padded with null, but one could use space or something
repeat with currentRow from 2 to (((count unSortedData) / 2) + 1)
set x to item (currentRow * 2 - 3) of unSortedData
set y to item (currentRow * 2 - 2) of unSortedData
if currentRow > (count aggregateList) then
set end of aggregateList to {}
repeat (currentStep * 2 - 2) times
set end of item currentRow of aggregateList to null
end repeat
end if
set end of item currentRow of aggregateList to x as string
set end of item currentRow of aggregateList to y as string
end repeat
end repeat
set my text item delimiters to tab
-- Coerce each item of aggregate list (each row)
repeat with currentRow from 1 to count aggregateList
set item currentRow of aggregateList to item currentRow of
aggregateList as string
end repeat
set my text item delimiters to return
set aggregateList to aggregateList as string
return result
try
set filename to open for access (choose file name) with write
permission
write aggregateList as string to filename
close access filename
on error
close access filename
end try
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.