Hello Shane.
Reading your post pushed me to give the content of my applet “tout quitter”.
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
property forTesting : true
-- true --> execute a subset of the script plus informative instructions
-- false --> execute the normal tasks
property cleanApplicationState : false
-- true --> empty the folder "Saved Application State:"
-- false --> doesn't empty the folder -- my current setting
if forTesting then set {everyApps, filteredApps} to {{}, {}}
set runningApplications to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set keepApps to {"Finder", "tout quitter"}
set regularActivationPolicy to current application's NSApplicationActivationPolicyRegular
repeat with anApplication in runningApplications
set appName to anApplication's localizedName() as text
if forTesting then set end of everyApps to appName
if anApplication's activationPolicy() is regularActivationPolicy and appName is not in keepApps then
if forTesting then
set end of filteredApps to appName
else
anApplication's terminate()
end if
end if
end repeat
if forTesting then return {everyApps, linefeed, linefeed, filteredApps}
# Now the applications are cleanly closed
if cleanApplicationState then
# We may delete the Saved Application State datas
try
set p2AS to (path to library folder from user domain as text) & "Saved Application State:"
tell application "System Events" to tell folder p2AS
delete every folder -- some items are folders
delete every file -- some items are aliases
end tell # System Events …
end try
end if
set p2pane to ((path to system preferences as text) & "StartupDisk.prefPane") as «class furl»
# Get four localized strings
set cancelBtn to localized string "CANCEL" in bundle p2pane
set restartBtn to localized string "RESTART" in bundle p2pane
set shutdownBtn to localized string "SHUTDOWN" in bundle p2pane
set prompt to localized string "RESTART?" in bundle p2pane
tell application "SystemUIServer" to display dialog prompt buttons {cancelBtn, restartBtn, shutdownBtn} default button 3 cancel button 1
set |éteindre| to (button returned of result) is shutdownBtn
# Now we may trigger shutdown or restart cleanly. Open documents were saved before.
tell application "System Events"
if |éteindre| then
shut down
else
restart
end if
end tell # System Events
In real life the property forTesting is set to false.
When I set it to true it return :
{{“loginwindow”, “universalaccessd”, “tout quitter”, “talagent”, “SystemUIServer”, “Dock”, “Finder”, “Spotlight”, “com.apple.dock.extra”, “Centre de notifications”, “Box Finder Extension”, “imklaunchagent”, “com.apple.PressAndHold”, “CoreLocationAgent”, “Wi-Fi”, “Agent Photos”, “FolderActionsDispatcher”, “AirPlayUIAgent”, “popCalendar”, “Box”, “TISwitcher”, “SMARTReporter”, “FastScripts”, “fake_mojave”, “TextEdit”, “nbagent”, “HP Device Monitor”, “Box Helper”, “ViewBridgeAuxiliary”, “Legacy Color Picker Extensions (TextEdit)”, “uBlock”, “storeuid”, “Box UI”, “LaterAgent”, “Xcode”, “com.apple.CoreSimulator.CoreSimulatorService”, “BBEdit”, “Legacy Color Picker Extensions (BBEdit)”, “Aperçu”, “com.apple.speech.speechsynthesisd”, “Mail”, “Safari”, “Safari Networking”, “uBlock Safari Icon”, “Contenu web Safari (préchauffé)”, “LibreOffice”, “CoreServicesUIAgent”, “Éditeur de script”, “com.apple.security.pboxd”, “Événements système”}, "
", "
", {“fake_mojave”, “TextEdit”, “uBlock”, “Xcode”, “BBEdit”, “Mail”, “LibreOffice”, “Éditeur de script”}}
In real life, after several months of use I disabled the cleaning of Saved Application State.
Given that, when I reStart the machine, “tout quitter.app” is executed and installed in the Dock.
It’s why it’s listed in previous tests.
I assume that it’s because it has no provision to respond to a quit call that the previous tests brought an infinite running requiring to force quit the Script Editor.
I wish to add a comment about your script.
I feel that it requires some enhancement because, as is, it would fail to keep the Script Editor running.
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{"Finder", "Script Editor"}} -- edit to suit
set theApps to theApps's filteredArrayUsingPredicate:thePred
repeat with anApp in theApps
--anApp's terminate()
log (localizedName() of anApp) as string -- EDITED according to Shane Stanley's comment
end repeat
return:
(tout quitter)
(fake_mojave)
(TextEdit)
(uBlock)
(Xcode)
(BBEdit)
(Mail)
(Safari)
(LibreOffice)
(Éditeur de script)
I had to edit it as :
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
property |NSURL| : a reference to current application's NSURL
property NSPredicate : a reference to current application's NSPredicate
property NSWorkspace : a reference to current application's NSWorkspace
set POSIXPath to POSIX path of (path to application "Script Editor")
(*
set theURL to current application's NSURL's fileURLWithPath:POSIXPath
--> error "Erreur dans Script Editor : NSURL ne comprend pas le message « fileURLWithPath_ »." number -1708 from NSURL
*)
set theURL to |NSURL|'s fileURLWithPath:POSIXPath
set {theResult, theValue, theError} to theURL's getResourceValue:(reference) forKey:"NSURLLocalizedNameKey" |error|:(reference)
set scriptEditor_loc to theValue as text
if scriptEditor_loc ends with ".app" then set scriptEditor_loc to text 1 thru -5 of scriptEditor_loc -- must drop the name extension
(*
set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
--> error "Erreur dans Script Editor : NSWorkspace ne comprend pas le message « sharedWorkspace »." number -1708 from NSWorkspace
*)
set theApps to NSWorkspace's sharedWorkspace()'s runningApplications()
(*
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{"Finder", scriptEditor_loc}} -- edit to suit
--> error "Erreur dans Script Editor : NSPredicate ne comprend pas le message « predicateWithFormat_argumentArray_ »." number -1708 from NSPredicate
*)
set thePred to NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{"Finder", "Script Debugger", scriptEditor_loc}} -- edit to suit
set theApps to theApps's filteredArrayUsingPredicate:thePred
repeat with anApp in theApps
--anApp's terminate()
log (localizedName() of anApp) as string -- EDITED according to Shane Stanley's comment
end repeat
I’m really puzzled by the need to define some properties.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 25 avril 2020 17:02:08
Added “Script Debugger” which is not localized in the list of ‘protected’ applications.