2

我正在为我的应用程序创建一个命令行工具。它使用pkgbuildproductbuild创建包。它是一个launchdaemon二进制文件。我有preinstallpostinstall脚本。

安装后,可能在postinstall脚本中,我需要检测 Firefox 是否正在运行,然后提示用户他需要重新启动 Firefox。有可能这样做吗?如何?

脚本的一些摘录,它是安装后脚本的一部分。

.........

## Check if Firefox is running in the background with no tab/window open.


function restart_empty_firefox (){
    echo "Restarting firefox if no tab is opened. "

    firefoxRunning=$(osascript \
        -e 'tell application "System Events" to set fireFoxIsRunning to ((count of (name of every process where name is "Firefox")) > 0)' \
        -e 'if fireFoxIsRunning then' \
        -e 'set targetApp to "Firefox"' \
        -e 'tell application targetApp to count (every window whose (closeable is true))' \
        -e 'else' \
        -e 'return 0' \
        -e 'end if')

    if [ $firefoxRunning -eq 0 ]; then
        echo 'Firefox is in the background with no window and so quitting ...'
        osascript -e 'quit app "Firefox"'
    else 
        ##### show a dialog to the user to restart Firefox
    fi

}

firefox_update # not shown here
restart_empty_firefox

.............
4

1 回答 1

2

您可以使用进程状态 ( ps) 工具:

ps aux | grep '[F]irefox.app' | awk '/firefox$/ {print $2}'

结果

82480

这告诉我们Firefox.app确实在运行(进程 82480)。

编辑:由于您似乎倾向于使用osascript这里的示例,该示例要求用户重新启动 Firefox(完全控制在他们手中):

#!/bin/bash

osascript <<'END'
set theApp to "Firefox"
set theIcon to "Applications:Firefox.app:Contents:Resources:firefox.icns"

tell application "System Events"
    if exists process theApp then
        display dialog "Warning: Mozilla " & theApp & " should be closed." buttons {"Continue"} with icon file theIcon default button 1
        if the button returned of the result is "Continue" then
        end if
    end if
    display notification "Installation complete" with title "Application Package" subtitle "Please relaunch " & theApp
    delay 3
end tell
END

如果您的脚本有足够的权限,那么quit建议使用而不是公然终止进程。最终应该这样做,否则只需要求用户退出并自行重新启动 Firefox - 问题已解决。

于 2015-09-08T19:46:37.453 回答