[Linux] Changing theme via command line



  • @luetage Will do 🙂



  • I didn't have a lot of time in the last weeks, but I've been able to get it working! The code still is a bit messy and I did not check for compatibility with Mac OS, even though it should work on Mac OS as well with minor corrections.

    Here's a short how-to:

    1. Prerequisites:

    The entire script is written in bash, which is why it should work on Mac OS, too. There are two files: One Bash script is for changing the wallpaper at fixed time intervals, randomly choosing a wallpaper out of a specified folder. Inside this bash script, a second bash script named "ChangeVivaldiColour.sh" is called, which will adapt the colours of vivaldi. I am using nitrogen to set the desktop background, but in principle any program capable of doing so can be used. It is important to install node.js required for crconsole using nvm as the path is different otherwise. I am using nvm to install newer versions of node.js as the default debian package is too old to run crconsole. In this case, I've used node.js in version 11.10.0, but newer version should work as well (but you will need to change the path to your node installation).

    First bash script:

    #!/bin/bash
    # delay is the time interval between changing backgrounds in seconds
    delay=300
    while :
    do
        # change the path to your wallpaper folder
        for i in $(ls $HOME/Pictures/wallpapers/selection/*.jpg | shuf)
        do
            echo $i
                    # replace the following line with any program capable of setting your wallpaper that you like
    		nitrogen --set-zoom-fill $i
    		# generate colour scheme
    		wal --vte -i $i -n
    		# Use colour scheme in Vivaldi
    		(source $HOME/Documents/Scripts/ChangeVivaldiColour.sh >> log.txt)
            sleep $delay
        done
    done
    

    The second bash script is:

    #!/bin/bash
    # Using Wal colours (replace this path with the path where the colours generated by wal are stored)
    fgcolour="$(head -2 /home/yourusernamehere/.cache/wal/colors | tail -1)"
    bgcolour="$(head -1 /home/yourusernamehere/.cache/wal/colors | tail -1)"
    # custom css is not working right now, I'll try to fix this
    sed -i "21s/.*/    background-color: ${fgcolour} !important;/" /opt/vivaldi/resources/vivaldi/style/custom.css
    sed -i "23s/.*/    --colorAccentBg: ${fgcolour} !important;/" /opt/vivaldi/resources/vivaldi/style/custom.css
    
    # testing if vivaldi is running
    PROCESS1=vivaldi
    PROCESS2=crconsole
    PIDS_Vivaldi=`ps cax | grep $PROCESS1 | grep -o '^[ ]*[0-9]*'`
    PIDS_crconsole=`ps cax | grep $PROCESS2 | grep -o '^[ ]*[0-9]*'`
    if [ -z "$PIDS_Vivaldi" ]; then
      # Vivaldi is not running
      if [ -z "$PIDS_crconsole" ]; then
    	echo "All is fine. Neither Vivaldi nor crconsole are running." 1>&2
      else
        # kill crconsole
        pkill -f "node $HOME/.nvm/versions/node/v11.10.0/bin/crconsole"
      fi
    else
      # Vivaldi is running
      # let's start using crconsole
      # find all open tabs
      tablist=$(
      echo .tabs | crconsole &
      sleep 1
      pkill -f "node $HOME/.nvm/versions/node/v11.10.0/bin/crconsole"
      )
      # read every tab
      while read -r line; do
        echo "... $line ..."
        # find /browser.html tab
        if [[ $line == *"/browser.html"* ]]; then
          tabnumber=$(echo "$line" | grep -o  '[0-9]\+')
          # change Vivaldi accent background colour
          var=$(
          (echo ".switch $tabnumber"; sleep 1; echo "chrome.storage.local.set({'BROWSER_COLOR_ACCENT_BG': '$fgcolour'})") | crconsole &
          sleep 1
          pkill -f "node $HOME/.nvm/versions/node/v11.10.0/bin/crconsole"
          )
          echo "$var" > log_file
        fi
      done <<< "$tablist"
      sleep 1
      pkill -f "node $HOME/.nvm/versions/node/v11.10.0/bin/crconsole"
    fi
    exit
    
    

    Place both files in the same folder, make them executable and run the first bash script at the startup of your system.
    To make Vivaldi change colours automatically, it has to be started with the right flag:

    vivaldi --remote-debugging-port=9222
    

    Right now, if a custom css file is used, the automatic colour change does not work, but I am working on this. If you find any bugs, let me know.



  • @Wismut With just minor changes this works perfectly! My version of the second script sources the entire colors.sh file so I can set more colors and change them as needed.

    # Source Wal colours (replace this path with the path where the colours generated by wal are stored)
    source ~/.cache/wal/colors.sh
    

    ...

        # change Vivaldi accent background colour
        var=$(
        (echo ".switch $tabnumber"; sleep 1; echo "chrome.storage.local.set({'BROWSER_COLOR_BG':'$background', 'BROWSER_COLOR_FG':'$foreground', 'BROWSER_COLOR_ACCENT_BG':'$color5', 'BROWSER_COLOR_HIGHLIGHT_BG':'$color1'})") | crconsole &
        sleep 1
        pkill -f "node /usr/bin/crconsole"
        )
    

    Thank you for figuring this out!


Log in to reply
 

Looks like your connection to Vivaldi Forum was lost, please wait while we try to reconnect.