最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

shutdown - Applescript shut down issues - Stack Overflow

programmeradmin5浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try 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.
发布评论

评论列表(0)

  1. 暂无评论