Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'd like to use screencapture -l to do automated screen captures of Google Chrome. This requires knowing the windowid of the Google Chrome process that's running.

For apps that support Applescript, this question has the answer. But that solution doesn't work for apps that aren't scriptable, like Google Chrome.

Is there any way to retrieve the windowid from a running Google Chrome process from a Python script? I know that the Quartz Debug app can do it, but that's a GUI app, so it can't be invoked via script.

share|improve this question
    
Does ist explicitly have to be the Chrome window, or do you actually just need the webpage you're visiting to an image? (Given Webkit rendering.) –  MacLemon Dec 6 '12 at 11:20
    
@MacLemon It needs to be Chrome-specific, because there's an issue with Safari on that particular site (Safari doesn't allow OpenID auth inside of iframes). I did find a way to just capture the contents of the browser through selenium, see stackoverflow.com/questions/13732281. I'd still like to know the answer to this specific question, though. –  Lorin Hochstein Dec 6 '12 at 15:51
add comment

2 Answers 2

up vote 2 down vote accepted

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()
share|improve this answer
add comment

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.

share|improve this answer
    
That is one nifty-looking app. Looks like you can even get the windowid from the file paths, which answers the above question directly, even though you don't need the windowid if you're using GrabFS. –  Lorin Hochstein Dec 6 '12 at 16:18
    
About the only downside is that you likely will want to convert the .tiff screenshots to something more compact like .png which is easy with the build in sips. sips -s format png Path/To/Image.tiff --out Path/To/ConvertedImage.png which actually results in a single copy/convert step. –  MacLemon Dec 6 '12 at 16:27
    
I have to say, that scroll bar is incredibly unsightly... –  Jason Salaz Dec 8 '12 at 2:08
1  
As the original poster added, he explicitly needs a screenshot of the Chrome browser window, and that one will include a scrollbar at times. If you want to do screenshots of full webpages without any browser chrome (toolbars, location bar, scrollbars, etc.) you can use Webkit2PNG. github.com/paulhammond/webkit2png –  MacLemon Dec 8 '12 at 12:17
    
It was just a comment :). But I do appreciate the Webkit2PNG link, as I'm about to put it to use, right now! –  Jason Salaz Dec 12 '12 at 6:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.