因为我张贴问题的PyObjc功能我已经找到另一个工作Objc例子(http://mattgemmell.com/2008/02/24/skinnable-cocoa-ui-with-webkit-and-css +来源:http://mattgemmell.com/files/source/skinnableapp.zip)
这是我现在有:
webview_obj_from_jsAppDelegate .py
from Foundation import *
from AppKit import *
import objc
class webview_obj_from_jsAppDelegate(NSObject):
interface = objc.IBOutlet()
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
def awakeFromNib(self):
self.interface.setFrameLoadDelegate_(self)
path = NSBundle.mainBundle().resourcePath() + '/interface/index.html'
self.interface.setMainFrameURL_(path)
self.interface.windowScriptObject().setValue_forKey_(self, 'AppController')
#this is what I suspect to be the problem
def isSelectorExcludedFromWebScript_(self, aSel):
return NO
def showMessage(self, message):
NSLog(message)
index.html的
<!DOCTYPE html>
<html lang="en">
<head>
<title>title</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="description" content="description">
<meta name="keywords" content="">
</head>
<body>
<input id="message_button" type="button" value="Show Message" onClick="window.AppController.showMessage_('I clicked a button and I liked it');" />
</body>
</html>
现在,如果我要isSelectorExcludedFromWebScript_选择添加这样的:
def isSelectorExcludedFromWebScript_(self, aSel):
if aSel is objc.selector(self.showMessage, signature = '[email protected]:'):
return NO
点击HTML按钮,给了我这个错误:
WebKit discarding exception: <OC_PythonException> <type 'exceptions.ValueError'>: isSelectorExcludedFromWebScript:: returned None, expecting a value
END编辑
我试图从JavaScript调用一个PyObjc函数,但我无法让它工作。我找到了一个用Objc编写的工作示例(https://github.com/ryanb/cocoa-web-app-example),但没有翻译它的运气。
几天前,我已经偶然发现这是更复杂的(http://stackoverflow.com/questions/2288582/embedded-webkit-script-callbacks-how)。不用说,我无法实现它的工作。
所以,如果任何人有一个简单的例子(pyobjc & js),我真的很感激,如果你会告诉我。