Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like a method for starting a program when another program starts. Specifically, I’d like a to open a MS Word document and have my citation manager open simultaneously (EndNote supports this function within MS Word preferences, but I recently switched citation managers).

I’d like to be able to open any existing Word document on my drive and trigger the second program. The same question was recently answered for the windows 7 environment.

share|improve this question
    
This is a great question, I would also like to know how to close one program when another closes. Can this be done the same way? –  Mike Kormendy Apr 25 at 8:30

2 Answers 2

up vote 2 down vote accepted

Try saving a property list like this as ~/Libary/LaunchAgents/test.plist and loading it with launchctl load ~/Libary/LaunchAgents/test.plist.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>test</string>
    <key>ProgramArguments</key>
    <array>
    <string>osascript</string>
    <string>-e</string>
    <string>tell application "System Events"
    set p to name of processes
    if p contains "TextEdit" and p does not contain "Mail"
    do shell script "open -gja Mail"
    end
    end</string>
    </array>
    <key>StartInterval</key>
    <integer>10</integer>
</dict>
</plist>

open -j (--hide) was added in 10.8. open -jg sometimes opens a new visible window if an application is running but has no open windows.

If StartInterval is 9 seconds or less, the program just gets throttled.

com.apple.launchd.peruser.501[128]: (test) Throttling respawn: Will start in 7 seconds
share|improve this answer
    
Can we close either application when the other closes in the same method? –  Mike Kormendy Apr 25 at 8:31

Thanks for the code. I managed to extend it a bit to also close the launched process if the original watched process isn't running anymore. Here's my code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>TextEditWatcher</string>
    <key>ProgramArguments</key>
    <array>
    <string>osascript</string>
    <string>-e</string>
    <string>tell application "System Events"
        set p to name of processes
        if p contains "TextEdit" and p does not contain "Mail" then
            do shell script "open -gja Mail"
        end if
        if p does not contain "TextEdit" and p contains "Mail" then
            tell application "Mail" to quit
        end if
        end tell</string>
    </array>
    <key>StartInterval</key>
    <integer>30</integer>
</dict>
</plist>

On my System (10.9.4), a StartInterval of 10 was already too much and as timing isn't crucial in my case, I chose 30 seconds. But 15 or 20 should be fine too. Installation and testing like mentioned in Lri's answer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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