Search code examples
applescriptitunesosx-mountain-lionlast.fm

Is it possible to write an applescript so that when I close one program it also closes another?


The example I can think of that is most relevant to me is to be able to close the last.fm app when I close iTunes. I quite often forget to close last.fm and I find it rather annoying. I'm sure there are other uses...


Solution

  • Yes you can.

    Over on my Blog thecocoaquest I have two posts that cover this.

    The first Post shows you a methods of doing this with a applescript and using a Launch Agent.

    Applescript – Quit or Launch Application if another is or is not running

    Here is one of the examples:

    If I have one App running the second app will launch and will always be running while the first app is running.

    Or when I quit the first App the second app will also quit

    #!/usr/bin/osascript
    #Copyright Mark Hunte 2012
    #http://www.markosx.com/thecocoaquest/kill-one-application-if-another-is-not-running-applescript
    set appMustBeRunning to "xcode"
    set appToKill to "Snippets"
    tell application "System Events"
        set appMustBeRunningID to (unix id of processes whose name is appMustBeRunning)
        set appToKillID to (unix id of processes whose name is appToKill)
    end tell
    if appMustBeRunningID is {} then
        try
            tell application "Snippets" to quit
        end try
    
    else if appToKillID is {} then
        tell application "Snippets" to launch
    end if
    

    The Second post is a revision showing how to add more than one master & slave application

    Applescript – Quit or Launch Application script.. ( Revised )

    Also has an a script for if you just want to run the Applescript as an Application.