Get full catalog contents using AppleScript

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

+7
list folder subfolder applescript
source share
3 answers

I'm sure there is a shell command that can do this faster, but here is one way in pure Applescript that gives you full control over the formatting of the information you want to display.

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 the side of the note, there is also a list of files with file names that can do this faster than Applescript.

UPDATE:. As a result of this discussion, the function is here again, but this time using the updated API. Perhaps this could lead to some cleaning, but it works with the directory of my desktop quite conveniently (and for me a deep deep folder):

 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 because these API changes were accepted and I never heard of them. I used my first proposed method in dozens of projects, mainly because it was the source code offered by Apple, and the complete absence of errors using this (even at the time of writing) never made it necessary to update anything,

Turnabout is fair play, and my apologies for the evil downvote for mmcgrail, and I replace it with upvote. To be clear, I never thought that the answer given by mmcgrail was wrong. This is a great one-line method, but I stayed away from my comments already made. But, rather, it was his voice and his stated context, with which I was offended. In the end, it's just code, and I think we are all here for the same reason: to find the best way to do what we do. It seems that now I have some updates that I can do.

Greetings

+7
source share

it is more than necessary. I know this is an old post, but I want you both to see how easy it can be

  tell application "Finder" set file_list to entire contents of (choose folder with prompt "Please select directory.") end tell 

if you need a list of file names you can 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 

In connection with the comments that follow this post, I asked a group of business experts to consider this issue and evaluate our answer openly, and I received a response from thsis

"What about smallpox on both houses ?:-)

Yes, all the content does exactly what you say - but it easily chokes large folders and takes forever (and a day if you're at 10.6). This is normal for little things, such as extracting all files of the same type from a folder that you know will only contain a small number of files.

The recursive method also works well, but it uses a “list folder” and the list of dictionaries for it says that it is out of date and we should not use it anymore. "

So, I hear, declare that I am wrong! both of these solutions are valid, but have smallpox or holes in their use. My words to Philip. If he decides to edit his answer (because this is the only way to change my voice), I will be happy to come back and give him +1

+10
source share

Wow this is pretty late, but I checked and it works.

 tell application "Finder" to set folder_root to (choose folder with prompt "Please select directory.") set fl to {} dump_folder(folder_root) on dump_folder(f) global fl tell application "System Events" to set end of fl to (get the POSIX path of f) tell application "Finder" to set nfl to (the items of the contents of f) repeat with nf in nfl dump_folder(nf as alias) end repeat end dump_folder fl 
0
source share

All Articles