set these_apps to {"Finder", "TeamViewer", "Script Editor"}
tell application "System Events" to set quitapps to name of every application process whose background only is false and name is not "Finder"
set y to 1
repeat with an_app in quitapps
set the_name to item y of quitapps
if the_name is not in these_apps then
quit application the_name
end if
set y to y + 1
end repeat
This is an ASObjC script that does what the OP wants. It tends to be faster than a basic AppleScript that does the same thing.
use framework "AppKit"
use framework "Foundation"
use scripting additions
on main()
set doNotClose to {"Finder", "Script Editor", "Teamviewer"}
set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{doNotClose}
set theApps to theApps's filteredArrayUsingPredicate:thePred
repeat with anApp in theApps
anApp's terminate()
end repeat
end main
main()
determines itself the name of the application executing it,
does not bring up dialog boxes asking to save the document:
set currentApplicationName to name of current application
tell application "System Events" to set quitappsIDs to unix id of every application process whose ¬
background only is false and ¬
not (name is "Finder") and ¬
not (name is currentApplicationName) and ¬
not (name is "TeamViewer") -- adding other applications
if quitappsIDs is {} then return
set {TID, text item delimiters} to {text item delimiters, " "}
set quitappsIDs to quitappsIDs as text
set text item delimiters to TID
do shell script "kill " & quitappsIDs