为什么我不能使用 Python 脚本中的 Cocoa 类?

发布于 2024-09-19 06:33:12 字数 1437 浏览 8 评论 0原文

今天是我第一次使用 Python,所以我确信这将是一个简单的问题。

我需要从命令行应用程序转换此 Python 脚本:webkit2png。最终结果将是一个 URL,该 URL 返回作为查询字符串参数传递到其中的网页图像。我已经在 Windows 上使用 .NET 和 IE、Gecko 和 WebKit 实现了这一点,但现在需要对 OS X 上的 Safari 执行相同的操作。

我想我已经将其转换,但不幸的是,我在运行来自 OS X 上 Apache 的脚本:

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)

错误在最后一行“initWithContentRect...”上抛出。我看到的错误是:

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

如果我在命令行上运行脚本(删除 CGI 内容后),它会完美运行。

这是我要导入的库:

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⒈起吃苦の倖褔 2024-09-26 06:33:12

您(通常)无法从未与 GUI 用户关联的进程连接到窗口服务器。请参阅此 Apple 技术说明
基本上,从 Apache 生成的进程中使用 NSWindow 等是一个很大的禁忌。如果没有 GUI 用户登录,甚至不能保证窗口服务器存在。因此,您无法可靠地执行您想要执行的操作。

问题是OS X自带的WebKit依赖于window服务器。一种解决办法可能是安装 Qt,它希望有一个独立于 Core Graphics 窗口服务器的 WebKit 后端。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文