rhythmbox-start-minimized



#!/usr/bin/env perl
# rhythmbox-start-minimized - Starts Rhythmbox minimized
# License:  Creative Commons Attribution-NonCommercial-ShareAlike 3.0
# Revision: 120315

#---------------------------------------------------------------------
#                           important note
#---------------------------------------------------------------------

# This software is provided on an  AS IS basis with ABSOLUTELY NO WAR-
# RANTY.  The  entire risk as to the  quality and  performance of  the
# software is with you.  Should the software prove defective,  you as-
# sume the cost of all necessary  servicing, repair or correction.  In
# no event will any of the developers,  or any other party, be  liable
# to anyone for damages arising out of use of the software, or inabil-
# ity to use the software.

#---------------------------------------------------------------------
#                              overview
#---------------------------------------------------------------------

                                # Label must be single-quoted here
my $USAGE_TEXT = << 'END_OF_USAGE_TEXT';
Usage: $PROGNAME --run

This program starts "rhythmbox" (if it isn't already running) and then
tries to minimize the program's window.
END_OF_USAGE_TEXT

#---------------------------------------------------------------------
#                            module setup
#---------------------------------------------------------------------

require 5.8.1;
use strict;
use Carp;
use warnings;
                                # Trap warnings
$SIG{__WARN__} = sub { die @_; };

#---------------------------------------------------------------------
#                           basic constants
#---------------------------------------------------------------------

use constant ZERO  => 0;        # Zero
use constant ONE   => 1;        # One
use constant TWO   => 2;        # Two

use constant FALSE => 0;        # Boolean FALSE
use constant TRUE  => 1;        # Boolean TRUE

#---------------------------------------------------------------------
#                         program parameters
#---------------------------------------------------------------------

my $DECIDELAY = 200;            # Number  of  deciseconds  (1/10s of a
                                # second) to  wait for  "rhythmbox" to
                                # start up

my $PURPOSE   = 'Starts Rhythmbox minimized';
my $REVISION  = '120315';
my $USE_LESS  = FALSE;          # Flag: Use "less" for usage text

#---------------------------------------------------------------------
#                          global variables
#---------------------------------------------------------------------

my $PROGNAME;                   # Program name (without path)
   $PROGNAME =  $0;
   $PROGNAME =~ s@.*/@@;

#---------------------------------------------------------------------
#                          support routines
#---------------------------------------------------------------------

# "UsageError" prints  usage text for the current program,  then term-
# inates the program with exit status one.

#---------------------------------------------------------------------

sub UsageError
{
    $USAGE_TEXT =~ s@^\s+@@s;
    $USAGE_TEXT =~ s@\$PROGNAME@$PROGNAME@g;

    $USAGE_TEXT = << "END";     # "END" must be double-quoted here
$PROGNAME $REVISION - $PURPOSE

$USAGE_TEXT
END
    $USAGE_TEXT =~ s@\s*\z@\n@s;

    if ($USE_LESS && (-t STDOUT) && open (OFD, "|/usr/bin/less"))
    {
                                # "END" must be double-quoted here
        $USAGE_TEXT = << "END";
To exit this "help" text, press "q" or "Q".  To scroll up or down, use
PGUP, PGDN, or the arrow keys.

$USAGE_TEXT
END
        print OFD $USAGE_TEXT;
        close OFD;
    }
    else
    {
        print "\n", $USAGE_TEXT, "\n";
    }

    exit ONE;
}

#---------------------------------------------------------------------
#                            main routine
#---------------------------------------------------------------------

sub Main
{
    my $OptRun;                 # Flag: "--run" option

#---------------------------------------------------------------------
# Initial setup.

    select STDERR; $| = ONE;    # Force STDERR flush on write
    select STDOUT; $| = ONE;    # Force STDOUT flush on write

    for my $arg (@ARGV)         # Process command-line arguments
    {
        if ($arg =~ m@^-+run\z@i)
        {
            $OptRun = TRUE;
        }
        else
        {
            die "Invalid argument: $arg\n";
        }
    }
                                # Check for required switch
    &UsageError() unless $OptRun;

#---------------------------------------------------------------------
# Additional setup.

    my $started = FALSE;        # Flag: We started "rhythmbox"
    my $n;                      # Scratch (integer)
    my $str;                    # Scratch (string )
    my $wid;                    # Window ID

    $str =  `/bin/ps ax 2>&1`;  # Get "ps ax" output and adjust it
    $str =  "" unless defined $str;
    $str =~ y/\011\040/ /s;
    $str =~ s@\s*\z@\n@;
                                # Limit this script to one instance
    my @str = split (/rhythmbox-tray-start/, $str);
    $n      = scalar (@str) - ONE;
    exit ZERO if $n > ONE;
                                # Start "rhythmbox" if necessary
    if ($str !~ m@:\d\d (/\S+/|)rhythmbox ?\n@)
    {
        system "rhythmbox >& /dev/null &";
        $started = TRUE;
    }

#---------------------------------------------------------------------
# Find "rhythmbox" main window.

    for (ONE..$DECIDELAY)
    {
                                # Get "wmctrl -l" output and adjust it
        $str =  `wmctrl -l 2>&1`;
        $str =  "" unless defined $str;
        $str =~ y/\011\040/ /s;
        $str =~ s@\s*\z@\n@;
                                # Is the window present?
        if ($str =~ m@(^|\n)(0x\w+) \S+ \S+ Rhythmbox\n@)
        {                       # Yes
            $wid = $2;          # Window ID
            last;               # Exit loop
        }

        last unless $started;   # Exit loop unless we need to wait
                                # Pause briefly
        select (undef, undef, undef, 0.100);
    }

#---------------------------------------------------------------------
# Wrap it up.

    if (defined ($wid))         # Did we find the window?
    {                           # Yes
                                # Pause briefly
        select (undef, undef, undef, 0.100);
                                # Minimize it
        system "xwit -iconify -names Rhythmbox";
    }

    undef;
}

#---------------------------------------------------------------------
#                            main program
#---------------------------------------------------------------------

&Main();                        # Call the main routine
exit ZERO;                      # Normal exit



To continue, press the browser's Back button.