osx 计算在 firefox 中打开的标签页数

osx count number of tabs open in firefox

在 OSX 中,当您通过按 window 栏最左上角的红色按钮关闭 Firefox(或任何其他应用程序)时,应用程序会关闭但不会退出。它坐落在码头上,带有一个圆点标志。

我需要一个脚本来识别 Firefox 的类似情况。 一种方法是计算 firefox 中打开的选项卡的数量。正确的?如果为零,则没有打开的选项卡。

脚本可以是 bash(首选)或 applescript(从 bash 运行)osascript -e 'commands'

也许这为您指明了正确的方向:

osascript -e 'tell application "Firefox" to count (every window whose (closeable is true))'

我尝试了 window 的属性 (visible is true or miniaturized is true),但是当 Firefox 被用户隐藏时,两者都不起作用。 没有属性 tab 可用,但我认为,没有 window.

将永远不会有标签

更新 osascript 似乎激活所有目标应用程序来编译给定的 applescript。为了防止它,我们必须使用一点技巧让 osascript 不知道我们最终针对哪个应用程序...

osascript <<FOO 
> tell application "System Events" to set fireFoxIsRunning to ((count of (name of every process where name is "Firefox")) > 0) 
> if fireFoxIsRunning then 
> set targetApp to "Firefox" 
> tell application targetApp to count (every window whose (closeable is true)) 
> else 
> return 0 
> end if 
> FOO

玩得开心,迈克尔/汉堡

我 post 最后的脚本,如果其他人有兴趣...将会有所帮助。 我在我的包中使用脚本作为预安装脚本。有趣的是,GUI Confirmation 工作得很好。

## Ask the user to quit firefox, in case it is running. 

## fireFoxIsRunning status, 
# -1 = Firefox is not running
# 0 = Running without any open (0) window
# 1 = window count return error => running with 1 or more windows. (sometime I got error: "every window whose closeable = true doesn’t understand the “count” message".

function firefox_status() {
    firefoxRunning=$(osascript \
            -e 'try ' \
            -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 return number of (windows whose closeable is true)' \
            -e 'else' \
            -e 'return -1' \
            -e 'end if' \
            -e 'on error errorMsg number errorNumber' \
            -e 'return 1' \
            -e 'end try')

echo firefoxRunning = $firefoxRunning

    if [ $firefoxRunning -ne -1 ]; then
        if [ $firefoxRunning -eq 0 ]; then
            echo 'Firefox is in the background with no window and so quitting ...'
            osascript -e 'quit app "Firefox"'
        else 
            echo "Firefox is running with $firefoxRunning windows"
            quiteValue=$(osascript \
            -e 'set valueReturned to display dialog "Firefox is running. Please quit Firefox to continue installation of the Your Program." with title "Your Program" buttons {"Cancel Installation", "Quit Firefox"} default button 2' \
            -e 'return button returned of valueReturned')
        fi
    else 
        echo "Firefox is not running"
    fi
}

firefox_status

# quiteValue will be set only when dialog is shown (firefox is running).
# So for empty value return exit code 0
if [ -z "$quiteValue" ]; then
    exit 0 
fi

if [ "$quiteValue" == "Quit Firefox" ]; then
    osascript -e 'quit app "Firefox"'
elif [ "$quiteValue" == "Cancel Installation" ]; then
    exit 1
fi

如@ShooTerKo 所述,osascript 激活应用程序编译脚本,并首先查询 "System Events" 以感知它是否是 运行ning,我找到的替代方案在 Apple reference 中,这被称为强制转换。这将在没有系统事件帮助的情况下像上面的脚本一样运行,即如果不是 运行ning,则不会 运行 Firefox。 当我在脚本编辑器中尝试代码时,它起作用了。但是当 运行 这段代码在上面的 bash 脚本中时,它会启动 Firefox 然后总是 return 1 (open window).

tell application "Firefox"
    if it is running then
        set num to count (every window whose (closeable is true))
        return num
    else
        return -1
    end if
end tell

然后,

if [ $firefoxRunning -ne -1 ]; then
    if [ $firefoxRunning -eq 0 ]; then
        echo 'Firefox is in the background with no window and so quitting ...'
        osascript -e 'quit app "Firefox"'
    fi
fi