The
“CloseApps” script of the next
example displays a list
in a dialog window that
the user may choose from. The list
contains the
names of all of the application processes that are running on the
computer. These include the programs that have a user interface
(e.g., windows and menus that you can interact with) and
faceless background applications (FBAs) such as
Time Synchronizer or File Sharing Extension. FBAs are programs that
work invisibly in the background without interacting with the user.
CloseApps is similar to one of the functions of the Windows NT Task
Manager utility, which lets you select and shut down a process. Figure 14-5 shows the dialog window displayed by this
script. Users may choose one or more processes, and the script will
quit the selected programs.
The script shown in Example 14-3 uses the
choose from
list scripting
addition and a list
of application processes. An
application process
is an element of the
Finder’s application
class. You
can get a list
of all of the currently running app
processes simply by requesting all of the Finder’s
application processes, as in:
tell app "Finder" to application processes
This phrase does not sound syntactically pleasing, but it does the
job. The script gets a list
of all application
processes with the code:
set applist to application processes
It then creates a list
of all of the process names
by getting the name
property of each member of
applist
(which contains the application process
objects) and adding the name
to the
list
(stored in the namelist
variable). An example of the name property of process
“Application Switcher” is naturally
enough “Application Switcher.” The
choose from list scripting addition populates
the window with the list
of process names in
namelist. The user can select one or more of the list names and click
the Close Em button, and the script will send a quit Apple event
withto each of the selected processes.
set applist to {} -- will contain list of process objects set namelist to {} -- will contain list of process names set closelist to {} (* will contain list of process names that the user wants to shut down *) tell application "Finder" set applist to application processes repeat with proc in applist set namelist to namelist & (name of proc) (* get names of each running process *) end repeat end tell Choose from list namelist with prompt "Which open applications do you " &¬ "want to close ?" OK button name "Close Em" cancel button name "Outta Here"¬ with multiple selections allowed set closelist to the result try set closelist_len to (length of closelist) if closelist_len is greater than 0 then repeat with proc in closelist try -- trap any errors caused by quitting the program tell application proc (* send a quit command to each of the selected programs *) quit end tell on error number errNum activate display dialog (proc & "reported error number " & errNum & ¬ " when trying to respond to the quit Apple event.") end try end repeat end if on error number errNum (* this error triggered when the user cancels the program *) if errNum is equal to -1728 then set theMessage to display dialog theMessage else display dialog end if end try