简体   繁体   中英

Close App A when App B closes: Mac OS X 10.7.3

Say I have two applications running; App A and App B. What would be the easiest way (or indeed is there anyway) to get App B to close automatically when App A is closed? Note that neither of the apps in question have been developed by me and so I have no control over their internal behaviour.

I am open to any suggestions including those that entail the use of Applescript, Automator, Terminal commands and BASH scripting. I would even consider developing a lightweight Mac OS X application to achieve this.

If you don't need B to exit immediately - if it's OK to wait a few seconds - then you could schedule a periodic background task (using cron or even just iCal) that does something like this:

if not exists (processes where name is A)
   tell application B to quit
end if

Another option, if you want an immediate response, would be to wrap App A in a script that launches it, waits for it to terminate, and then terminates B ( osascript -e "tell application B to quit" ). Then you could just always use that script to launch A.

You could even insert the script into the application bundle so that double-clicking runs your script. You would do this by doing "show package contents" on the application, replacing the <CFBundleExecutable> in <app>\\Contents\\info.plist with your script name, and dropping that script into <app>\\Contents\\MacOS . Then have the script just run the executable that is already there.

Fantastic question. I spent about 10 minutes looking for an old project where I had registered for notifications for when applications quit but couldn't easily find my code. But I did find a potential alternative for you.

If you download Apple's AppList sample code project , you'll see that it is observing the list of NSRunningApplications and when an app quits, it removes that app from the list of running apps in the window. You can take the technique they're using there and when you detect your "application A" quits, you can send a "quit" Apple Event to "application B".

Since you are running Lion, you can use a Cocoa-AppleScript to access Cocoa methods to add your application as an observer, getting notifications when applications quit.

For example, create a new application from the AppleScript Editor > File > New from Template > Cocoa-AppleScript applet . In the run handler, add the application as an observer to get notifications when an application quits:

    set theNotice to current application's NSWorkspaceDidTerminateApplicationNotification
    tell current application's NSWorkspace's sharedWorkspace's notificationCenter
        addObserver_selector_name_object_(me, "appQuitNotification:", theNotice, missing value)
    end tell

Next, add an appQuitNotification handler (this is the handler selector specified in addObserver_selector_name_object_ above), something like:

on appQuitNotification_(aNotification) -- an application quit
    -- aNotification's userInfo record contains an NSRunningApplication instance that we can get properties from
    set theApplication to (aNotification's userInfo's NSWorkspaceApplicationKey's localizedName()) as text
    say "application " & theApplication & " quit" -- for testing
    if theApplication is "this" then tell application "that" to quit -- or whatever
end appQuitNotification_

...and you are done. As long as your application is running, it will get notifications from the system when an application quits. Note that these Cocoa-AppleScript applications can't be run directly from the script editor, so they can be a bit of a pain to debug since there is no event log to look at - you will need to add your own dialogs or whatever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM