views:

60

answers:

1

Today is the first time I've used Python, so I'm sure this'll be an easy question.

I need to convert this Python script from a command line application: webkit2png. The end result will be a URL that returns an image of the webpage passed into it as a querystring param. I've achieved this on Windows with .NET and IE, Gecko and WebKit, but now need to do the same for Safari on OS X.

I think I've got it converted, but unfortunately I'm running into a problem running the script from Apache on OS X:

app = AppKit.NSApplication.sharedApplication()

# create an app delegate
delegate = AppDelegate.alloc().init()
AppKit.NSApp().setDelegate_(delegate)

# create a window
rect = Foundation.NSMakeRect(0,0,100,100)
win = AppKit.NSWindow.alloc()
win.initWithContentRect_styleMask_backing_defer_ (rect, 
        AppKit.NSBorderlessWindowMask, 2, 0)

The error is thrown on the final line "initWithContentRect...". The error I see is:

<class 'objc.error'>: NSInternalInconsistencyException - Error (1002) creating CGSWindow 
  args = ('NSInternalInconsistencyException - Error (1002) creating CGSWindow',) 
  message = 'NSInternalInconsistencyException - Error (1002) creating CGSWindow' 
  name = u'NSInternalInconsistencyException'

If I run the script on the command line (after removing the CGI stuff), it runs perfectly.

Here's the libraries I'm importing:

import cgi
import cgitb; cgitb.enable()  # for troubleshooting              
import sys
try:
  import Foundation
  import WebKit
  import AppKit
  import objc
except ImportError:
  print "Cannot find pyobjc library files.  Are you sure it is installed?"
  sys.exit()
+2  A: 

You cannot (usually) connect to the window server from a process not associated to a GUI user. See this Apple tech note. Basically, it's a big no-no to use NSWindow etc. from the process spawned by Apache. The window server is not even guaranteed to exist if there's no GUI user logged in. So, you can't reliably do what you're trying to do.

The problem is that the WebKit which comes with OS X depends on the window server. One way out might be to install Qt, which hopefully has a backend of WebKit independent of the Core Graphics window server.

Yuji
I see. I was planning to run this on an XServe without someone logged in, so you're right, it doesn't sound like a good fit. I'll take a look at Qt's WebKit wrapper. Thanks!
Matthew Brindley