After run firefox go to full screen but not kiosk mode - bash

I would like to start Firefox automatically on startup, and then maximize the window. With the code below, I am currently able to start Firefox, however I cannot get past that.
#!/bin/bash
firefox &
while [ "$(wmctrl -l | grep \"Mozilla Firefox\" 2>&1 | wc -l)" -eq 0 ];
do
sleep 1;
done
wmctrl -r firefox -b toggle, fullscreen
The first portion starts running Firefox. After that there is a loop, which I'm writing to create and display the Firefox window. The last step is maximize Firefox to full screen.
I believe I have a problem with the while loop.

I suggest:
while [[ $(wmctrl -l | grep "Mozilla Firefox" 2>&1 | wc -l) -eq 0 ]]; do

Related

Is there a way for tux to make status bar show different contents in different clients when they are all attached?

I write a simple shell script to help tmux show all the session in the status bar, however, the tmux's status bar always show the same thing in the different clients when they are all attached.
My script is as follows.
#!/usr/bin/env bash
session=`tmux ls | cut -d " " -f 1 | tr -d ":" | tr "\\n" " "`
# attached_count=`tmux ls | grep -n \(attached\) | cut -d ":" -f 1`
active_session=`tmux display-message -p '#S'`
# session_start="#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]"
session_start="#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]"
# session_end="#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]"
session_end="#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]"
session_active_start="#[fg=cyan,bg=black,nobold,noitalics,nounderscore]#[fg=black,bg=cyan]"
session_active_end="#[fg=black,bg=cyan,nobold,noitalics,nounderscore]"
session_str=""
count=1
for i in $session
do
# if [[ $count -eq $attached_count ]]; then
if [[ $i == $active_session ]]; then
session_str="${session_str}${session_active_start}${i}${session_active_end}"
else
session_str="${session_str}${session_start}${i}${session_end}"
fi
count=$((count+1))
# break
# echo "${i}"
done
session_str=`echo ${session_str} | tr -d "\n"`
printf "${session_str}"
```bash
It will output a long string content all my session name and make the attached one in other colors
"#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]besiii#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]conf#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]default#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]#[fg=brightblack,bg=black,nobold,noitalics,nounderscore]#[fg=white,bg=brightblack]lang#[fg=black,bg=brightblack,nobold,noitalics,nounderscore]#[fg=cyan,bg=black,nobold,noitalics,nounderscore]#[fg=black,bg=cyan]vert#[fg=black,bg=cyan,nobold,noitalics,nounderscore]"
However, all my iterm windows which attach tmux's client will show the same status bar.
Is there a way to make tmux show different contents in different attached clients??
TL;DR: No.
Tmux sessions are designed to show exactly the same thing for all connected users. If a user is connecting on a small terminal, everyone's view will be squashed to only show the small terminal size. There is no way around that (unless you're viewing different windows in the same session), and there is no way to show different content to different users when viewing the same window.

Script won't work in the background

I'm making my first steps in Linux and shell scripting. Wrote a small script that should alert me if my laptop's battery is running low.
It works in the foreground but not in the background for some reason. Gives out:
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control
The script code is the following:
#!/bin/bash
perc=`upower -i $(upower -e | grep BAT) | grep percentage | cut -c26- | cut -c -2`
state=`upower -i $(upower -e | grep BAT) | grep state | cut -c26-`
while true; do
while [[ $perc -gt 20 ]]; do
sleep 300
done
while [[ $state = 'discharging' ]]; do
mplayer /root/scripts/sad.ogg
sleep 120
done
while [[ $perc -le 20 ]]; do
sleep 300
done
done
Will greatly appreciate any advice!
I think your problem is with mplayer the do connect error is saying that it's truing to get a socket with lirc, it should work if you set your
$HOME/.mplayer/config
lirc=no

How can I make my screen locker script work?

I've been having some problems with my screenlocker program. Spent a day trying to solve it but nothing worked, so I decided to write a program that locks my screen:
LOCKTIME=60
lastIdleTime=0
extra=0
while [ 1 ]; do
sound=$(pacmd list-sink-inputs | grep -c "state: RUNNING")
idleTime=$(($(xprintidle) / 1000))
lock=$(gnome-screensaver-command -q | grep -c " active")
if [[ $lock != 0 ]]; then
extra=$idleTime
else
if [[ $sound != 0 || $idleTime -lt $lastIdleTime ]]; then
extra=$idleTime
fi
if [[ $(($idleTime - $extra)) -gt $LOCKTIME ]]; then
gnome-screensaver-command -l
fi
fi
lastIdleTime=$idleTime
sleep 1
done
If I execute it manually, everything goes well. But I want to run it at startup, so I tried to use crontab and create a desktop entry at ~/.config/autostart folder. But it seems that crontab doesn't execute the program, or it executes but the script can't lock my screen, and it runs with desktop entry, but xprintidle doesn't update and gnome-screensaver-command -q | grep -c " active" returns 0 all the time, so after 60 seconds it stays locking my screen every second.
I also wrote it in python, and it doesn't work either. The only diference is that gnome-screensaver-command -q | grep -c " active" returns 1 all the time.
Is there a better way to execute and keep it running (and working) every startup?
Btw, I'm using Antergos with GNOME and GDM.

How to detect that the sound is currently playing in Linux?

I use ALSA. I want to prevent PC to suspend while a sound is played. I use this bash code to run a screen locker and a suspend command:
# Run a screen locker.
#xautolock -time 5 -locker slimlock &
# Run suspend
#xautolock -time 6 -locker 'systemctl suspend' &
I want to detect that sound or video is played and prevent PC to suspend.
For instance a pseudocode:
if (video is not played)
{
run a screen locker
}
if (sound is not played and video is not played)
{
run suspend command
}
How to detect that a sound or a video is playing with a command line utility?
Check if any /proc/asound/card*/pcm*/sub*/status file contains state: RUNNING.
I use this:
pacmd list-sink-inputs
If you use xscreensaver then this command will tell you whether the screen is blanked:
xscreensaver-command -time | grep -q 'screen \(locked\|blanked\)'
Typically video players will stop the screensaver from activating, so if it has activated then it probably means there is no video playing.
You can use it like this:
if xscreensaver-command -time | grep -q 'screen \(locked\|blanked\)'; then
echo Screen is off (screensaver active)
else
echo Screen is on, video might be playing
fi
You might try this script (requires installing xmacroplay):
#!/bin/bash
# Script to prevent screen blanking when audio is playing.
command -v xmacroplay > /dev/null 2>&1 || { echo "ERROR: must install xmacroplay"; exit -1; }
while true; do
sleep 50
if pacmd list-sink-inputs | grep -w state | grep -q RUNNING ; then
xmacroplay :0 >& /dev/null <<EOF
MotionNotify 90 90
MotionNotify 120 120
EOF
fi
done
While the other script works, it keeps moving the mouse to a fixed location. This one tries to move it only if it hasn't changed, and keeps it near the current location.
#!/bin/bash
# Script to prevent screen blanking when audio is playing.
if [ -z DISPLAY ]; then
DISPLAY=:0
fi
while true; do
sleep 50
if pacmd list-sink-inputs | grep -w state | grep -q RUNNING ; then
xdotool mousemove_relative -- -1 -1
sleep 1
xdotool mousemove_relative -- 1 1
fi
done

Open applications in different workspaces in Gnome

Given my laziness, I tried to write a bash script that opens at once some daily apps in different desktops. This script should work in Gnome. I've written that so far:
#!/bin/bash
firefox &
thunderbird &
/usr/bin/netbeans --locale en &
amsn &
gnome-terminal &
sleep 2
wmctrl -r firefox -t 0 && wmctrl -r netbeans -t 1 && wmctrl -r gnome-terminal -t 2 && wmctrl -r amsn -t 6 && wmctrl -r thunderbird -t 7
... but it doesn't work. My apps open, but they won't be assigned to the desktops I specify :(.
I changed the value of sleep to 15., but only firefox & netbeans are assigned correctly; the rest opens in the workspace where I execute the script from.
Thanks to Akira comment, I finally succeeded at making it work (the script runs at startup like a charm) Here is the new code:
#!/bin/bash
wmctrl -n 8
firefox &
thunderbird &
/usr/bin/netbeans --locale en &
amsn &
gnome-terminal &
sleep 15
wmctrl -r firefox -t 0
wmctrl -r netbeans -t 1
wmctrl -r terminal -t 2
wmctrl -r amsn -t 6
wmctrl -r thunderbird -t 7
#focus on terminal
wmctrl -a terminal
checkout DevilsPie, it watches creation of windows and act accordingly.
Devil's Pie can be configured to detect windows as they are created, and match the window to a set of rules. If the window matches the rules, it can perform a series of actions on that window. For example, I can make all windows created by X-Chat appear on all workspaces, and the main Gkrellm1 window does not appear in the pager or task list.
Or you can use a window manager which is able to do the same in-house, eg. fluxbox.
In dconf-editor:
org->gnome->shell->extensions->auto-move-windows
here is what it should look like:
['firefox.desktop:1','pidgin.desktop:2']
This script will check if it's required to change the workspace, switches to it, starts the app, waits until the window is created and switches back to the origin namespace.
Because it uses wmctrl -l to check if a new window was created, it can handle fast as well as slow started applications, without the need to wait for for a static amount of seconds.
I called this script start-on-workspace.
Usage: start-on-workspace <Workspace> <command> [argument...
#!/bin/sh -e
# get the target ns
target=$(($1 - 1))
shift
# get the current ns
current=$(wmctrl -d | grep '*' | cut -d' ' -f1)
if [ $current != target ]; then
# switch to target ns
wmctrl -s $target
fi
# get a checksum of currently running windows
a=$(wmctrl -l | cut -d' ' -f1 | sha1sum | cut -d' ' -f1)
b=$a
# start the app
$# &
# wait until there is a change on the window list
while [ $a = "$b" ]; do
a=$(wmctrl -l | cut -d' ' -f1 | sha1sum | cut -d' ' -f1)
sleep 0.1
done
# switch back to the origin namespace if needed
if [ $current != target ]; then
wmctrl -s $current
fi

Resources