6.4. List Processing
One of the most common operations with a list is
iteratingthat
is, going through the list one item at a time.
That's what lets you rename all the files in a
folder, for example, or search all your open documents, one at a
time. No matter what you use it for, iterating can greatly speed
upand simplifyyour scripts.
Gem in the Rough Choosing from All Applications | In the previous script, you used the choose from
list command to display a list of all your open programs.
But what if you want to display a list of all your stored
programsincluding those that
aren't open? The trick is to use the choose
application command. When you run
that, you'll see a dialog box similar to the one
that appears when you choose Script Editor's
File
Open
Dictionary command. You're presented with a list of
every program on your computerincluding hidden programs and
ones written for Mac OS 9 (if you have that installed on your Mac). Using the choose
application command, you can
rewrite your program-activating script to let you choose from
any program on your hard drive: set chosenApp to (choose application)
--chosenApp lists all programs
tell application (chosenApp as string)
activate
end tell
(Incidentally, the reason you have to type chosenApp as
string is because the choose
application command returns
a list of programs you selected. You
can't target a tell statement
at a list of programs, however, so you have to convert your selection
to a string before you can make Mac OS X
activate your selected program.) For more details on the choose from list and
choose application commands, look in the
Standard Additions dictionary (Sidebar 3.5). There,
you'll find additional options like with
prompt (for customizing the message in the dialog box) and
multiple selections allowed (for letting you
select more than one item at once in the dialog box). |
To go through all the items in a list, you have to use a
specialized kind of
repeat statementone where you
specify the list to use:
set eyeColors to {"Brown", "Black", "Blue"}
repeat with curColor in eyeColors --"repeat with" and "in" are the keywords
display dialog curColor
end repeat
Here's how the script works:
The eyeColors variable is set
to a list containing three strings, each representing a
different eye color. Each time the repeat with statement runs,
AppleScript sets curColor to the next item in
your list of eye colors. That means the first time the
repeat statement runs,
curColor will be
"Brown"; the second time it runs,
curColor will be
"Black"; and the last time the
repeat statement runs,
curColor will be
"Blue."
|
repeat with
statements continue looping until
they've reached the very end of your list.
Therefore, if your eyeColors list had 200 items,
your repeat with statement would also run 200
times, looping through each eye color until it reached the end of the
list.
|
|
The display
dialog command presents the current
item of the list (the curColor variable) in a
dialog box. By the time the script ends, therefore, you will have
seen each item in the list appear in a dialog
box.
Thus, when you run the script, a dialog box appears with the word
Brown in it. Click OK, and a window appears with the word Black. Do
this again, and the dialog box displays the word Blue for you. If you
hit the Cancel button (or use Cancel's keyboard
shortcut, -period) at any point along the way, the
current dialog box closes and you won't see any more
dialog boxes from your script.
6.4.1. Batch Renaming
As mentioned earlier, a perfect example of when
you'd want to use iteration is for renaming all the
files in a folder. AppleScript makes it easy, for instance, to add a
certain extension to all the files on your
desktop.
The trick is to use a repeat with statement,
going through the files on your desktop one at a time. By renaming
them individually, you can assure that
they all get
renamed, like this:
set ext to the text returned of (display dialog ¬
"Add what extension to all Desktop files?" default answer ".txt")
tell application "Finder"
set dFiles to every file of the desktop
repeat with curFile in dFiles
set the name of curFile to (the name of curFile & ext)
end repeat
end tell
Here's how it works:
First, the script asks for the file extension you want to add on to
the end of each file's name. (The default
answer option automatically uses .txt if you
don't provide an extension yourself.) The script sets dFiles to the list of every file
on the desktop. By going through this list, you can append your
extension to each file individually. The repeat statement, each time it runs, sets
curFile to the next item in the list. In other
words, curFile holds the file that needs to be
renamed next. The script appends the file extension you specified to
curFile. Since this command is inside the
repeat statement, curFile
represents a different file each time. Therefore, by the time the
script is finished, every file on the desktop will have your chosen
file extension (Figure 6-5).
| Figure 6-5. Left: Imagine your desktop, packed with files, all of them missing file extensions. If you've copied files from Mac OS 9 or a digital camera, for example, there's a good chance they won't have file extensions.Middle: You can fix the situation by running this script. Simply specify the extension you want to use, and click OK. Right: After the script is finished, every file on your desktop will have that extension. |
|
Your script has one problem, though: it blindly appends an extension
to every file on your desktop, regardless of whether a file already
has an extension. For instance, if you have a
file called Geckos.doc, running this script
gives you a file called Geckos.doc.txt
(or whatever file extension you specified).
Luckily, it's simple to fix this problem. Since
every file extension
comes after a period, all you need to do is check whether a file name
contains a period, in order to determine whether
it already has an extension:
set ext to the text returned of (display dialog ¬
"Add what extension to all Desktop files?" default answer ".txt")
tell application "Finder"
set dFiles to every file of the desktop
repeat with curFile in dFiles
if the name of curFile does not contain "." then
(* In other words, if the file being checked doesn't have an
extension, then *)
set the name of curFile to (the name of curFile & ext)
end if --Otherwise, if the file *does* have an extension, do nothing
end repeat
end tell
|
Don't worryany folders or disks you have on
your desktop won't be affected by this script. Since
the script is specifically checking for files,
anything elsewhether folder, disk, or Frisbeewill not
get a file extension.
|
|
Now your script works properly, adding extensions only to files that
don't have them already.
|
If you'd like to change how your script works,
just edit what happens to curFile in your
repeat with statement (so that your script
moves the current file instead of renaming it,
for example). Or, if you'd like to see another
prewritten example of a Finder-based repeat with
script, see Section 11.3.2; there,
you'll learn how to automatically delete all new
files that enter a folder, to keep that folder permanently clean.
|
|
|