How do I find the windowid of Google Chrome to pass to screencapture -l [duplicate]

Solution 1:

I solved this by writing a python script. When you run it you can specify an application to dump the list of windows IDs for, or if you don't specify any application it'll dump all the windows.

The windows are dumped presentation order, so whatever is at the top of the list is the 'highest' window (ostensibly the currently active one if it's visible)

Example Output

bloop:~ r$ ./wlist2 Chrom
9040 0 "Chromium" "terminal - How do I find the windowid of Google Chrome to pass to screencapture -l - Ask Different"
9039 0 "Chromium" ""

How to use this to capture a screen automatically from a shell script

screencapture -l $(./wlist2 Chrom | head -1 | awk '{print $1}') out.png

Script:

#!/usr/bin/python2.6

from itertools import chain
from Quartz import CGWindowListCreate as create_list
from Quartz import CGMainDisplayID as display_id
from Quartz import CGWindowListCreateDescriptionFromArray as lookup
import sys

class Windows(list):
    def find(self, name):
        for window in self:
            if window.IsOnscreen and window.OwnerName.find(name) > -1:
                print window

    def dump(self):
        for window in self:
            if window.IsOnscreen:
                print window


    def __getitem__(self, item):
        result = list.__getitem__(self, item)
        try:
            return Windows(result)
        except TypeError:
            return result

class Window(object):

    key_list = []

    def __init__(self, kwargs):
        for k in kwargs.keys():
            setattr(self, k.replace('kCGWindow',''), kwargs[k])
            self.key_list.append(k.replace('kCGWindow',''))

    def __repr__(self):
        t = '%d %d "%s" "%s"' % (
            getattr(self, 'Number', -1),
            getattr(self, 'Layer', -1),
            getattr(self, 'OwnerName', None),
            getattr(self, 'Name', None)
        )
        return t.encode('utf-8')

wlist=Windows()
for x in lookup(create_list(display_id(), 0)):
    wlist.insert(0, Window(dict(x)) )

if len(sys.argv) > 1:
    wlist.find(sys.argv[1])
else:
    wlist.dump()

Solution 2:

You can do that with GrabFS, a screenshot filesystem for OS X. (Download at the bottom of the page. Works fine for me on 10.8.2 with OSXFUSE) It gives you the textures of any processes windows as files which you can simply copy. To update the screenshot, copy again.

Looks like this (converted from TIFF to PNG to save space.): Google Chrome Screenshot of this page taken with GrabFS.]![Screenshot of Google Chrome displaying this thread taken with GrabFS. You'll likely need to do a little shell scripting to automate this, but that will be easy.