Script to force quit all open applications not working - syntax error

If I put the following in Terminal

kill $(osascript -e ‘tell app “System Events” to unix id of processes where background only is false’|tr -d ,)

it kills every running process.

If I put it in an Apple script, it does not work.

Example:

tell application "Terminal"
	do script "kill $(osascript -e 'tell app "System Events" to unix id of processes where background only is false'|tr -d ,)"
end tell

or

do shell script "kill $(osascript -e 'tell app "System Events" to unix id of processes where background only is false'|tr -d ,)"

I get an syntax error stating: A identifier can’t go after this “"”.

Flagging "System.

What do I do wrong?

You have to escape the double quotes around System Events with backslashes

do shell script "kill $(osascript -e 'tell app \"System Events\" to unix id of processes where background only is false'|tr -d ,)"

The original script about is little strange. Because you can terminate the application processes without Terminal and osascript involved.

And, most important: It is better filter the apps like the Finder.app, to avoid termination of them.

That does the trick!

Thanks!

OK you got my interest. What do you suggest?

And it is fine to reboot the finder too.

One thing that needs to happen is that I should not have to use a password. It needs to kill anything even if it has open applications or stuff that is not saved or a hanging applications. This runs on my blind server and if it hangs this script runs and should kill everything.

Ok, this is the script, which keep the current application (this script) only, and restarts the Finder:


use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
property NSRunningApplication : a reference to current application's NSRunningApplication
property NSWorkspace : a reference to current application's NSWorkspace
property NSPredicate : a reference to current application's NSPredicate

set currApp to NSRunningApplication's currentApplication()
set theApps to NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (SELF IN %@)" argumentArray:{{currApp}}
set theApps to theApps's filteredArrayUsingPredicate:thePred
repeat with anApp in theApps
	anApp's terminate()
end repeat

-- restart the "Finder"
repeat while running of application "Finder" is true
	delay 0.02
end repeat
tell application "Finder" to launch

And, this is the script, which keep the current application and Finder:


use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
property NSRunningApplication : a reference to current application's NSRunningApplication
property NSWorkspace : a reference to current application's NSWorkspace
property NSPredicate : a reference to current application's NSPredicate

set theFinder to (NSRunningApplication's runningApplicationsWithBundleIdentifier:"com.apple.finder")'s firstObject()
set currApp to NSRunningApplication's currentApplication()
set theApps to NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (SELF IN %@)" argumentArray:{{theFinder, currApp}}
set theApps to theApps's filteredArrayUsingPredicate:thePred
repeat with anApp in theApps
	anApp's terminate()
end repeat

Thanks, works.

what is in your opinion the better way to go, your solution or mine? And why would you see it that way? Love to hear.

This task can be performed using plain AppleScript as well (without involving Terminal and osascript). The next plain apple-script keeps current application, terminates other applications and restarts the Finder:


set currAppName to name of current application

tell application "System Events"
	set curAppID to unix id of process currAppName
	set IDs to unix id of processes whose (background only is false) and not (unix id is curAppID)
end tell

set AppleScript's text item delimiters to " "
set IDsString to IDs as string

do shell script "kill " & IDsString

sorry removed

Thanks everybody!