skip to Main Content

The question is related to bash and Linux like Debian; Ubuntu or Linux Mint.

It’s known to open a new firefox window by bash on follow way:

Firefox

It’s known to open a new Firefox tab on follow way:

Firefox https://stackoverflow.com

How to open a new Firefox on a size of x% of display size by bash and without awk?

2

Answers


  1. Open a new Firefox window, with specific size and URL

    How I do this, using xdotool and wmctrl:

    #!/bin/bash
    
    targetUrl="${1:-https://stackoverflow.com/search?tab=votes&q=user%3A1765658+firefox}"
    geometry=1200x900+200+120
    
    mkBound() { local refstr=({0..9} _ {A..Z} = {a..z}) tfmt
        printf -v tfmt %8s
        tfmt=(${tfmt// /%s}{,,}{,}) tfmt=${tfmt[*]} tfmt=${tfmt// /-}
        printf -v bound $tfmt ${refstr[RANDOM%64]}{,,}{,,,}{,,,}
    }
    mkBound
    printf -v ffurl "data:text/html;ascii,<head><title>%s</title></head>" $bound
    
    firefox --new-window "$ffurl"
    
    while ffwin=$(wmctrl -l|sed -ne "/$bound/s/ .*$//gp");[[ -z ${ffwin} ]] ;do
        echo -n .
        sleep .05
    done
    echo
    
    IFS='+x' read -r width height xpos ypos <<<"$geometry"
    xdotool windowraise $ffwin
    xdotool windowfocus $ffwin
    xdotool windowactivate $ffwin
    sleep .8
    xdotool windowmove $ffwin $xpos $ypos
    xdotool windowsize $ffwin $width $height
    xdotool windowactivate $ffwin
    sleep .2
    xdotool key ctrl+l
    xdotool type --delay 22 "$targetUrl"
    xdotool key Return
    

    Save this in a script, then run them with an url as argument.

    You could replace --new-window by --private-window if needed.

    Nota: I use a 48 random chars string as bound to force firefox to title this string in order to ensure wmctrl will find only this window…

    For strictly answering about size of x% of display size:

    You could replace 4th line:

    geometry=1200x900+200+120
    

    by either:

    psize=40
    while IFS=' +x' read cmd a b c d ;do
        [[ $cmd == -geometry ]] &&
            geometry=$((a*psize/100))+$((b*psize/100))+$c+$d
    done  < <(xwininfo -root)
    

    or even…

    psize=40
    ar=('')
    while IFS=' x+' read dev stat a b c d e f; do
        if [[ $stat == connected ]]; then
            typ=sec
            [[ $a == primary ]] &&
                typ=${a::3} a=$b b=$c c=$d d=$e
            printf '%3d: %s (%s)n' ${#ar[@]} $dev $typ
            ar+=($((a*psize/100))+$((b*psize/100))+$c+$d)
        fi
    done < <(xrandr)
    read -p 'Wich display: ' -rsn 1 userKey
    if [[ ${ar[userKey]} ]]; then
        geometry=${ar[userKey]}
        echo $userKey
    else
        echo user abort
        exit 1
    fi
    

    You may find a full version on my site (not on github;) using getopts for

    Usage: soFirefox [-h] [ -g str | [ [ -p int | [-x int] [-y int] ] [-d str] [URL]
        -d str  Display either 'HDMI-1', 'DP-2', 'secondary', 'root' or 'primary'.
        -g str  Geometry string in format 'WxH+X+Y' (must be alone).
        -h      Show this.
        -p int  Percent of display.
        -x int  Percent width of display.
        -y int  Percent height of display.
        URL     Default to 'https://stackoverflow.com/search?tab=votes&q=user%3A1765658+firefox'.
    

    You can run:

    soFirefox -d primary -y 80 -x 60 https://stackoverflow.com/a/75327166/1765658
    
    Login or Signup to reply.
  2. Here is a suggestion for a script without awk:

    #!/bin/bash
    
    if [ $# -ne 1 ] || ! [[ $1 =~ ^[0-9]+$ ]] || [ $1 -lt 1 ]; then
      echo "usage: $(basename $0) <number greater 0>"
      exit 1
    fi
    
    # Get the display size
    resolution=$(xrandr | grep '*' | tr -s ' ' | cut -d ' ' -f2)
    display_width=$(echo "$resolution" | cut -d 'x' -f1)
    display_height=$(echo "$resolution" | cut -d 'x' -f2)
    
    
    # Calculate the window size depending on the given command line argument
    window_width=$(echo "($display_width * $1)/100" | bc)
    window_height=$(echo "($display_height * $1)/100" | bc)
    
    # Open a new instance of Firefox with the calculated size
    firefox --width $window_width --height $window_height &
    
    # Open a new tab in the existing instance of Firefox
    # see also: https://askubuntu.com/a/1123804
    firefox --new-tab --url about:newtab &
    

    Note, in my testing I found that if a Firefox window is already open, the size of the already open window is used. The --width and --height parameters are then ignored.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search