I concur that, if you really must force-quit an application, ‘killall’ is a pretty good way to go about it.
Nevertheless, since part of Matt-Boy’s question relates to the general possibilities of UI scripting (for a variety of purposes), the subject might be worth exploring a little further.
A couple of things struck me recently, while trying to get a UI script to work on both a French-speaking Panther machine, as well as an English Tiger. Firstly, UI scripting might involve identifying various elements (such as menu items) by name - so, if a more portable script is required, some consideration needs to be given to localization issues. (However, let’s leave that little complication aside for the moment.)
The other point is that, in terms of UI scripting, there seem to be some significant differences between Panther and Tiger. I’m not necessarily referring to just a few changes in syntax or a handful of extra features, either. While much of the AppleScript language tends to remain fairly consistent, with due consideration given to legacy code, etc., it seems quite a different matter when it comes to the user interface, which is still evolving at quite a pace. From one upgrade to the next, even an apparently simple dialog panel (while appearing fairly similar to its predecessor) may turn out to have been structurally altered ‘under the hood’.
That’s probably why Matt-Boy’s original (Panther) code didn’t fare too well on Adam’s Tiger machine - and why the following examples may well fall flat on anything pre-Tiger, too. (Nevertheless, I hope there’s something in them that might prove useful…)
Purely as a UI scripting exercise, I thought it might be interesting to attempt a force-quit routine via the UI - although this first effort doesn’t attempt to negotiate the dock:
set p to "TextEdit"
tell application "System Events" to if process p exists then
key code 53 using {command down, option down}
tell window "Force Quit Applications" of process "loginwindow"
select (first row of table 1 of scroll area 1 whose value of text field 1 is p)
click button "Force Quit"
click button "Force Quit" of sheet 1
click (first button whose description is "close button")
end tell
end if
Anyway, lest I be accused of side-stepping the ‘click and hold’ issue, mentioned earlier in the discussion, I guess we’d better move on to some Dock/UI scripting. While a variation of this next example can certainly produce a force-quit, the result is difficult to visually distinguish from a regular quit - especially since you won’t see any menu names change (as you might when you manually click the icon and press the option key). The “Hide Others” command should provide a better demonstration, since it’s much easier to see what it does - compared to the “Hide” command.
set p to "TextEdit"
tell application "System Events" to if process p exists then tell process "Dock"
tell UI element p of list 1
perform action "AXShowMenu"
tell menu item "Hide Others" of menu 1 to if exists then select
keystroke return
end tell
end tell