Creating Lists from a Tab Delimited text file

I have the following script:


set theData to "path:to:Some:Text:File.txt" as string

tell application "Microsoft Excel"
	-- open the data file.
	open theData
	--find the first empty cell in the column of filenames
	set lastCell to first row index of (find column 1 what "" look in values look at whole)
	--create two lists.  1 of the file prefixes & 1 of the Folder Locations
	set prefixList to the value of the range ("A2:A" & (lastCell - 1))
	set folderPathList to the value of the range ("B2:B" & (lastCell - 1))
	close workbook 1 without saving
end tell

doSomething()

The script works great. However, I’d like to run it on a machine that does not have Excel. So, does anyone know how I would achieve the same results using a text editor such as TextWrangler?

My suggestion. Don’t use any editor. If you already have a two colum file in tab delimited text format using AppleScript’s text functions isn’t a problem.

-- set up the lists
set prefixList to {}
set folderPathList to {}

-- select and read the file natively
set theFile to (choose file) as string
set theData to read file theFile

-- set up text item delimiters
set {ATID, AppleScript's text item delimiters} to {"", tab}

-- break the read data into lines (paragraphs)
repeat with aLine in (paragraphs of theData)
	-- append to the lists breaking lines into items with tab delimiters
	set {end of prefixList, end of folderPathList} to {text item 1, text item 2} of aLine
end repeat

-- always set delims back to default
set AppleScript's text item delimiters to {""}

I knew I was working too hard!

I actually forgot that Applescript could read a text document. :rolleyes:

Thanks, James!

Ok, so let’s throw in the curve ball.

I want the script to be a folder action that runs when a file is saved into the hotfolder.

This script works beautifully as a standalone:


set prefixList to {}
set folderPathList to {}

set theDataFile to "path:to:Text:file.txt" as string
set theData to read file theDataFile
-- create the list of prefixes and folderpaths to compare thePrefix to.
set {ATID, AppleScript's text item delimiters} to {"", tab}
-- break the read data into lines (paragraphs)
repeat with aLine in (paragraphs of theData)
	-- append to the lists breaking lines into items with tab delimiters
	set {end of prefixList, end of folderPathList} to {text item 1, text item 2} of aLine
end repeat
set AppleScript's text item delimiters to ATID

return prefixList

But, as soon as I try to run it from a folder action, the script fails. Here is that script:


on adding folder items to this_folder after receiving dropped_items
	set allFiles to list folder this_folder without invisibles
	set fileCount to count the items in allFiles
	set filePath to "Troy_Pool:Screencraft:templates:SeeFile hotfolder:"
	-- set up the lists
	set prefixList to {}
	set folderPathList to {}
	
	-- select and read the file natively
	set theDataFile to "path:to:Text:file.txt" as string
	set theData to read file theDataFile
	
--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	--gets to this point in the script and fails.
	--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	
	-- create the list of prefixes and folderpaths to compare thePrefix to.
	set {ATID, AppleScript's text item delimiters} to {"", tab}
	-- break the read data into lines (paragraphs)
	repeat with aLine in (paragraphs of theData)
		-- append to the lists breaking lines into items with tab delimiters
		set {end of prefixList, end of folderPathList} to {text item 1, text item 2} of aLine
		display dialog (prefixList & " : " & aLine)
	end repeat
	set AppleScript's text item delimiters to ATID
	display dialog prefixList
end

I added the display dialog’s to see where it was failing - it will read the text file and display that dialog but will not go any further.

Anyone have any idea what I have done wrong??

One quick comment hits me in the eye:

on adding folder items to this_folder after receiving dropped_items
   set allFiles to list folder this_folder without invisibles -- this line is unnecessary. That list is dropped_items.
   set fileCount to count the items in allFiles

Read prefers an alias.

Thanks, it is a hangover from when I was selecting a folder manually.

Even if I specifiy the file path as an alias, the script fails in the same place. BTW, is there a way to get an event log from a folder action?

If I read your post correctly, Adam, you simply set the file path to alias rather than string and made no other changes. If that is the case, then I had tested that and it still fails.

—edit-----
noticed your other edits - added all of your code, and she still fails.

A moment - that one is wrong. Is the data file supposed to be the one that’s dropped?

no. they are going to be image files.

The data file simply needs to be read in so that I can determine information from those lists. I didn’t post the entire script because the rest of the script is fairly irrelevant if I cannot create the lists from that data file.

Assuming yes, “dropped” is a list. You want to read an item in the list.

on adding folder items to tfolder after receiving dropped
– dropped is a list, even if there is only one file
set fileCount to count dropped
set filePath to “Troy_Pool:Screencraft:templates:SeeFile hotfolder:” as alias
– set up the lists
set prefixList to {}
set folderPathList to {}
– select and read the file natively
set theData to read item 1 of dropped – must be a .txt file – not an rtf or doc
– create the list of prefixes and folderpaths to compare thePrefix to.
set {ATID, AppleScript’s text item delimiters} to {“”, tab}
– break the read data into lines (paragraphs)
set P to paragraphs of theData
repeat with aLine in P
– append to the lists breaking lines into items with tab delimiters
set {end of prefixList, end of folderPathList} to {text item 1, text item 2} of aLine
display dialog (prefixList & " : " & aLine)
end repeat
set AppleScript’s text item delimiters to ATID
display dialog prefixList
end adding folder items to

I haven’t read the last few posts or so, but this folder action works for me, the display dialog is merely present to verify the previous steps are performing correctly.

on adding folder items to this_folder after receiving dropped_items
	set prefixList to {}
	set folderPathList to {}
	tell application "Finder"
		repeat with aItem in dropped_items
			if file type of aItem is "TEXT" or name extension of aItem is "txt" then
				set aItem to (aItem as string)
				set theData to read file aItem
				set {ATID, AppleScript's text item delimiters} to {"", tab}
				repeat with aLine in (paragraphs of theData)
					set {end of prefixList, end of folderPathList} to {text item 1, text item 2} of aLine
				end repeat
				set AppleScript's text item delimiters to {""}
			end if
		end repeat
		display dialog (prefixList as string)
		display dialog (folderPathList as string)
	end tell
end adding folder items to

Ahhh!

This thread is taking on a life of its own in the wrong direction… :o

Here is the deal. I have a data file which is a tab delimited text file that is in a stationary place. The file is made up of “prefix folderPath”

the hotfoder that this script is attached to, will be waiting for image files that have a naming convention that includes one of the prefixes listed in the data file.

when the folder receives images, it should, read the data file and create two lists - one of the prefixes and one of the folder paths.

It will then repeat through the image files that were dropped into the folder. It will check the prefix of the image file - find the proper folder path and move it to that folder.

(Oh and this is my 100th post! I am now a Centurian.) :cool:

OKay here you go

on adding folder items to this_folder after receiving dropped_items
	set theFile to "path:to:some:tab:delimited:text:file.txt"
	set theData to read file theFile
	set prefixList to {}
	set folderPathList to {}
	set {ATID, AppleScript's text item delimiters} to {"", tab}
	repeat with aLine in (paragraphs of theData)
		set {end of prefixList, end of folderPathList} to {text item 1, text item 2} of aLine
	end repeat
	set AppleScript's text item delimiters to {""}
	repeat with aFile in dropped_items
		(*
		You now have the two lists you desire.
		Now step through your dropped items to do
		any processing, moving, etc. that you require
		*)
		
	end repeat
end adding folder items to

Since my version is virtually identical to James’, I won’t post it, but let the lesson be that the thread took a life of its own in the wrong direction because you didn’t clarify the “big picture” until well into it. :confused:

Apparently, we need to have a an emoticon that is spanking someone. I’ve learned my lesson!

First off, thank you guys for all of your assitance on this.

I took James’ latest script and added some display dialog’s for testing. (is there any other way to trouble shoot a script attached to a folder?)

on adding folder items to this_folder after receiving dropped_items
	set theFile to "path:to:some:tab:delimited:text:file.txt"
	set theData to read file theFile
	set prefixList to {}
	set folderPathList to {}
	set {ATID, AppleScript's text item delimiters} to {"", tab}
	repeat with aLine in (paragraphs of theData)
display dialog aLine
		set {end of prefixList, end of folderPathList} to {text item 1, text item 2} of aLine
display dialog prefixList
	end repeat
	set AppleScript's text item delimiters to {""}
	repeat with aFile in dropped_items
		(*
		You now have the two lists you desire.
		Now step through your dropped items to do
		any processing, moving, etc. that you require
		*)
		
	end repeat
end adding folder items to

when an item is dropped on the folder, the script does the following

display dialog aLine
:: prefix1 folderPath1
display dialog prefixList
:: prefix1
display dialog aLine
:: prefix2 folderPath2
display dialog prefixList

---- script errors out! —

So, I think we may still be in the same place we were with my original script (although, James’ version is much more stream-lined).

Didn’t mean that in an unfriendly way, Scott; but it does happen all the time - folks like James and I will go probing around with guesses of what a script that doesn’t work was supposed to do.:wink:

Now the unanswered question: When I write a droplet {script applications with an on open the_dropped — end open handler) or an folder action (script attached to a folder starting with “on adding folder items to this_folder after receiving dropped_items”) I always debug it first as a straight run from the Editor script by doing this:


on adding folder items to theFolder after receiving theStuff
	
	-- do stuff to the contents of theFolder items, a list of aliases
	
end adding folder items to

-- becomes:

set theFolder to (choose folder)
set theStuff to choose file with multiple selections allowed -- or folder
-- on adding folder items to theFolder after receiving  theStuff

-- do stuff to the contents of theFolder items, a list of aliases

-- end adding folder items to

-- or for a droplet, the same idea; comment out the on open droppings and replace with "choose file or folder with multiple selections allowed

-- whatever was supposed to happen

-- end open

When that works, I put it back in handler form and try it that way.

Scott,

Thats the exact code I was using, as you mentioned, and it works on my end. This would lead me to believe there is something unaccounted for in your text file. How was this created? Could you provide it or a few sample lines?

actually, found the an issue with my display dialog line.

The display dialog prefixList - should have read “display dialog prefixList as string.”

So far, so good, I have run into another issue at this point that is unrelated to the above code. Thanks for all of your help!