I've been trying to emulate MS Windows behavior on my OS X and close processes that do not have a window.

What I'd really like to do is "quit" the process on clicking the red "x" button. Instead, I've managed to code this workaround, which constantly runs in the background.

(see my question here)

It kind of defeat the purpose of not having background apps running, I suppose.

Since I own my Mac for about 2 weeks, I'm not sure if my script is "good" in terms of memory consumption, etc. (right now: CPU = 0, Memory = 12.3 MB, Threads = 3).

How would you guys do it? I'm open to suggestions.


set app_path to path to current application
set app_name to get name of me
set myPath to path to me
tell application "Finder" to set myFolder to (container of myPath) as string
set commonScript to load script alias ((myFolder) & "FuncoesExtras.scpt")

set WhiteList to {app_name, "App Store", "iTunes", "Finder", "Mail"}


    tell application "System Events"
        repeat with this_app in (get processes whose background only is false and windows is {})
            set NomeDoApp to the name of this_app
            if NomeDoApp is not in WhiteList then
                    tell NomeDoApp to quit
                    log_event("App " & NomeDoApp & " encerrado com sucesso", "FecharProgramas") of commonScript
                on error
                    do shell script "killall " & quoted form of NomeDoApp
                    log_event("Forcando interrupcao do App " & NomeDoApp, "FecharProgramas") of commonScript
                end try
            end if
        end repeat
    end tell

    tell application "System Events" to set myPID to (unix id of processes whose name is app_name)
    do shell script ("/usr/bin/renice 18 " & myPID)

    delay 10

end repeat

1 Answer 1


Maybe a late response but I'm new to codereview but not to AppleScript. The problem with AppleScript and scripts that stays open is that they leak memory. Why? I just blame it on AppleEvents. It's a nice feature, it really is, but it does eats memory somehow. So the longer the script will run the more memory it will eat. Eventually, maybe after 6 months, your fans will start blowing for now reason and the mac becomes significant slower. But maybe it does that after a day, a week or a year. It's hard to tell in AppleScript where and how much a script leaks.

To solve this there are several ways to do this. You can do it with cron (is since Tiger built inside launchd) with a shell script or create a new process of itself and quit. Either way they all remove the repeat loop out of the AppleScript and let that part be handled by other, much better, software. Note: using an idle handler inside AppleScript will eat memory too which should be the nicest solution instead of using a repeat and a delay.

My advise, is that the script won't be eating to much memory so I would spawn a new process of itself just over some time. You're waiting 10 seconds so 360 loops would be enough. You spawn a new process around every hour.

change the repeat to repeat 360 times and add the following code to the end of your AppleScript file.

do shell script "osascript " & quoted form of posix path of (path to me) & " &>/dev/null &"

Note: User interaction (display or choose commands) is not allowed when a script is launched by osascript in Mountain lion or earlier. For any user interaction change the target to SystemUIServer or Finder for instance.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.