Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Bash Else/If statement based on Window Renaming

Writer Sophia Terry

I'm trying to create an else/if statement in bash so that when a window renames itself to something. Example: The spotify renames to Sponsored Message or Advertisement) I can run an if to mute amixer, and else if it doesn't rename to either. My original plan was to base the automuter off notifications but since it isn't very accurate, I wanted to ask if anyone knew of reading the window and running a command.

Thanks :)

2

1 Answer

I think the best command to do this would be xdotool however I am not familiar with the Spotify app so you'll have to test if the xwindowname is updated by the app. Below is a list of commands to test this from there a simple script would be easy enough to write but first we need proof of concept.

pgreg <name of spotify app>
xdotool search --pid <pid returned by pgrep>
xdotool getwindowname <window id returned by xdotool search>

The last command will tell you the window name and then you can test if it is changed when the ads come on.

If everything works and the window name is changed when the ads play a simple script like.

NOTE: This is very rough to show the concepts not give a copy and paste answer.

#!/bin/bash
while true;
do
pid=$(pgrep spotify)
win=$(xdotool search --pid $pid)
state=$(xdotool getwindowname $win | <filter for ads>)
if [[ $state = 'ads' ]]; then mute
else unmute
fi
done

Something like that very rough but get across the concept and idea.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy