Re: Simple folder walk...I think
Re: Simple folder walk...I think
- Subject: Re: Simple folder walk...I think
- From: "Marc K. Myers" <email@hidden>
- Date: Fri, 16 Aug 2002 15:16:45 -0400
- Organization: [very little]
>
Date: Fri, 16 Aug 2002 09:17:13 -0500
>
From: "John Tuttle" <email@hidden>
>
To: email@hidden
>
Subject: Simple folder walk...I think
>
>
I'm attempting to write a short script which will walk through a folder
>
which contains text files and other subfolders. When a text file is
>
encountered an action is performed on the file. The idea is to make changes
>
to all text files within all nested folders under the parent folder.
>
>
This seems to be a simple idea but I have run into problems.
>
>
I though that this operation would be a good candidate for a couple of
>
handlers; one to act on a text file when it is encountered and the other to
>
act on a folder when it is encountered.
>
>
The folder handler works (I think) but I get an error when the file handler
>
runs. Any ideas? The script is below, also, any constructive comments on
>
structure are welcome...lots to learn!
>
>
Thanks
>
>
Script:
>
>
set mytext to ""
>
tell application "Finder"
>
activate
>
--choose the parent folder
>
set myfolder to choose folder
>
--get a list of all items within the folder to process
>
set mylist to every item of myfolder as list
>
-- get an item count for the list for the repeat loop
>
set mycount to count items in myfolder
>
>
--begin repeat loop for every item in the parent folder
>
set x to 1
>
repeat mycount times
>
--grab the first item in the list
>
set myitem to item x of mylist
>
--determine if the item is a folder
>
set myclass to class of myitem
>
--if item is a folder run the folder handler, otherwise run the file
>
handler
>
if myclass = "folder" then
>
HandleFolder(myitem)
>
else
>
HandleFile(myitem)
>
end if
>
set x to x + 1
>
end repeat
>
end tell
>
-----
>
on HandleFolder(myitem)
>
set folderlist to every item of myitem as list
>
set foldercount to count items in myitem
>
set F to 1
>
repeat foldercount times
>
set myitem to item F of folderlist
>
set fi_class to class of myitem
>
if fi_class = "folder" then
>
my HandleFolder(myitem)
>
else
>
HandleFile(myitem)
>
end if
>
set F to F + 1
>
end repeat
>
end HandleFolder
>
----
>
on HandleFile(myitem)
>
>
end HandleFile
This is the approach I'd take:
procFldr(choose folder)
on procFldr(aFldr)
tell application "Finder"
set fldrList to folders of aFldr
set fileList to files of aFldr
end tell
repeat with aFile in fileList
set theFile to (aFile as alias)
my procFile(theFile)
end repeat
repeat with aFldr in fldrList
my procFldr(aFldr)
end repeat
end procFldr
on procFile(aFile)
if file type of (info for aFile) is "TEXT" then
-- do something with the file
end if
end procFile
Note that I don't put anything in the Finder tell block that doesn't
need to be there. I use the Finder to generate lists of files and
folders and that's it. Everything else is done within AppleScript
itself and the "info for" scripting addition command.
Marc K. Myers <email@hidden>
http://AppleScriptsToGo.com
4020 W.220th St.
Fairview Park, OH 44126
(440) 331-1074
[8/16/02 3:12:55 PM]
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.