Note: You are viewing an old version of this page. View the current version.

Differences between version 2 and previous revision of KnowledgeBase/Scripts/ff.sh.

Other diffs: Previous Major Revision, Previous Author

Newer page: version 2 Last edited on Tuesday, 20 October 2009 8:28:21 by CyberLeo Revert
Older page: version 1 Last edited on Saturday, 28 June 2008 2:52:54 by CyberLeo Revert
@@ -1,5 +1,5 @@
-<verbatim
+<code brush="bash"
 #!/bin/bash 
  
 # If passed nothing, about:blank. 
 # If passed a single filename, prepend file://cwd/ to it. 
@@ -111,5 +111,5 @@
 fi 
  
 echo ${url} 
 exec /usr/bin/firefox -a firefox -remote "openURL(${url},new-tab)" 
-</verbatim
+</code

version 2

#!/bin/bash

# If passed nothing, about:blank.
# If passed a single filename, prepend file://cwd/ to it.
# If passed a single pathname, check if it's absolute or relative.
#  If absolute, prepend file://
#  If relative, prepend cwd, realpath(), then prepend file://
# If passed a URL, do nothing to it.

# Check if firefox is running (ping)
# If firefox isn't running, start it naked first, and check to see when it starts pinging. (timeout 60 seconds or so)
# Pass the URL to firefox via -remote openURL()

# If multiple parameters are passed, they had better be escaped properly. Treat each as a separate file/path/url
#  and openURL() each.

# Select action based on command name (ffw = new windows, ffo = all URLs in tabs of new window, ff = new tabs)
me="$(basename "${0}")"
if [ "${me}" = "ffw" ]
then
        action="new-window"
elif [ "${me}" = "ffo" ]
then
        action="new-window"
        nextaction="new-tab"
else
        action="new-tab"
fi

# Parse the arguments to launch things.

startff() {
        url="$1"
        action="$2"

        # Check if firefox is running. If not, start naked
        if ! /usr/bin/firefox -a firefox -remote "ping()"
        then
                ( cd ~; exec /usr/bin/firefox ) &
                timeout=30
                while ! /usr/bin/firefox -a firefox -remote "ping()"
                do
                        sleep 1
                        if [ -z "$(( timeout-- ))" ]
                        then
                                # Not responding. Kill ourselves.
                                echo "Not responding."
                                exit 64
                        fi
                done
        fi

        # Should be running now.
        if ! /usr/bin/firefox -a firefox -remote "openURL(${url},${action})"
        then
                echo "Fail: openURL(${url},${action})"
        fi
}

if [ "${#}" -eq 0 ]
then
        startff "about:blank" ${action}
        exit 0
fi

while [ -n "${1}" ]
do
        spec="${1}"

        # Check if it's a URL (://)
        if [ -n "$(echo "${spec}" | grep "://")" ]
        then
                # url
                url="${spec}"
        else
                # Check if it's a file.
                if [ "${spec:0:1}" = "/" ]
                then
                        # Absolute path
                        url="$(printf "file://%s" "${spec}")"
                else
                        # Relative path
                        url="$(printf "file://%s/%s" "${PWD}" "${spec}")"
                fi
        fi

        startff "${url}" "${action}"

        if [ -n "${nextaction}" ]
        then
                action="${nextaction}"
                unset nextaction
        fi

        shift
done

exit 0

# Legacy code below here.
if [ -z "${*}" ]
then
        url="about:blank"
elif [ -z "$(echo ${*} | grep "://")" ]
then
        # Assemble a 'local' URL
        url="$(printf "file://%s/%s" "$(pwd)" "${*}")"
else
        url="${*}"
fi

echo ${url}
exec /usr/bin/firefox -a firefox -remote "openURL(${url},new-tab)"