6.5. Joining Lists Together
On Section 4.3 you learned how to use ampersands to
link strings together (to
concatenate them, in nerd-ese). You can do the same
thing for lists, too, which is a great way to join related items
under one virtual roof. For
instance:
--Example 1:
set combinedList to {"Tomato", "Carrot", "Rutabega"} & {"Broccoli", "Pepper"}
--combinedList is now {"Tomato", "Carrot", "Rutabega", "Broccoli", "Pepper"}
--Example 2:
set bigList to (items 1 thru 3 of combinedList) & ¬
(items 2 thru 4 of combinedList)
(* bigList is now {"Tomato", "Carrot", "Rutabega", "Carrot", "Rutabega",
"Broccoli"} *)
--Example 3:
set luckyNumbers to {1, 6, 28} & 496
--luckyNumbers is now {1, 6, 28, 496}
These three examples each illustrate an important aspect of
concatenating lists:
Example 1 shows how you can
concatenate two separate lists to create one unified list. That would
be useful if you wanted to merge a list of your first-grade and
second-grade students, for example, into a single all-student master
list. Example 2 demonstrates that you can
concatenate different sections of the same list.
If you wanted to put together a list of only students whose names
start with A, E, I, O, and U (for your vowel-themed class party), for
instance, you could use concatenation to assemble a new list from
these separate subgroups (the list of students whose first names
begin with A, concatenated with the list of students whose first
names begin with E, and so on). Example 3 shows that you can append
a single item to a list, using concatenation as well. If a new
student transferred into your class, for example, you could
concatenate the student's name directly to your
existing class list.
 |
Behind the scenes, the code from Example 3 is actually turning the
number 496 into a single-item list. That means AppleScript is
actually concatenating this:
|
|
set luckyNumbers to {1, 6, 28} & {496}
 |
Why does that matter? It shows that AppleScript can only concatenate
two objects that are the same type (in this case, two lists). If you
try to concatenate two objects that are of
different types (like an alias and a number),
AppleScript can't combine the two objects, so it
simply throws them in a list together.
|
|
As these examples show, list concatenation is a powerful tool in any
AppleScripter's toolbox. For further
list-concatenating inspiration, though, just read on.
6.5.1. Merging File Lists
If you want to display all the items in a particular folder,
you can do it easily with the
choose from
list command:
tell application "Finder"
set allApps to the name of every file in folder "Applications"
end tell
choose from list allApps
Or, if you'd like to present a dialog box of all the
files in the frontmost Finder window, you can
modify your script to do that, too:
tell application "Finder"
set allFiles to the name of every file in the front window
end tell
choose from list allFiles
Even better, though, would be if your script could summarize all the
files in all open Finder windows. That way, you
wouldn't have to go searching through every window
onscreen just to find the one that contains a particular file.
You can pull off this stunt by using the word
every twice: the first time to locate all open windows,
and the second time to get all the files in those windows.
Here's how it would look in your script
tell application "Finder"
set allFiles to the name of every file in every Finder window
end tell
choose from list allFiles
Now, when you run your script, you'll see something
like Figure 6-6.
Unfortunately, the list this script displays is passivenothing
happens if you double-click a file's name. To be
truly useful, the dialog box should take the file you select, figure
out which window it came from, and open the file for you
automatically.
To do that, your script needs to iterate through
all the open Finder windows, and search for the
window that contains the file you selected. Here's
how:
tell application "Finder"
set allFiles to the name of every file in every Finder window
end tell
set chosenFile to (choose from list allFiles)
tell application "Finder"
set allWindows to every Finder window
repeat with curWindow in allWindows
if (name of every file in curWindow) contains chosenFile then
set fileInCorrectLocation to file (chosenFile as string) ¬
of curWindow
open fileInCorrectLocation
end if
end repeat
end tell
Here's how these extra lines work:
After getting a list of every file in every Finder window, the script
asks you which file you'd like to open. Whichever
you select, the script puts the file's name into the
chosenFile variable. The script gets a list of every open Finder window and stores that
list in allWindows. Each time the repeat with statement runs, the
script sets curWindow to the next item in the
list of windows. (See Section 6.4.1 for more on how repeat
with statements work.) The script sees whether curWindow (the window
it's currently checking) contains the file you
selected. If it does, the script opens the file straight from that
window. Otherwise, the script runs the repeat
statement again, checking the next Finder window
to see if it contains the file you chose.
By the time the script finishes running, it will have found the
window that contains your chosen file and opened the file for you.
Mission accomplished!
Gem in the Rough Listing Folders and Disks | Like many things in AppleScript, there's more than
one way to get a list of the items in a
folder. The list
folder command, from the Standard
Additions dictionary (Sidebar 3.5), is perfect if
you know exactly what folder you want to get the items from. For
example, this command: set systemItems to (list folder ¬
":System:Library:")
obtains a list of the files and folders inside your System folder and
puts that list into the systemItems variable. That command, however, would also include
invisible files and folders in the list (you can
tell because they start with a period). If you'd
rather omit such invisible itemsbecause
they're too geeky for you, for instancejust
specify without invisibles: set systemItems to (list folder ¬
":System:Library" without invisibles)
One nice thing about the list folder command is
that it's flexible enough to work even outside of a
tell statement directed at the Finder. The
not-so-great thing about list folder, though, is
that it doesn't give you nearly as much control over
which items appear in your list as using the
every keyword with the Finder;
for example, you can't get a list of week-old files
with list folder. However, no matter which of those commands you use to list files and
folders, neither one can list the disks
connected to your computer. For that, you have to use the special
list disks command, which returns a list of all
the CDs, DVDs, floppy disks, and hard drives you can see on your
desktop. |
 |