Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quit All Applications using Applescript?

How would I quit all running user applications using Applescript?

like image 945
Brock Woolf Avatar asked Jan 30 '09 12:01

Brock Woolf


People also ask

How do you close all open Applications on a Mac?

Mission Control Plus is a nifty Mac utility that adds a few much needed shortcuts to your macOS functionality. Just make sure Mission Control Plus starts at login and press Option + ⌘ + W to close all active apps.

How do I force quit AppleScript?

use killall for all the processes you want to quit, copy-paste the code into TextEdit, save it as Photos. command on your Desktop, then run in Terminal chmod +x ~/Desktop/Photos. command to make it executable. Now all you have to do is duble-click it anytime you want to restart Photos.

Does Apple still use AppleScript?

AppleScript is a scripting language created by Apple Inc. that facilitates automated control over scriptable Mac applications. First introduced in System 7, it is currently included in all versions of macOS as part of a package of system automation tools.

How do I run AppleScript on Mac?

In the Script Editor app on your Mac, click the Run button in the toolbar, or press Command-R, to execute the commands in your script.


2 Answers

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
like image 64
Brock Woolf Avatar answered Oct 16 '22 04:10

Brock Woolf


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
like image 39
clozach Avatar answered Oct 16 '22 04:10

clozach