Re: Does AppleScript have arrays within the language?
Re: Does AppleScript have arrays within the language?
- Subject: Re: Does AppleScript have arrays within the language?
- From: email@hidden
- Date: Wed, 13 Dec 2000 17:26:53 -0500
On Wed, 13 Dec 2000 17:29:53 -0000, "Nick Middleweek" <email@hidden>
asked,
>
In order to loop through the files in a folder do we have to put them into a
>
list and loop through the list or can we just loop through the files of the
>
folder object?
There are three ways to approach this:
1. Loop through the files of the folder object
2. Get all the files into a list, and loop through the list
3. Tell the finder to do some thing to every file of the folder.
Method 1 looks like this:
tell application "Finder"
repeat with eachFile in files of folder "Foo" of disk "Frisbee"
set comment of eachFile to name of eachFile & " Test 1"
end repeat
end tell
It is very slow, and sometimes produces wrong results, since it repeatedly asks
the Finder for each item of the list of files. (That is, "repeat with x in L"
turns first sets x to the reference, "item 1 of L", then "item 2 of L". In this
case, L is 'every file of folder "Foo" of disk "Frisbee'. So each time
'eachFile' is iterated in the loop, the script asks the Finder for the 'item n
of every file of folder "Foo" of disk "Frisbee"'. In addition to being slow, it
also runs into trouble if the files in the folder change. "item 1 of folder
...' may become "item 2 of folder..." if you add something to the folder. So
you may process one file twice, or skip one, if the folder is changing as you
iterate.
Method 2 looks like this:
tell application "Finder"
set fileList to files of folder "Foo" of disk "Frisbee"
repeat with eachFile in fileList
set comment of eachFile to name of eachFile & " Test 1"
end repeat
end tell
This method will get a snapshot of the contents of the folder in an AppleScript
list, and then walk through the list to operate on each file. In this case, the
list is a list of Finder objects, which is OK because we are asking the Finder
to do the action (set the file's comment, in this case). If you are using some
other tool to manipulate the file (a scripting addition, for example), you might
prefer a list of aliases. So say, "set fileList to files of folder "Foo" of
disk "Frisbee" as alias list" to get that. Also, watch out for special cases.
Your loop should work properly if there is only one file in the folder, or if
their are zero files. Additionally, "as alias list" has the annoying property
that if there is only one file, it can't be turned into an alias list. You can
count the files, and then ask for an alias if there's only one, or ask for an
alias list, trapping the error in a "try" block, and if there is an error, ask
for an alias and put it in a list.
The third method is the fastest, but the most restrictive. It looks like this:
tell application "Finder"
set comment of every file of folder "Foo" of disk "Frisbee" to "Gotcha"
end tell
You only send one message to the Finder, and it iterates through the list
itself. Its very fast (sending messages between applications is very slow), but
can only do the same thing to every item. You can delete them all, or change
their comment to a fixed string, but you can't do something like putting " old"
on the end of every file name, because you can't get a hold of the individual
file names. I tried,
set comment of every file of folder "Foo" of disk "Frisbee" to its name &
"Gotcha"
But in this context, 'its name' is "Finder", not the file's name. And
tell application "Finder"
tell every file of folder "Foo" of disk "Frisbee"
set its comment to its name & "Gotcha"
end tell
end tell
Gives the error message, 'Can't get every file of folder "Foo" of disk
"Frisbee"'
But for moving files to another folder, or deleting them, or changing a label or
setting a fixed comment, the "set every file..." version is the fastest to
write, the easiest to read, and the quickest to execute.
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden