Thursday, April 24, 2014

#1 2003-07-06 12:35:52 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

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

Applescript:

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


One mans trash is another mans treasure

Filed under: Finder

Offline

 

#2 2008-12-31 01:57:29 pm

billtubbs
Member
From: Vancouver
Registered: 2007-08-19
Posts: 1

Re: List Folder Contents

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.


Filed under: token, unknown, ?

Offline

 

#3 2008-12-31 02:05:45 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10270
Website

Re: List Folder Contents

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


regards

Stefan

Offline

 

#4 2008-12-31 02:41:45 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4626

Re: List Folder Contents

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.


iMac & MBP both running OS X 10.9.1

Offline

 

#5 2012-01-22 11:03:32 am

ShosMeister
Member
From: Harpers Ferry, WV
Registered: 2007-05-05
Posts: 6
Website

Re: List Folder Contents

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)

Offline

 

#6 2012-01-22 11:18:36 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10270
Website

Re: List Folder Contents

ShosMeister wrote:

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

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

Applescript:


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


regards

Stefan

Offline

 

#7 2012-01-22 11:57:57 am

ShosMeister
Member
From: Harpers Ferry, WV
Registered: 2007-05-05
Posts: 6
Website

Re: List Folder Contents

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)

Offline

 

#8 2012-01-22 12:10:14 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10270
Website

Re: List Folder Contents

ShosMeister wrote:

Thanks for the reply but that's not the issue.

Believe me, it was the issue. 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:

Code:

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

Last edited by StefanK (2012-01-22 12:13:01 pm)


regards

Stefan

Offline

 

#9 2012-01-22 12:47:41 pm

ShosMeister
Member
From: Harpers Ferry, WV
Registered: 2007-05-05
Posts: 6
Website

Re: List Folder Contents

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!!!

Offline

 

#10 2012-01-24 05:07:11 pm

ShosMeister
Member
From: Harpers Ferry, WV
Registered: 2007-05-05
Posts: 6
Website

Re: List Folder Contents

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):

Applescript:


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.

Offline

 

#11 2012-01-25 02:32:47 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10270
Website

Re: List Folder Contents

ShosMeister wrote:

With the limited debugging ability of AS… ,

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

(*ERROR set tmp_Kind: Can’t get kind of "Macintosh HD:Users:angelomi:Library:Application Support:Adium 2.0:Users:Default:AccountPrefs.plist".*)

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

Applescript:

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


regards

Stefan

Offline

 

#12 2012-01-25 05:26:46 pm

ShosMeister
Member
From: Harpers Ferry, WV
Registered: 2007-05-05
Posts: 6
Website

Re: List Folder Contents

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.

Offline

 

#13 2012-01-27 04:30:31 pm

ShosMeister
Member
From: Harpers Ferry, WV
Registered: 2007-05-05
Posts: 6
Website

Re: List Folder Contents

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?

Offline

 

#14 2012-06-25 09:49:26 am

Rich_Cohen
Member
Registered: 2009-12-01
Posts: 25

Re: List Folder Contents

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

Applescript:

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

Offline

 

#15 2012-06-25 04:48:08 pm

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 1765

Re: List Folder Contents

Applescript:

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).

Last edited by DJ Bazzie Wazzie (2012-06-25 04:49:32 pm)


Kind regards

You can chat here about AppleScript

Offline

 

#16 2013-04-09 09:44:16 am

phlacin
Member
Registered: 2008-03-01
Posts: 16

Re: List Folder Contents

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?


Rich_Cohen wrote:

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

Applescript:

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

Offline

 

#17 2013-04-09 11:50:59 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2254
Website

Re: List Folder Contents

Hello.

This should give you the file count:

Applescript:

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

Last edited by McUsrII (2013-04-09 12:46:13 pm)


Filed under: mdfind

Offline

 

#18 2013-10-18 08:21:54 am

ditomiranda
Member
Registered: 2012-08-27
Posts: 2

Re: List Folder Contents

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 roll

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

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.049 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)