I am having problems getting a list of various folder contents. According to my book to get the first level of a disk I should use:
tell application "Finder"
set folder_list to items of disk "USER1:"
end tell
This produces the error, "Unable to get all the items of disk “USER1:” Can someone help me figure out why my book is wrong?
Ultimately, I ma trying to get a list of folders in a folder 2 levels down on that disk (“USER1:Common:Web-updates:”).
Thanks for the reply. That was one of the many iterations I tried but must have had something else wrong at that time. That much now works. However, what I really need is:
tell application "Finder"
set web_folders to list of folder "Common:Web-updates" of disk "USER1"
end tell
Or something of that nature to get a list of the folders in “Common:Web-updates”. TIA.
The only syntax I have been able to get the Finder to accept in my limited experience is
folder “Web-updates” of folder “Common” of disk “User1”
It doesn’t seem willing to use furls or file references or paths or POSIX paths. I haven’t tried aliases. This behavior seems strange, and I would hope it gets opened up to using the other forms eventually. Otherwise you have to keep translating from the furl given by another application to this form for the Finder to use.
I don’t know about “list of.” If that doesn’t work, maybe try “items of” or “files of” or “folders of”? Those automatically return lists, I believe.
By copying “items of” into folder_list, you will also get any files that may be on top level. If you really want only folders, I think you should use “folders of.”
If you want nothing more than a list of subfolder names, you can do this:
set targetFolder to (choose folder)
tell application "Finder"
set web_folders to folders of targetFolder
web_folders
end tell
set subFolder to ""
repeat with i in web_folders
set subFolder to (subFolder) & name of i & return
end repeat
return subFolder
To get a recursive listing of files within subfolders, you can do this (courtesy of Dave Livesy):
global fileList
on listFolder(f, s)
list folder f
repeat with i in the result
if folder of (info for alias (f & i)) then
set fileList to fileList & s & i & "(f)" & return
listFolder(f & i & ":", s & " ")
else
set fileList to fileList & s & i & return
end if
end repeat
end listFolder
set fileList to ""
set theFolder to (choose folder "Select a folder to list:") as string
listFolder(theFolder, "")
set listFile to (theFolder & "Folder Listing")
open for access file listFile with write permission
write fileList to listFile
close access file listFile
or use “List Files” by Alessandro Levi Montalcini, which, according to Mr. Livesy, you can get here: http://www.montalcini.com/ (I’m behind a corporate firewall so I can’t verify this)
Thanks for the suggestion but that doesn’t work either. I still cannot get a list of the contents of my target folder.
Ethan:
Thanks but this will be an unattended script so ‘choose’ will not work. I know what disk and folder I need to use but get various errors no matter what syntax I have tried so far.
I’m not sure if this is what you are looking for or not, and I am assuming that disk “USER1” is a Volume… To list all of the “folders” of your target disk try this…
Thanks for the suggestion but I don’t get it. Why is it necessary to resort to UNIX commands to get what should be (in my mind anyway) a routine and simple Applescript statement? What is the point of Applescript? If I have to use UNIX commands then I might as well write a UNIX shell script and forget Applescript completely. Is Applescript simply provided as a programmer’s torture device with no practical advantage and many disadvantages? What am I missing about the usefulness of Applescript?
I forgot to mention that this is only the first layer of the onion. The ultimate goal is to get a list of folders that can be used in a repeat loop so the loop can manipulate the files in each folder. So whatever the syntax is of getting the folder names, the resulting item list has to be usable by Applscript for that purpose.
Thank you, Rob. that was exactly what I needed and the simplicity I had expected. I guess I must have bungled up my question to make it confusing to everyone and that caused the misfires for responses. My apologies.