Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Javascript调用PyObjc函数_Javascript_Objective C_Webview_Pyobjc - Fatal编程技术网

从Javascript调用PyObjc函数

从Javascript调用PyObjc函数,javascript,objective-c,webview,pyobjc,Javascript,Objective C,Webview,Pyobjc,编辑 自从我发布这个问题以来,我发现了另一个正在工作的Objc示例(+来源:) 这就是我现在拥有的: webview\u obj\u from\u jsAppDelegate.py from Foundation import * from AppKit import * import objc class webview_obj_from_jsAppDelegate(NSObject): interface = objc.IBOutlet() def applicationDidFi

编辑

自从我发布这个问题以来,我发现了另一个正在工作的Objc示例(+来源:)

这就是我现在拥有的:

webview\u obj\u from\u 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>
单击HTML按钮会出现以下错误:

WebKit discarding exception: <OC_PythonException> <type 'exceptions.ValueError'>: isSelectorExcludedFromWebScript:: returned None, expecting a value
WebKit丢弃异常::isSelectorExcludedFromWebScript::返回None,应为值
结束编辑

我试图从javascript调用PyObjc函数,但无法使其工作。我找到了一个有效的例子(https://github.com/ryanb/cocoa-web-app-example)是用Objc写的,但没法翻译

几天前我偶然发现了这个(http://stackoverflow.com/questions/2288582/embedded-webkit-script-callbacks-how)哪个更复杂。不用说,我无法让它工作


因此,如果有人有一个简单的例子(pyobjc&js),如果你能给我看的话,我会非常感激。

如果你能从JavaScript调用Objective-C,那么调用pyobjc编写的Objective-C类或实例应该“管用”

但是,您不能通过PyObjC直接从JavaScript调用Python


你需要展示更多你尝试过的东西。

我能提个建议吗?Javascript和Python在说HTTP方面都做得非常出色。也许在机器上设置一个Python HTTP服务器,当Javascript触发时,它可以对本地服务器执行XHttpRequest


或者。。如果您将QtWebKit与PySide/PyQt一起使用,就有可能获得Javascript和Python聊天(非常粗略)。PyObjC目前的支持非常差,在许多情况下只能“勉强工作”,而在大多数情况下不能。我无意冒犯PyObjC的同事,但公平地说,这个项目只是不像以前那样频繁地更新。

isSelectorExcludedFromWebScript的第二个实现是错误的:

def isSelectorExcludedFromWebScript_(self, aSel):
   if aSel is objc.selector(self.showMessage, signature = 'v@:'):
      return NO
“aSel”参数是Objective-C中的SEL,它是一个选择器名称。这样的方法应该更有效:

def isSelectorExludedFromWebScript_(self, aSel):
    if aSel == b'showMessage':
       return False

    return True
作为一般性提示:当某些Cocoa类记录它在Python异常中忽略的内容时,或者当您预期某个地方可能存在被吞没的Python异常时(确实发生了),您可以要求PyObjC打印有关异常的信息,并将其转换为Objective-C:

import objc
objc.setVerbose(True)
最后:“showMessage”方法应命名为“showMessage”,下划线确保Cocoa方法将命名为“showMessage:”,并具有单个参数(如方法签名所示)

import objc
objc.setVerbose(True)