File: make-snapshot.sh

package info (click to toggle)
ttfautohint 1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,504 kB
  • ctags: 4,347
  • sloc: ansic: 38,386; sh: 5,749; cpp: 4,131; perl: 340; makefile: 309; sed: 39
file content (87 lines) | stat: -rw-r--r-- 2,283 bytes parent folder | download
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
#! /bin/sh
#
# Copyright (C) 2013-2016 by Werner Lemberg.
#
# This file is part of the ttfautohint library, and may only be used,
# modified, and distributed under the terms given in `COPYING'.  By
# continuing to use, modify, or distribute this file you indicate that you
# have read `COPYING' and understand and accept it fully.
#
# The file `COPYING' mentioned in the previous paragraph is distributed
# with the ttfautohint library.
#
#
# make-snapshot.sh <application> <filename>
#
# Make a snapshot from an application's start window and save it to a file.
# This needs X11 and ImageMagick's `import' tool.
#
# This script is very simple.  Some possible problems:
#
# o In case the program doesn't create a visible X11 window, you have to
#   abort the script with ^C.
#
# o It fails for intelligent applications like `firefox' in case they are
#   already run, since firefox replaces the process with a new window of the
#   already running instance.
#
# o It loops forever for programs like `k3b' that spawns itself, thus
#   having a different process ID for the visible window.


# This script uses ideas from
# http://blog.chewearn.com/2010/01/18/find-window-id-of-a-process-id-in-bash-script/.

if [ $# -ne 2 ]; then
  echo "Usage: $0 application imagename"
  exit 1
fi


find_WID()
{
  # Get all windows with the name $APP (ignoring case).
  xwininfo -root -tree 2>/dev/null \
  | grep -i $1 \
  | while read DATA; do
      # Extract Window ID.
      WID=`echo $DATA | awk '{print $1}'`

      # Check whether the window's PID is matching the application's PID.
      if [ `xprop -id $WID _NET_WM_PID | awk '{print $3}'` -eq $PID ]; then
        # Check whether window is displayed actually.
        if [ "`xwininfo -id $WID | grep 'IsViewable'`" != '' ]; then
          echo $WID
          return
        fi
      fi
    done
}


# Start program in background and get its process ID.
$1 &
PID=$!

sleep 1

# Get application name.
APP=`ps --no-header -o comm -p $PID`
if [ "$APP" == "" ]; then
  echo "Couldn't start application \`$1'"
  exit 1
fi

# Loop until program has displayed a window
# so that we can actually get the Windows ID.
while [ "$WID" == "" ]; do
  WID=`find_WID $APP`
  sleep 1
done

# Make snapshot.
import -silent -window $WID $2

kill $PID

# eof