open text files & save as worksheets in excel with Automator?

hi,
i'm an Automator newbie and i'll admit upfront i don't know apple script. i have a bunch of text files that i'd like to open in excel. the basic actions would be to tell excel to look for 'all documents' and to open as space delimited. then i want to save the files as excel spreadsheets rather than txt files.

i assume i need to use some combination of applescript and automator to do this, but i don't even know where to start. it seems this is something simple someone might have already written a script/action for. is there a repository of such things anywhere? if not, does anyone want to help me through the process of creating such an action?

thanks,
jill

PB G4 1.5GHz, dual PM G5 Mac OS X (10.4.7) airport, bluetooth

Posted on Aug 3, 2006 10:29 PM

Reply
11 replies

Aug 6, 2006 6:12 AM in response to jill

jill,

Here is a solution.

Create an Automator workflow with the following actions:
1) Get Specified Finder Items (specify the folder containing your space delimited text files)
2) Get Folder Contents
3) Run AppleScript (delete all of the text in the action, click on the link below which will put the AppleScript into Script Editor, select the whole thing, copy it to the Clipboard, paste it into the action)

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">on run {input, parameters}

repeat with aTextFile in input
set theTextFilePath to aTextFile as string
tell application "Microsoft Excel"
open text file filename theTextFilePath data type delimited with space
set theWorkbook to last workbook
set thePath to path of last workbook
set theOriginalWorkbookName to name of last workbook
set theNewWorkbookName to ((characters 1 through -5 of theOriginalWorkbookName) as string) & ".xls"
set theNewFilePath to thePath & ":" & theNewWorkbookName
save workbook as theWorkbook filename theNewFilePath file format workbook normal file format
close theWorkbook saving no
end tell

end repeat

return input
end run</pre>

I am assuming that the text files are the only files in the folder. If they are not then some further filtering will be required likely through inserting a Filter Finder Items action after step 2).



PowerBook 12" Mac OS X (10.4.7)

Aug 6, 2006 9:42 AM in response to lc55

thank you lc55! i completed the steps you described, and it seemed to work beautifully (i even included the Filter Finder Items action, as you suggested). that is, the action ran with no errors. However, i don't see any output files. I'm assuming it should put them in the same directory that it selected the files from. do you have any suggestions as to what i might be doing wrong?
thanks again,
jill

PB G4 1.5GHz, dual PM G5 Mac OS X (10.4.7) airport, bluetooth

Aug 6, 2006 5:20 PM in response to jill

jill,

Yes the Excel files should be in with the original text fiels.

See what the Run AppleScript action is actually receiving by placing a "View Results" action just before it and also befor the Filter Finder Items action. The Filter Finder Items might be too efficient and prevent any files from reaching the Run AppleScript action.

Aug 6, 2006 6:20 PM in response to lc55

thanks. that helped me to figure out what was wrong. now it runs and creates the excel files. the only problem is that the apple script doesn't seem to open/format the text files as space-delimited. any suggestions as to how to implement this?

thanks for you patience,
jill

PB G4 1.5GHz, dual PM G5 Mac OS X (10.4.7) airport, bluetooth

Aug 6, 2006 6:47 PM in response to jill

I'm confused as I was under the impression that your text files were already space-delimited. That is why I used the AppleScript statement:

open text file filename theTextFilePath data type delimited with space

Are they not?

If they are then the text that is separated by spaces should be placed in separate columns in Excel. Is this not happening?

Aug 6, 2006 7:10 PM in response to lc55

that's what i thought, but no - that's not happening. it's putting everything in the row in the first cell of that row, despite the fact that there's a space between each entry in the row. that is, it's not making a new column after each space. it's as opening it as if in excel you'd clicked ...

oops - my bad - i just went to excel and tried to open one and it needs to be tab delimited, not space. i'm sorry -

can i just change the apple script to read 'delimited with tab'?

Aug 6, 2006 7:42 PM in response to jill

i went ahead and changed it to 'delimited with tab' and it works beautifully now. i think the earlier problem (prior to the space/tab confusion) was that my filenames were too long. i manged to create another action (following your example) to rename the files with shorter names and that helped.

i never would have figured out the applescript for myself - at least not very quickly. thanks so much for your help - you're a trooper! this will save me lots of time!

Aug 6, 2006 8:17 PM in response to jill

That's great!

Also congratulations on being able to figure out how to change things to suit your needs. Learning through doing, in most cases, is the best way to learn and put these technologies to use for yours and others needs.

Hope you have fun with all that extra time on your hands.

Aug 6, 2006 10:15 PM in response to lc55

one more question if i may...
is there anyway to expand the path in an action? sometime when i add a file or folder to Get Specified Finder Items, the path is too long to see what the final folder name is. i can't seem to figure out a way to view it in the action once it's added.

thanks again,
jill

Aug 7, 2006 12:07 AM in response to jill

jill,

Yes you can reduce the size of the column containing the actions and this will expand the right column. Once you do that, if you still need room, then you will see a faint line representing a column handle in the titles row of the action. Just like in Excel you can grab the handle and expand the column.

If all else fails then hopefully you have a widescreen and then just increase the size of the window.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

open text files & save as worksheets in excel with Automator?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.