EDIT
Since I posted the question I have found another working Objc example(http://mattgemmell.com/2008/02/24/skinnable-cocoa-ui-with-webkit-and-css + source: http://mattgemmell.com/files/source/skinnableapp.zip)
This is what I have now:
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>
Now, if I add a selector to isSelectorExcludedFromWebScript_ like this:
def isSelectorExcludedFromWebScript_(self, aSel):
if aSel is objc.selector(self.showMessage, signature = 'v@:'):
return NO
clicking the HTML button gives me this error:
WebKit discarding exception: <OC_PythonException> <type 'exceptions.ValueError'>: isSelectorExcludedFromWebScript:: returned None, expecting a value
END EDIT
I am trying to call a PyObjc function from javascript, but I can't get it to work. I found a working example(https://github.com/ryanb/cocoa-web-app-example) written in Objc, but no luck translating it.
A couple of days ago I've stumbled upon this(http://stackoverflow.com/questions/2288582/embedded-webkit-script-callbacks-how) which is more complicated. Needless to say, I couldn't get it to work.
So if anybody has a simple example(pyobjc & js) I'd really appreciate it if you'd show me.
Can I make a suggestion? Javascript and Python both do a pretty bang up job of speaking HTTP. Perhaps setup a Python HTTP server on the machine, and when Javascript gets the trigger, it could just do an XHttpRequest to the local server?
Alternatively.. It is possible to get Javascript and Python chatting (very roughly) if you use QtWebKit with PySide / PyQt. PyObjC is very poorly supported these days and in many cases only manages to 'barely work' while in most cases not. No offense to the PyObjC folks, but lets be fair - the project just isn't kept up to date as frequently as it used to be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With