quitting some apps

this code gives me all my open apps

tell application "System Events" to set quitapps to name of every application process whose background only is false and name is not "Finder"

Here is the list of open apps: {“Messages”, “Finder”, “Script Editor”, “Safari”, “Contacts”, “Calendar”, “Notes”, “TeamViewer”}

I need to quit all of them EXCEPT…Teamviewer, Finder and Script Editor. I cant figure out how to keep those open. Any ideas would be helpful. Thanks,

figured it out.

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()

The following script has 3 advantages:

  1. does not use a repeat loop,
  2. determines itself the name of the application executing it,
  3. 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