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 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.
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
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.
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
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.)
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.
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
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.
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.
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?