My script is not working – pls help

I'm trying to write a script that brings up a dialog box, with the options "Yes", "No", and "Quit all apps".
I just can't get my buttons to work. I want it to that when I press "Yes", it quits the frontmost app, when I press "No", the alert box disappears, and when I press "Quit all apps", I want every app to quit except Finder and BTT.
here is my script:

global frontApp, frontAppName
tell application "System Events"
	set frontApp to first application process whose frontmost is true
	set frontAppName to name of frontApp
	tell process frontAppName
	end tell
end tell

set buttonSet to {"Quit all apps", "No", "Yes"}

return display alert "⌘Q" message "Are you sure that you want to quit " & frontAppName & "? 
Any unsaved changes will be lost." buttons buttonSet as warning

if the button returned of the result is "Yes" then
	tell application frontApp
		quit
	end tell
	
else if the button returned of the result is "Quit all apps" then
	-- get list of open apps
	tell application "System Events" to set theapps to name of every application process whose visible is true and name is not "Finder"
	set keepapp to {"BetterTouchTool"}
	-- quit each app
	repeat with closeapp in theapps
		if closeapp is not in keepapp then quit application closeapp
	end repeat
	
else
	return "cancelled"
end if

help is appreciated
thanks

tell application "System Events" to set FrontmostApp to name of application processes whose frontmost is true

display dialog "Are you sure that you want to quit " & FrontmostApp & "? Any unsaved changes will be lost." buttons {"Yes", "No", "Quit all Apps"} with title "⌘Q" default button 1 cancel button "No"

if the button returned of the result is "yes" then
	tell application (path to frontmost application as text) to quit
	
else if the button returned of the result is "Quit all Apps" then
	-- get list of open apps
	tell application "System Events"
		set allApps to displayed name of (every process whose background only is false and frontmost is false) as list
	end tell
	
	-- leave some apps open 
	set exclusions to {"Finder", "BetterTouchTool"}
	
	-- quit each app
	repeat with thisApp in allApps
		set thisApp to thisApp as text
		if thisApp is not in exclusions then
			tell application thisApp to quit
		end if
	end repeat
	
end if

This should do it.


Here the script to get an alert style window:

tell application "System Events" to set FrontmostApp to name of application processes whose frontmost is true

display alert "⌘Q" message "Are you sure that you want to quit " & FrontmostApp & "? Any unsaved changes will be lost." as critical buttons {"Quit all Apps", "Yes", "No"} default button 2 cancel button "No"

if the button returned of the result is "yes" then
	tell application (path to frontmost application as text) to quit
	
else if the button returned of the result is "Quit all Apps" then
	-- get list of open apps
	tell application "System Events"
		set allApps to displayed name of (every process whose background only is false and frontmost is false) as list
	end tell
	
	-- leave some apps open 
	set exclusions to {"Finder", "BetterTouchTool"}
	
	-- quit each app
	repeat with thisApp in allApps
		set thisApp to thisApp as text
		if thisApp is not in exclusions then
			tell application thisApp to quit
		end if
	end repeat
	
end if

Even though I do not understand the sense of the script. Any app that does not automatically a backup before quitting does already display a native alert:

got this error when running the alert one

You didn't paste the last end if line.

oh lol, missed it

this is awesome! one problem, the quit all apps button quits all apps except the frontmost app. I'm wanting it to include the frontmost app, is this possible?

Upsi my bad.

Replace the -- get list of open apps paragraphs with this one:

-- get list of open apps
	tell application "System Events"
		set allApps to displayed name of (every process whose background only is false or frontmost is true) as list
	end tell

(Basically the line just changed "and frontmost is false" to "or frontmost is true" - this way you quit all apps that are open but hidden by ⌘H and the frontmost app, but not background apps like BTT -when no window is open-, Alfred, or such helpers).

Caliguvara saves the day again! thanks so much!

1 Like