I need to get the entire (visible) contents of a folder and its subfolders as a list. Is this possible?
I'm sure there is a shell command that can do this faster, but here is one way in pure Applescript that gives you total control over formatting what info you would like displayed.
property kFileList : {}
tell application "Finder"
set source_folder to choose folder with prompt "Please select directory."
my createList(source_folder)
end tell
on createList(item_list)
set the the_items to list folder item_list without invisibles
set item_list to item_list as string
repeat with i from 1 to number of items in the the_items
set the_item to item i of the the_items
set the_item to (item_list & the_item) as alias
set this_info to info for the_item
set file_name to name of this_info
set end of kFileList to file_name
if folder of this_info is true then
my createList(the_item)
end if
end repeat
end createList
On a side note, there are also a number file listing applications that can do this faster than Applescript.
UPDATE: As a result of this discussion, here is the function again, but this time using the updated API. This could probably could use some cleaning up, but it works cataloging my Desktop handily enough (and that's a deep, deep folder for me):
property kFileList : {}
tell application "Finder"
set source_folder to choose folder with prompt "Please select directory."
my createList(source_folder)
end tell
return kFileList
on createList(mSource_folder)
set item_list to ""
tell application "System Events"
set item_list to get the name of every disk item of mSource_folder
end tell
set item_count to (get count of items in item_list)
repeat with i from 1 to item_count
set the_properties to ""
set the_item to item i of the item_list
set the_item to ((mSource_folder & the_item) as string) as alias
tell application "System Events"
set file_info to get info for the_item
end tell
if visible of file_info is true then
set file_name to displayed name of file_info
set end of kFileList to file_name
if folder of file_info is true then
my createList(the_item)
end if
end if
end repeat
end createList
I must be subscribed to the wrong mailing lists or missing one because these API changes were enacted and I never once heard about them. I've used my first-offered method in dozens of projects largely because it was the code originally offered by Apple, and the complete lack of errors using this (even at the time of this writing) never triggered a need for me to update anything.
Turnabout is fair play, and my apologies for the spiteful downvote to mmcgrail, and I am replacing it with an upvote. Just to be clear, I never thought the answer given by mmcgrail was wrong. It's a great one-line convenience method but one I've stayed away from per my comments already given. But rather, it was his down vote and its stated context that I took offense with. In the end, it's just code, and I think we're all here for the same reason: to find a better way of doing what we do. It seems I now have some updates of my own to enact.
Cheers
that is way more work than needed. I know this is an old post but I want you both to see how easy this can be
tell application "Finder"
set file_list to entire contents of (choose folder with prompt "Please select directory.")
end tell
if you want a list of file names then you could do this
tell application "Finder"
set file_list to name of every file of entire contents of (choose folder with prompt "Please select directory.")
end tell
Due to the comments that follow this post I asked a group of experts in business to look at this question and evaluate our answer in an unbiased manner and the answer I got was thsis
"How about a pox on both your houses? :-)
Yes, entire contents does exactly what you say -- but it easily chokes on large folders, and takes forever (and a day if you're on 10.6). It's OK for small things, like extracting all the files of one kind out of a folder you know will only contain a small number of files.
The recursive method also works well -- but it's using "list folder", and the dictionary listing for it says it's deprecated and we shouldn't use it any more."
So I hear by declare that I was wrong! both of these solutions are valid but have "pox" or holes in their uses. My applogies to to Philip. Should he decide to edit his answer (because thats the only way to change my vote)I will be happy to return and give him +1
Hi, I'd like to know the way to generate a "choose from list" action with these results (contents of folder ?), and though how to entry directly the path of the folder (that I want to see the content) in this script ? Thx