Is there any way of listing all the files in a folder (and possibly buffering them, but I can work that out myself) without using too many OSAXen?
thanks in advance.
Halabut
: Is there any way of listing all the files in a folder
: (and possibly buffering them, but I can work that out
: myself) without using too many OSAXen?
: thanks in advance.
: Halabut
Hi-
The simple answer is that getting an Applescript list of files in a folder is simple:
tell application "Finder"
set theFolder to (choose folder)
set theFileList to every file of theFolder
end tell
But this is an Applescript “list” object, not a text list, and it only lists files, not folders. You need to use “item” instead of “file” to list everything.
You can put the info into a text file by converting the “list” object into textual information and adding it to a text file. Here’s a simple example.
tell application "Finder"
set theFolder to (choose folder)
set theFileList to every item of theFolder
end tell
set theTextList to ""
repeat with i in theFileList
set theTextList to (theTextList & ((i as text) & return))
end repeat
set theFileRef to (open for access "HD:List Text" with write permission)
write theTextList to theFileRef starting at eof
close access theFileRef
There’s almost certainly a better way to do this, but it’s 2 AM, and I’m tired! Hope it gets you started…
Good luck!
Brad
Thanks
Halabut
Thanks for helping out with this problem. Unfortunately, the solution that you provided didn’t really work. In order to test the script you should have a level of at least 4 or 5 subfolders in the structure. It seems to work fine for me when there are no more than three subfolders. Beyond that however, it just isn’t recognizing all of the folders that are contained by the root. Let me know if you have any more ideas.
: Thanks for helping out with this problem. Unfortunately,
: the solution that you provided didn’t really work. In
: order to test the script you should have a level of at
: least 4 or 5 subfolders in the structure. It seems to
: work fine for me when there are no more than three
: subfolders. Beyond that however, it just isn’t
: recognizing all of the folders that are contained by
: the root. Let me know if you have any more ideas.
What follows is a little script which “walks” a tree of nested folders and delivers a count of the total number of folders in the tree. I would think you could easily modify this script the test to count the total number of files of a given kind that are subordinate to a given node in the tree. This technique can be slow for “large” trees.
set folder_name to choose folder with prompt "Select the root folder of the tree"
set folder_count to 0
count_folders(folder_name, folder_count)
display dialog "The total number of folders is: " & (folder_count as string)
on count_folders(folder_name)
global folder_count
-- insert test here assigning count to a suitable global variable --
set folder_count to folder_count + 1
tell application "Finder"
set subordinate_folder_names to every folder of folder_name
repeat with subordinate_folder_name in subordinate_folder_names
my count_folders(subordinate_folder_name)
end repeat
end tell
end count_folders
: Thanks for helping out with this problem. Unfortunately,
: the solution that you provided didn’t really work. In
: order to test the script you should have a level of at
: least 4 or 5 subfolders in the structure. It seems to
: work fine for me when there are no more than three
: subfolders. Beyond that however, it just isn’t
: recognizing all of the folders that are contained by
: the root. Let me know if you have any more ideas.
Hi again-
Well, I was going to suggest a true recursive search like the one that Jim suggested above. Try setting up a script like the one he provided and see if it works more reliably for you.
Another option (one I use often) is the Akua Swwets osax collection. It has a “the entries in” addition that lets you do something just like in the first script I posted for you, but it uses the scripting addition rather than the Finder to look for the files. Again, you might want to try it and see if it works more reliably:
–same script as before, using “the entries in” osax from Akua Sweets 1.36
tell application "Finder"
set search_file_type to "EPSF"
set chosen_folder to choose folder
set the_list to (the entries in (chosen_folder as string) whose types are in
search_file_type to a depth of -1)
set image_count to (number of items of the_list)
display dialog "There are " & image_count & " images in this folder."
end tell
Between the recursion approach and the osax approach, hopefully something will work!
Best of luck!
Brad
Thank you so much for the little handler. It is exactly what I was looking for.
I am working on a very similar problem to this one. I am trying to write a script that searches through a folder and counts every file of type “EPSF”. In order for this script to work properly it has to search all the subfolders of the parent folder, AND all their subfolder, and so on, and so on. However, sometimes, AppleScript is skipping some of the folders. Here is the counting handler:
tell application "Finder"
set search_file_type to "EPSF"
set chosen_folder to choose folder
set the_list to every folder of the entire contents of chosen_folder
set image_count to count (every file of chosen_folder whose file type is search_file_type)
repeat with an_item in the_list
set new_list to (every file of an_item whose file type is search_file_type)
set sub_count to count new_list
set image_count to image_count + sub_count
end repeat
display dialog "There are " & image_count & " images in this folder."
end tell
The problem here is that the variable “the_list” is not always accurate. Sometimes it counts all of the subfolders and sometimes it skips a certain folder’s subfolders. Is there a limit to the level of subfolders that the entire contents property will recognize? Or, is AppleScript running out of memory and disposing of some of the data in the_list (the folder structures can be quite extensive.)? Please, please, help me. This is absolutely driving me nuts!
: I am working on a very similar problem to this one. I am
: trying to write a script that searches through a
: folder and counts every file of type “EPSF”.
: In order for this script to work properly it has to
: search all the subfolders of the parent folder, AND
: all their subfolder, and so on, and so on. However,
: sometimes, AppleScript is skipping some of the
: folders. Here is the counting handler: tell
: application “Finder”
: set search_file_type to “EPSF”
: set chosen_folder to choose folder
: set the_list to every folder of the entire contents of
: chosen_folder
: set image_count to count (every file of chosen_folder
: whose file type is search_file_type)
: repeat with an_item in the_list
: set new_list to (every file of an_item whose file type is
: search_file_type)
: set sub_count to count new_list
: set image_count to image_count + sub_count
: end repeat
: display dialog “There are " & image_count
: & " images in this folder.”
: end tell
: The problem here is that the variable
: “the_list” is not always accurate. Sometimes
: it counts all of the subfolders and sometimes it skips
: a certain folder’s subfolders. Is there a limit to the
: level of subfolders that the entire contents property
: will recognize? Or, is AppleScript running out of
: memory and disposing of some of the data in the_list
: (the folder structures can be quite extensive.)?
: Please, please, help me. This is absolutely driving me
: nuts!
Hi Gregor,
Hmmm, I’d never really tried using the Finder for this (I use osax a lot more than I need to), but I discovered the Finder seems to work fine for this on everything I’ve tried.
I simplified the script structure a bit, so that instead of looking for the folders (and subfolders) and then looking for the files, it just looks for the files directly:
tell application "Finder"
set search_file_type to "EPSF"
set chosen_folder to choose folder
set the_list to every file of the entire contents of chosen_folder whose file type is search_file_type
set image_count to (number of items of the_list)
display dialog "There are " & image_count & " images in this folder."
end tell
This works great on my machine. Give it a try, and if it’s still missing some of your EPS files, let me know, and we’ll try to get it straightened out!
Best of luck!
Brad