views:

677

answers:

3

I need to get the entire (visible) contents of a folder and its subfolders as a list. Is this possible?

+1  A: 

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

Philip Regan
please see my post
mcgrailm
I made an edit to my post
mcgrailm
+1  A: 

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

mcgrailm
Right, because a massive list of {document file "FileName" of folder "SubFolder" of folder "Folder" of folder "username" of folder "Users" of startup disk of application "Finder", ad nauseum} and having to subsequently process *that* is so much more useful as to require a down vote on mine.
Philip Regan
what is there to process you have the list and your reference the items as needed i.e alias the item in file list your doing way more work than is needed that's why I gave you a down vote I didn't give it to you out of spite where as that appears to be what your doing
mcgrailm
also your kFileList returns a list of files name only and no reference to where the file came from so what then go back trough the folder to figure out where they came from ?
mcgrailm
This is a "six of one, half dozen of the other" scenario. By getting the file info properties up front, it's a cinch to make a customized list right out of the gate as opposed to getting the list of aliases and then pulling desired information after the fact. Fewer lines of code isn't always the best answer, nor is it the worst answer. Please think twice before downvoting.
Philip Regan
@spiteful downvote: Yes. Yes, I did. Yours was as unnecessary as mine per my point above. I rarely downvote (six times in a year including here whereas you seem to be on a roll at 5 within 3 months). I only downvote for truly egregious errors in answers, but you seem to have caught me on a grumpy day. Next time, again, think twice before downvoting and sacrificing precious points just for the sake of "wanting to show us both how easy this is". If you simply don't like an answer, then just leave it alone.
Philip Regan
I disagree, your way is wasteful you create a list of file names which are of no use because you don't know where they came from, how are you going to do anything with that?? and if I wanted that I could just add alittle more code to what I have done see edit@grumpy: no not grumpy. your code is useless and wasteful and your right less code isn't ALWAYS better but in this instance I beleive it is
mcgrailm
as for down voting in the past three months it has only been recently that i have pick up on SO and I very rarely down vote its typically when the answer given doesn't accomplish the goal or doesn't accomplish the goal in a meaningful way.
mcgrailm
The code I supplied is far from "useless", and now you're getting insulting. You're assuming that this can only address file names. The variable `file_name` can *easily* be made into a list of any subset of the file's info. But mine can actually offers *more control* over functionality than yours. For example, what if the OP wanted to only go down a certain depth from the start folder to avoid needless cataloging? Hooks are easy to add in mine, but yours can't do that. I'm offering data retrieval control and formatting up front, which is just a flip from what you're doing. Keep trying, dude.
Philip Regan
" I very rarely down vote its typically when the answer given doesn't accomplish the goal or doesn't accomplish the goal in a meaningful way."... " That's very subjective. Please see the accepted answer to "How should down voting be used?" (http://meta.stackoverflow.com/questions/19268/how-should-down-voting-be-used) "...a downvote should be used for an answer that is significantly wrong or irrelevant or misleading or offers really bad advice". I answered the question in a meaningful way, and it wasn't wrong, irrelevant, nor was it bad advice. Again, think twice before downvoting. Good bye.
Philip Regan
A: 

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

Ad59