14

How would I quit all running user applications using Applescript?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144

5 Answers5

11

It's okay... I think I found my answer:

tell application "System Events" to set the visible of every process to true

set white_list to {"Finder"}

try
    tell application "Finder"
        set process_list to the name of every process whose visible is true
    end tell
    repeat with i from 1 to (number of items in process_list)
        set this_process to item i of the process_list
        if this_process is not in white_list then
            tell application this_process
                quit
            end tell
        end if
    end repeat
on error
    tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144
  • Great stuff. One thing to note is that the OS (at least on 10.7.4) automatically activates Finder - with non-minimized windows showing - once the last application quits. If you prefer a clean view of your desktop instead, you can minimize all Finder windows, `tell application "System Events" to click (first button of every window of process "Finder" whose role description is "minimize button")`, and/or close them all, `tell application "Finder"to close every window`. If you close them, minimizing them first prevents flickering. – mklement0 May 14 '12 at 14:12
  • While using the `name` property of the process class _usually_ works, there are apps for which that value differs from the name of the app as displayed in the UI and as understood by the `tell application` statement. Therefore, it is better to use the `displayed name` property (though there may still be cases where even that doesn't work). Thus, the relevant line above should be `set process_list to the displayed name of every process whose visible is true`. – mklement0 May 26 '12 at 22:13
3

After some googling, I found a better approach:

  • It uses background only to build the initial app list, rather than visible is true. The difference is that the other scripts will fail to quit an app that's been hidden with ⌘H.
  • It provides an exclusions list so that, for example, you can prevent your script editor from quitting each time you test the script.

Adapted from a thread on MacScripter.

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

-- leave some apps open 
set exclusions to {"AppleScript Editor", "Automator", "Finder", "LaunchBar"}

-- 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
clozach
  • 5,118
  • 5
  • 41
  • 54
1
tell application "System Events" to set the visible of every process to true

set white_list to {"Finder"}

try   
    tell application "Finder"   
        set process_list to the name of every process whose visible is true   
    end tell   
    repeat with i from 1 to (number of items in process_list)   
        set this_process to item i of the process_list   
        if this_process is not in white_list then   
            tell application this_process   
                quit   
            end tell   
        end if   
    end repeat   
on error   
    tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0   
end try
wch1zpink
  • 3,026
  • 1
  • 8
  • 19
0
    tell application "System Events" to set quitapps to name of every application process whose visible is true and name is not "Finder"

repeat with closeall in quitapps

quit application closeall

end repeat
Jeff
  • 11
  • 3
  • 1
    Welcome to Stackoverflow. Would you mind extending your answer a little bit to explain how it solves the problem? – Nagama Inamdar Mar 24 '15 at 05:54
  • this closes all visible applications, except the finder, safely so that it doesn't close other running processes that may affect the computers background processes. – Jeff Mar 29 '15 at 01:58
0

Yes, but the process names may not match the application names. Therefore, it is better and safer to work with application bundle identifiers that are persistent:

property white_list : {"com.apple.finder", "com.latenightsw.ScriptDebugger8"}

tell application "System Events"
    set bundleIDs to bundle identifier of processes whose background only is false
end tell

repeat with theID in bundleIDs
    if theID is not in white_list then tell application id theID to quit
end repeat
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8