Skip to content

Instantly share code, notes, and snippets.

@mikez
Last active April 15, 2022 20:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikez/483462dbf1783af4b0fd96c98d473eed to your computer and use it in GitHub Desktop.
Save mikez/483462dbf1783af4b0fd96c98d473eed to your computer and use it in GitHub Desktop.
Close all apps except the frontmost visible one. macOS only.
#!/bin/bash
# get frontmost app
frontmost_app=$(osascript -e '
tell application "System Events"
POSIX path of (file of application processes whose frontmost is true and visible is true)
end tell
')
escaped_frontmost_app=${frontmost_app//\//\\/}
# write automator workflow to temporary file
tmpfile=$(mktemp)
plutil -convert xml1 -o "$tmpfile" - <<END
{
"actions": [{
"action": {
"ActionParameters": {
"appException": false,
"fileNames": ["$escaped_frontmost_app"],
"saveChanges": true
},
"ActionBundlePath":"\/System\/Library\/Automator\/Quit All Applications.action"
}
}]
}
END
# run automator
automator "$tmpfile"
# cleanup
rm "$tmpfile"
@mikez
Copy link
Author

mikez commented May 29, 2019

If you know a more elegant way, I'm curious! I tried to access the parameters directly in Automator; without luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment