karim
karim

Reputation: 15589

osx count number of tabs open in firefox

In OSX, when you close Firefox (or any other app) by pressing the top leftmost red button of the window bar, the app is closed but not quitted. It sits in the dock with a dot sign.

I need a script to identify similar situation for Firefox. One way can be to count the number of open tabs in firefox. Right? If it is Zero, then no opened tab.

The script can be bash (preferable) or applescript (runs from bash with) osascript -e 'commands'.

Upvotes: 1

Views: 1087

Answers (2)

karim
karim

Reputation: 15589

I post the final script, if anybody else is interested ... will be helpful. I am using the script as preinstall script in my package. Interestingly the GUI Confirmation works very nicely.

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

As mentioned by @ShooTerKo that osascript activate the application to compile the script, and first query "System Events" to sense if it is running, the alternative to that I found in Apple reference that is termed as coercions. This will act as above script without the help of System Events i.e. will not run Firefox if it is not running. When I tried the code in script editor it works. But when ran this code in a bash script as above, then it start Firefox and then always 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

and then,

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

Upvotes: 0

ShooTerKo
ShooTerKo

Reputation: 2282

maybe this points you to right direction:

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

I tried window's attributes (visible is true or miniaturized is true), but both did not work when Firefox was hidden by the user. There is no attribute tab available, but I think, that there will never be a tab without a window.

UPDATE osascript seems to activate all targeted application to compile the given applescript. To prevent it we must use a little hack to let osascript not know which app we finally target...

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

Have fun, Michael / Hamburg

Upvotes: 3

Related Questions