I'll apologize upfront for posting another dang question/problem with an AppleScript shut down app. I must have read 1,000 threads relating to this topic.
Sequoia 15.3.2
2025 Apple M4 pro
I've pared down the code for my app to the basics and it has the same problems. The code follows:
on run
tell application "Finder" to activate
tell application "System Events" to set the visible of every process to true
try
tell application "Finder"
set listOfApps to name of every application process whose visible is true and name is not "Finder" and name is not "Script Editor" and name is not {name of me}
end tell
repeat with appToClose in listOfApps
tell application appToClose to quit without saving
end repeat
on error errStr number errorNumber
display dialog "errStr = " & errStr & return & "number = " & number & return & "errorNumber = " & errorNumber buttons {"Quit"} default button 1
return
end try
tell application "Finder" to close every window
tell application "System Events" to shut down
end run
If I comment out the "shut down" command, the code works fine in both the AppleScript Editor and as an app. If uncommented and run as an app, I get one of the following three results when I next boot.
- The computer immediately shuts down.
- The Apple -> Force Quit dialog shows my app name. ie. still running
- The following error appears on the desktop:
Any insights would be appreciated. Fairly new to AppleScript and I find it limited and frustrating.
I'll apologize upfront for posting another dang question/problem with an AppleScript shut down app. I must have read 1,000 threads relating to this topic.
Sequoia 15.3.2
2025 Apple M4 pro
I've pared down the code for my app to the basics and it has the same problems. The code follows:
on run
tell application "Finder" to activate
tell application "System Events" to set the visible of every process to true
try
tell application "Finder"
set listOfApps to name of every application process whose visible is true and name is not "Finder" and name is not "Script Editor" and name is not {name of me}
end tell
repeat with appToClose in listOfApps
tell application appToClose to quit without saving
end repeat
on error errStr number errorNumber
display dialog "errStr = " & errStr & return & "number = " & number & return & "errorNumber = " & errorNumber buttons {"Quit"} default button 1
return
end try
tell application "Finder" to close every window
tell application "System Events" to shut down
end run
If I comment out the "shut down" command, the code works fine in both the AppleScript Editor and as an app. If uncommented and run as an app, I get one of the following three results when I next boot.
- The computer immediately shuts down.
- The Apple -> Force Quit dialog shows my app name. ie. still running
- The following error appears on the desktop:
Any insights would be appreciated. Fairly new to AppleScript and I find it limited and frustrating.
Share Improve this question asked Mar 30 at 18:23 Uncle BillUncle Bill 637 bronze badges1 Answer
Reset to default 0Try this:
on run
delay 2
set keepAlive to {"Finder", "Script Editor", "killApp"} -- latter is name of this applet
tell application "System Events"
set openApps to name of (every process whose background only is false)
end tell
-- optional: list of open applications
set AppleScript's text item delimiters to linefeed
display dialog (openApps as text) with title "Open applications"
repeat with appn in openApps
if appn is not in keepAlive then
tell application appn to quit
end if
end repeat
end run
A couple of notes:
- You don't really need the Finder here. You can get the name property from System Events.