I'm trying to make a script I can use to find all instances of a folder, be it on the root drive, external drive or mounted networked storage, and list all the contents in either a drop down box or a selectable list in a dialogue window.

To pseudo-code it:

tell application "Finder"
    for (every instance of "*/Folder"
        append items in "*/Folder" to contentsList
    end for
end tell

I honestly have no idea how to actually do it in AppleScript since it doesn't natively have for loops, and I'm not sure about adding items to arrays in AppleScript.

Is anyone able to give me any pointers? Alternatively, being able to do the same in a shell script would be just as good.

Thanks in advance!

  • 1
    Which real-world problem are you trying to solve here? There may be better ways to solve it than to recursively traverse through the whole filesystem with AppleScript.
    – nohillside
    Oct 10, 2013 at 12:07
  • In this case, it is for finding /Users/ folders on any and all connected filesystems and listing the names of the home folders inside them. Definitely, if there is a better way, I want to know about it! Oct 10, 2013 at 12:31

2 Answers 2

set l to {}
tell application "Finder"
    repeat with f in (folders of entire contents of (POSIX file "/Users/username/" as alias) where name is "Folder") as alias list
        repeat with i in (get items of f)
            set end of l to POSIX path of (i as alias)
        end repeat
    end repeat
end tell
choose from list l
find ~ -name Folder -exec ls {} +
shopt -s globstar # bash 4.0 or later
printf %s\\n ~/**/Folder/*
mdfind 'kMDItemContentType==public.folder&&kMDItemFSName==Folder' -onlyin ~ |
while IFS= read -r l; do ls "$l"; done

With bash you can do something like

for dir in $(find ~ -type d -name 'Folder' -print); do
    echo $dir
    ls -al "$dir"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .