aboutsummaryrefslogtreecommitdiff
path: root/xtitle-alternative
blob: dc0e50828ec2cf143d5f4a75dca312e96aa7cfa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# Downloaded from http://www.shelldorado.com/scripts/cmds/xtitle
##########################################################################
# Shellscript:	xtitle - set title on xterm window
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Requires   :	xprop
# Category   :	Desktop
# Date       :	1995-03-17
# SCCS-Id.   :	@(#) xtitle	1.8 04/02/18
##########################################################################
# Notes
#    o	Thanks to Des Herriott (des@corp.netcom.net.uk) for
#	the way to get a title using "xprop"
#
# Example
#    o	Get the old window title, set the new title to "New Title"
#	    OldTitle=`xtitle -g w -w "New Title"`
#
# Changes
# 1995-06-01 hs	Separate settings for title and icon (0.2)
# 1996-03-04 hs	Determine how to echo without newline (0.3)
# 1996-10-01 hs	Get a window title, syntax changed (0.4)
# 1999-10-27 hs	Use "getopts" to preserve whitespace in arguments (1.4)
# 2002-04-09 hs	Added "Requires" header field (1.6)
##########################################################################

PN=`basename "$0"`			# program name
VER='1.8'

# Character for escape sequence to set title
BOTH=0 ICON=1 WINDOW=2			# Escape sequences - do not change
ICON_PROP=WM_ICON_NAME			# Must match output of "xprop"
WIN_PROP=WM_NAME

Usage () {
    echo >&2 "$PN - set title of xterm window, $VER (hs '95)
usage: $PN [ -g type] [-w title] [-i title] [-t title]
   or  $PN title

switches:
    -t	set both titles (default)
    -w	set window title
    -i	set icon title
    -g  get title, type: w=window, i=icon, b=both

If no arguments are given, $PN prints the window title. In the
second form, both (icon and window) titles are set to the given
title."
    exit 1
}

# Determine how to echo without a newline
[ -z "$ECHO" ] &&
    if [ X`echo -n` = "X-n" ]
    then ECHO="echo"; NL="\c"
    else ECHO="echo -n"; NL=
    fi

Echo ()	{
    $ECHO "$@$NL"
}
Fatal () {
    for Line
    do echo >&2 "$PN: $Line"
    done
    exit 1
}

# GetProperty { $ICON_PROP | $WIN_PROP }
#
GetProperty () {
    if [ -n "$WINDOWID" ]
    then
	# "prop" Example output:
	#   WM_NAME(STRING) = "Title"
	# or (empty title):
	#   WM_NAME(STRING) =
	for Property
	do
	    xprop -id $WINDOWID 2>/dev/null |
		    grep "^$Property" |
		    sed 's/.*=[ 	"]*\([^"]*\)["]*$/\1/'
	done
    else
	echo >&2 "$PN: cannot get window property
	(getting the window title works with a local X server only)"
    fi
}

# SetTitle { $BOTH | $ICON | $WINDOW } "Text"
SetTitle () {
    Echo "]${1};$2"
}

[ -z "$DISPLAY" ] && 
    Fatal "cannot determine X Windows server (DISPLAY not set)"

if [ $# -lt 1 ]
then					# Default action: print window title
    GetProperty "$WIN_PROP"
    exit 0
fi

while getopts g:w:i:b:t:h opt
do
    case "$opt" in
	g)				# Get title
	    case "$OPTARG" in
		w)   GetProperty "$WIN_PROP";;
		i)   GetProperty "$ICON_PROP";;
		b|t) GetProperty "$WIN_PROP" "$ICON_PROP";;
		*)   Fatal "title type must be one of {w|i|b}";;
	    esac
	    exit 0;;
	w)				# Set window title
	    SetTitle "$WINDOW" "$OPTARG";;
	i)				# Set icon title
	    SetTitle "$ICON" "$OPTARG";;
	b|t)				# Set both titles
	    SetTitle "$BOTH" "$OPTARG";;
	h)	Usage;;
	*)	Usage;;
    esac
done
shift `expr $OPTIND - 1`

# Default action: set both titles
[ $# -gt 0 ] && SetTitle "$BOTH" "$*"
exit 0