osx:当另一个程序打开时启动一个程序

osx:当另一个程序打开时启动一个程序

我想要一种当另一个程序启动时启动一个程序的方法。具体来说,我想要打开 MS Word 文档并同时打开我的引文管理器(EndNote 在 MS Word 首选项中支持此功能,但我最近切换了引文管理器)。

我希望能够打开驱动器上任何现有的 Word 文档并触发第二个程序。同样的疑问最近针对windows 7环境进行了回答。

答案1

尝试将这样的属性列表保存为~/Libary/LaunchAgents/test.plist并用 加载它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) 是在 10.8 中添加的。open -jg如果应用程序正在运行但没有打开的窗口,有时会打开一个新的可见窗口。

如果 StartInterval 为 9 秒或更短,程序就会受到限制。

com.apple.launchd.peruser.501[128]: (test) Throttling respawn: Will start in 7 seconds

答案2

感谢提供代码。我设法对其进行了一点扩展,以便在原始监视进程不再运行时也关闭已启动的进程。这是我的代码:

<?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>

在我的系统 (10.9.4) 上,StartInterval 为 10 已经太多了,而且由于时间对我而言并不重要,所以我选择了 30 秒。但 15 或 20 也应该没问题。安装和测试如 Lri 的回答中所述。

相关内容