List Folder Contents

This script will list a folder’s contents returning full paths as strings and limit the list to specific file types. The script can also process subfolders (recursion).

OS version: Any

set the_folder to (path to desktop) as string
set file_types to {"osas", "TEXT"} --file types, set to {} and inc_folders to true to just return folders
set with_subfolders to true --list subfolders or not (recursion)
set inc_folders to false --include folders in the list of files to return
set the_files to get_folder_list(the_folder, file_types, with_subfolders, inc_folders)
return the_files

on get_folder_list(the_folder, file_types, with_subfolders, inc_folders)
	set the_files to {}
	tell application "Finder" to set folder_list to every item of folder the_folder
	repeat with new_file in folder_list
		try
			set temp_file_type to file type of new_file
		on error
			set temp_file_type to "fold"
		end try
		if file_types contains temp_file_type or file_types = {} then set the_files to the_files & {new_file as string}
		if temp_file_type = "fold" then
			if inc_folders = true and file_types ? {} then set the_files to the_files & {new_file as string}
			if with_subfolders = true then set the_files to the_files & my get_folder_list((new_file as string), file_types, with_subfolders, inc_folders)
		end if
	end repeat
	return the_files
end get_folder_list

I tried compiling this script and it says found unknown token and highlights the ‘?’ in the get_folder_list handler.

Is ‘?’ an operator and is it still in use in Applescript? It’s not listed in my applescript reference book.

Bill.

Hi,

this is probably an text encoding problem, the missing character is ≠(is not).
the topic is quite old, since Tiger the same task can be performed incredibly faster using Spotlight metadata

Funny how it showed up all of a sudden given that it’s from 2003. Further, the Trash Man designation indicates that the author (whoever it was) did not submit a still-operable email address.

I know a lot of people hate resurrecting old threads but I think it says a lot about the searchability of the forum and the logevity/compatibility of the Mac overall.

Trying to implement this code using a user selection rather then the dialogue box. Changed all the variables to globals (file_types, inc_sub_folders, etc.) and just pass the user selection in as the_folder. I get an error though that I’ve been unable to sort out. Here’s the Events/Replies output:

tell application “Finder”
get selection
→ {folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk}
get folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk
→ “Macintosh HD:Users:angelomi:Library:Sounds:”
get_folder_list(“Macintosh HD:Users:angelomi:Library:Sounds:”, {}, “Yes”, “Yes”, “HFS”)
→ error number -1708

Result: error “Finder got an error: Can’t continue get_folder_list.” number -1708

Any thoughts/help would be appreciated.

Browser: Safari 534.52.7
Operating System: Mac OS X (10.6)

This is a common error. The Finder has no idea what a handler is. Put my in front of the handler


my get_folder_list("Macintosh HD:Users:angelomi:Library:Sounds:", {}, "Yes", "Yes", "HFS")

Stefan,

Thanks for the reply but that’s not the issue. In fact, I’ve tried it with and without the my and I’ve also tried the original script and received a different error - something about it trying to make the items into and integer. Here is the error I get when I did have the “my” in there:

tell application “Finder”
get selection
→ {folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk}
get folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk
→ “Macintosh HD:Users:angelomi:Library:Sounds:”
get every item of folder “Macintosh HD:Users:angelomi:Library:Sounds:”
→ {folder “AIM®” of folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk}
get file type of folder “AIM®” of folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk
→ error number -1728 from file type of folder “AIM®” of folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk
get folder “AIM®” of folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk
→ “Macintosh HD:Users:angelomi:Library:Sounds:AIM®:”
get kind of folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk
→ “Folder”
get every item of folder (folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk)
→ error number -1700 from folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk to integer
Result:
error “Finder got an error: Can’t make folder "Sounds" of folder "Library" of folder "angelomi" of folder "Users" of startup disk into type integer.” number -1700 from folder “Sounds” of folder “Library” of folder “angelomi” of folder “Users” of startup disk to integer

Browser: Safari 534.52.7
Operating System: Mac OS X (10.6)

Believe me, it was the issue. :slight_smile:
A call of a handler within an application tell block requires the keyword my to reference AppleScript itself

The second error message has another reason:

 get every item of folder (folder "Sounds" of folder "Library" of folder "angelomi" of folder "Users" of startup disk)

the expression within the parentheses represents a Finder folder specifier.
The additional keyword folder in front of an object specifier throws the error. Omit it

Sorry for implying I didn’t believe you, I just thought the two errors were related. Thanks for explaining they aren’t. Now need to fix the overflow error I’m getting when I get to the lowest later. For some reason my check for the list being empty isn’t doing what I expect.

Thanks!!!

Trying to run down my error that’s coming up on a recursive call to my delete function. If I select an individual file, all works fine. If I select the folder that file (and others) is contained in, it errors out when it starts looking at the same file that if I run with that as the selection is fine.

With the limited debugging ability of AS, I’ve managed to (I think) find what the problem is; I just don’t know how to correct it. First the script (please ignore the logs and dialogs as they are for debugging):


on Delete_Selections(selected_items)
  log "Begin Delete_Selections"
  #  display dialog "CurrentItem:" & (selected_items)
  repeat with tmp_File in selected_items
    #  display dialog "Selection:" & (tmp_File)
    log tmp_File

    try
      log "TRY set tmp_Kind"
      set tmp_Kind to kind of tmp_File
    on error errString
      log "ERROR set tmp_Kind: " & errString
      set tmp_Kind to "Folder"
    end try
    log "TRY end"
    log tmp_Kind
		
    if (word 1 of tmp_Kind) = "Application" then
      beep 5
      # open tmp_File using application file id "com.apple.AppDelete"
    else if tmp_Kind = "Volume" then
      beep 4
      # eject
    else if tmp_Kind = "Folder" then
      display dialog "recursive with:" & (tmp_File)
      try
        log "Try"
        tell application "Finder" to set theItems to count of items of tmp_File
      on error errString
        log "On Error: " & errString
        tell application "Finder" to set theItems to count of files of tmp_File
      end try
      log "End Try"
      display dialog "Folder " & (tmp_File) & "has " & (theItems) & " items in it."

      if theItems > 0 then
        set tmp_Files to my get_folder_list(tmp_File, file_types, with_subfolders, inc_folders, use_posix_path)
        log tmp_Files
        my Delete_Selections(tmp_Files)
        beep 3
      end if
    else
      beep 1
      # move tmp_File to trash
    end if
  end repeat
end Delete_Selections

The result of tmp_File when I run with just the file selected is (note the 3rd line):

(Begin Delete_Selections)
tell application “Finder”
(document file AccountPrefs.plist of folder Default of folder Users of folder Adium 2.0 of folder Application Support of folder Library of folder angelomi of folder Users of startup disk)
end tell
(TRY set tmp_Kind)
tell application “Finder”
get kind of document file “AccountPrefs.plist” of folder “Default” of folder “Users” of folder “Adium 2.0” of folder “Application Support” of folder “Library” of folder “angelomi” of folder “Users” of startup disk
→ “XML Property List File”
end tell
(TRY end)
(XML Property List File)

When I run it with the parent folder selected, it runs fine and gets the list of files in the folder and returns it. Then when I recursively call Delete_Selections with my new list, the result is (note the 2nd line is tmp_File):

(Begin Delete_Selections)
(Macintosh HD:Users:angelomi:Library:Application Support:Adium 2.0:Users:Default:AccountPrefs.plist)
(TRY set tmp_Kind)
(ERROR set tmp_Kind: Can’t get kind of “Macintosh HD:Users:angelomi:Library:Application Support:Adium 2.0:Users:Default:AccountPrefs.plist”.)
(TRY end)
(Folder)

As I’ve not been using AS that much I’m not sure what’s going on except that the variable is obviously in a different format from

(document file AccountPrefs.plist of folder Default of folder Users of folder Adium 2.0 of folder Application Support of folder Library of folder angelomi of folder Users of startup disk)

to

(Macintosh HD:Users:angelomi:Library:Application Support:Adium 2.0:Users:Default:AccountPrefs.plist)

Just don’t know why nor how to fix it. Any suggestions of nudges in the right direction would be appreciated.

sorry, the debugging ability is quite the same as the error message reading ability of the scripter :wink:

The error message says that it can’t get the kind of a literal string, which is correct,
kind is a property of a Finder item specifier, a string doesn’t have any properties

When the script starts, you get a list of Finder item specifiers, the recursive handler returns a list of literal strings.

Either you change the handler to list Finder items instead of just the paths or you change this line

tell application "Finder" to set tmp_Kind to kind of item (tmp_File as text)

The coercion is necessary in case tmp_File is already a Finder object

Thanks, that worked. Had to coerce in another line as well. Still has problems though getting the counts. If the folder has files or is empty, works fine. But, if the folder just has other folders in it, it thinks it’s empty. I’ll keep looking at it.

Thinking that since all of the problems I seem to be having occur when I try to count items and there are folders there or only folders, that instead of running down the list of files alphabetically, do a search for all the folders and then recursively step through those until I get no folders. Then when I count, it’s either empty or only files. Then, as I step back up the list, I can process each folder to the same way without worrying about underlying folders being in the way.

Make sense or am I going about this all wrong?

Here is an alternative that created an indented list as a .txt file.

property file_id : null

set this_folder to choose folder with prompt "Choose the root folder"
set log_file_path to (path to desktop as string) & "Folder Inventory.txt"
set file_id to open for access file log_file_path with write permission
write ("Start " & (current date) & return) to file_id starting at eof
my do_subfolders(this_folder, "")
write (return & return) to file_id starting at eof
close access file_id
say "finished"
return

on do_subfolders(this_folder, name_prefix)
	tell application "Finder"
		log name_prefix & (name of this_folder) as string
		write (return & name_prefix & (name of this_folder) as string) to file_id starting at eof
		set file_list to every file in this_folder
		repeat with this_file in file_list
			log name_prefix & "      " & (name of this_file) as string
			write (return & name_prefix & "      " & (name of this_file) as string) to file_id starting at eof
		end repeat
		set folder_list to every folder in this_folder
		repeat with this_subfolder in folder_list
			my do_subfolders(this_subfolder, "  " & name_prefix)
		end repeat
	end tell
end do_subfolders
every paragraph of (do shell script "find " & quoted form of POSIX path of (path to desktop folder) & " -iname '*.txt'")

will do the same (you can also use mdfind to filter on metadata).

This is a great solution for what I’m looking for, but is there an easy way to add a line at the top of the text document that will tell me how many items were in the folder as well? An item count?

Hello.

This should give you the file count:

set fc to (do shell script "mdfind  -onlyin " & quoted form of (POSIX path of this_folder) & " ' kMDItemKind == * ' |wc -l") as number

Oh my god! This script is wonderful!
Im just starting trying understand scripting and i cant stop being amazed with some scripts like this one; what would you need to do to make it a drag and drop app? I have tried automator but im missing something, or everything :rolleyes:

would you help guys? i have another 2 scripts that im trying the same thing on, this one is far more interesting thou

Cheers!

D