LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2019, 11:26 AM   #1
jakfish
Member
 
Registered: Aug 2016
Posts: 52

Rep: Reputation: Disabled
Need Help Automating a Script


Hello, All,

Running Ubuntu Mate 18.3 on a tiny GPD Pocket, trying to save screen real estate, I'm trying to remove the long text from Chromium's window title bar.

I found an excellent script here (best script is the last one of the post):

https://unix.stackexchange.com/quest...-custom-window

This is the script:
Code:
#!/bin/bash
# stop all instance of this script if "killall" provided as first argument
if [ "$1" == "killall" ]; then
  scriptname=$(basename "$0")
  pattern="[0-9]* /bin/bash .*$scriptname$"
  pids=$(ps ax -o pid,cmd | grep -P "$pattern" | sed 's/^ *//;s/ *$//' | grep -Pv ' grep|killall$' | cut -d" " -f1)
  if [ "$pids" != "" ]; then
    kill -TERM $pids
    echo "$(echo '$pids' | wc -l) instances stopped"
  else
    echo "None found to stop"
  fi
  exit 0
fi

# ask for window
echo -en "\nClick the window you want to set its title "
id=$(printf %i $(xwininfo | grep 'Window id' | cut -d" " -f4))

# fail if no window id
if [ "$id" == "" ]; then
  echo 'Error: Window id not found'
  exit 1
else
  echo "- Got it"
fi

# ask for title
read -e -p "Enter target title: " title

# fail if no title
if [ "$title" == "" ]; then
  echo "Error: No title to set"
  exit 1
fi

# define loop as a function, so we can run it in background
windowByIdSetStickyTitle() {
  local id title curr_title
  id="$1"
  title="$2"

  while true; do
    # get current title
    curr_title="$(xdotool getwindowname $id 2>/dev/null)"

    # exit if we can't find window anymore
    if [ $? -ne 0 ]; then
      echo "Window id does not exist anymore"
      break
    fi

    # update title if changed
    if [ "$curr_title" != "$title" ]; then
      xdotool set_window --name "$title" $id
    fi

    # needed else you will eat up a significant amount of cpu
    sleep 3
  done
}

# infinite loop
windowByIdSetStickyTitle $id "$title" &


# done
echo "Sticky title set"
exit 0
It works wonderfully, but rather than always filling in the two script prompts ("Click the window you want to set its title" and "Enter target title:"), I''d like to automatically insert Chromium's initial window title ("Google - Chromium" no quotes) and rename it "." (period, no quotes) every time the script is run.

Could I avail on veteran scripters to insert this automation in this fine script?

Many thanks,
Jake

Last edited by astrogeek; 02-21-2019 at 01:16 PM. Reason: Added CODE tags
 
Old 02-21-2019, 12:08 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
$1 is your script's first argument, for when you use arguments.

Replace the line reading input and instead assign the argument to be $title
 
1 members found this post helpful.
Old 02-21-2019, 12:48 PM   #3
jakfish
Member
 
Registered: Aug 2016
Posts: 52

Original Poster
Rep: Reputation: Disabled
I very much appreciate your fast response.

Since I'm quite new at this, could I ask you to be a little more specific?

I see several $1's, perhaps not all pertaining to exact window, and of course some $2's need replacing as well.

To enable $1=Google - Chromium and $2=. --how would I go about that?

Thanks again for posting,
Jake
 
Old 02-21-2019, 12:51 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Can you go and edit your original post to add [code] [/code] tags around the script you found? It will be much easier to read that way since it will preserve the spacing.
 
1 members found this post helpful.
Old 02-21-2019, 01:03 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
What happens if you
Code:
xdotool set_window --name "Google - Chromium" .
That looks to be the line that does the work...yes? Just hard-code the values in that line.

I that works, that would be your entire script.
 
1 members found this post helpful.
Old 02-21-2019, 01:04 PM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
1 members found this post helpful.
Old 02-21-2019, 01:16 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by jakfish View Post
I very much appreciate your fast response.

Since I'm quite new at this, could I ask you to be a little more specific?

I see several $1's, perhaps not all pertaining to exact window, and of course some $2's need replacing as well.

To enable $1=Google - Chromium and $2=. --how would I go about that?

Thanks again for posting,
Jake
I suppose I can admit a little guilt with not reading every line of the script.

OK. First.

Seems to me that you grabbed a script and you're running it. But meanwhile you have no experience with writing scripts. Is this a true of false assumption?

Yes there are plenty of people here who can write full scripts, debug anything in that script, or edit it for you. Count me as not the person to do exactly that. But I will explain my inputs better.

What I can do is explain better how to edit and what I was thinking. Otherwise I feel it would be beneficial for you to look up some references on bash and learn a bit about it, especially if you plan to tinker with it.

$1, $2, $3 are special variables for bash that represent the passing arguments of the script, if any. They don't always exist. $# is a special variable too. The $# tells you 'how many arguments someone called a script with'.

You can do things like:
Code:
echo "Number of arguments: " $#
echo "Argument 1: " $1
echo "Argument 2: " $2
As debug near the top of your script.

I do see the $1's being used and they are for a killall command. Well, do you, or don't you need that part at all? I haven't checked it much to determine it.

Later in the script you see the line:
Code:
read -e -p "Enter target title: " title
That line is reading input from the user to take in the string for the window name. And it assigns whatever you type in to the variable named 'title'. You reference that variable with the notation $title. You also can assign it to a passing argument variable with something like this:
Code:
title=$1
or:
Code:
title=$2
These are choices you can make when modifying that code. You can even then print it back out to ensure you've gotten it correctly:
Code:
title=$2
echo "The title is: " $title
 
2 members found this post helpful.
Old 02-21-2019, 01:19 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

I have done that for you this time, welcome to the Programming forum!
 
1 members found this post helpful.
Old 02-21-2019, 01:30 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Another part that I haven't covered too well is identifying the window.

In that script the mouse click is used to get the ID of the window and it saves it to the variable $id.

This is used in the while loop to identify the window using that id for the command identified by scasey.

Seems you always wish to rename the window with "." to shorten it to nearly nothing.

You can modify that line to change the name without needing to read in any name.

Meanwhile I'm hard pressed to determine how you avoid the click. That is how the script chooses what window you wish to rename.
 
2 members found this post helpful.
Old 02-21-2019, 01:47 PM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by rtmistler View Post
Another part that I haven't covered too well is identifying the window.

In that script the mouse click is used to get the ID of the window and it saves it to the variable $id.

This is used in the while loop to identify the window using that id for the command identified by scasey.

Seems you always wish to rename the window with "." to shorten it to nearly nothing.

You can modify that line to change the name without needing to read in any name.

Meanwhile I'm hard pressed to determine how you avoid the click. That is how the script chooses what window you wish to rename.
My take is that the OP only wants to rename the window that the click would identify with
"Google - Chromium"...if that's the case, they (should) only need to replace the $title in
Code:
xdotool set_window --name "$title" $id
with that string, and the $id with the . (dot)

But, in perusing the man page for xdotool, it appears that set_window --name wants the window ID, not the name being displayed, so a modification to my suggestion:
Code:
# ask for window
echo -en "\nClick the window you want to set its title "
id=$(printf %i $(xwininfo | grep 'Window id' | cut -d" " -f4))
# change the title
xdotool set_window --name "$id" .
Yes, I don't either see a way to avoid the click.

It's not clear to me why the script needs to loop...why it just can't be those two lines.
 
2 members found this post helpful.
Old 02-21-2019, 01:52 PM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
you can use pkill <process name> instead of those lines.
 
Old 02-21-2019, 02:05 PM   #12
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by pan64 View Post
you can use pkill <process name> instead of those lines.
??? the script is renaming an existing window's title.
It's not looking for process ID, but "Window ID" from the xwininfo command...apparently.
 
1 members found this post helpful.
Old 02-21-2019, 02:13 PM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I think scasey has it fine in their last rendition.

And I think the killall stuff is just part of some junk that had other purposes beyond what they grabbed this script for. Or grabbed this chunk of script for. Poorly written script, has a forever loop and this is potentially why it has a killall.

The danger would be what my suspicion is, is that the OP just grabbed something and "it works!", but they have no idea about anything else in the script.
 
3 members found this post helpful.
Old 02-21-2019, 02:25 PM   #14
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by rtmistler View Post
I think scasey has it fine in their last rendition.

And I think the killall stuff is just part of some junk that had other purposes beyond what they grabbed this script for. Or grabbed this chunk of script for. Poorly written script, has a forever loop and this is potentially why it has a killall.

The danger would be what my suspicion is, is that the OP just grabbed something and "it works!", but they have no idea about anything else in the script.
Thank you.

I agree, the script is very strange. I also agree that the OP doesn't understand what they have and would add to your implicit caution about running scripts when on doesn't know what, exactly they do.

I'd add the same caution about explicitly following advice posted here, including mine. No one is perfect.

You've certainly done a good job of explaining to the OP what the various piece parts of the script mean. Kudos for that.
 
2 members found this post helpful.
Old 02-21-2019, 02:25 PM   #15
jakfish
Member
 
Registered: Aug 2016
Posts: 52

Original Poster
Rep: Reputation: Disabled
Wow, I can not thank you all enough. I stepped away from the computer for an hour and I returned deluged with help, all the way to a too-kind moderator who corrected the syntax of my original post. I'm going to put together all the advice now and will post back with results, but I first wanted to express my gratitude.

This is a great forum,
Jake
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
New(ish) to command line, need help with automating a program Cordacc Programming 1 10-08-2014 08:27 AM
Automating Input to a program with a simple script--Help I'm stuck Euler2 Linux - Newbie 5 02-25-2010 10:53 AM
need help automating ssh-keygen linxq4u Linux - General 1 08-25-2007 11:13 AM
FTP Script (Automating) noodle123 Linux - General 2 10-16-2002 08:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration