Note that there are some explanatory texts on larger screens.

plurals
  1. POMacRuby: communicating with Objective C from JavaScript
    primarykey
    data
    text
    <p>I am creating a simple "hello world" application to try to understand how to call Objective-C from JavaScript. My <code>sayHello()</code> and <code>sayGoodBye()</code> methods work, but my <code>speak(text)</code> method does not work. What am I doing wrong? And why do I need to specify <code>@com.respondsToSelector</code> even though I specified <code>self.isSelectorExcludedFromWebScript</code>?</p> <p>page.html</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;h3&gt;Example of calling MacRuby/Objective-C from a web page&lt;/h3&gt; &lt;button onclick="Communicator.sayHello()"&gt;Hello&lt;/button&gt; &lt;button onclick="Communicator.sayGoodBye()"&gt;Good Bye&lt;/button&gt; &lt;button onclick="Communicator.speak('passing an argument')"&gt;Pass an argument&lt;/button&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>app.rb</p> <pre><code>framework 'Cocoa' framework 'WebKit' application = NSApplication.sharedApplication class Communicator def sayHello speak('Hello!') end def sayGoodBye speak('Good Bye!') end def speak(text) puts "Speaking: #{text}" speaker = NSSpeechSynthesizer.alloc.initWithVoice('com.apple.speech.synthesis.voice.Agnes') speaker.startSpeakingString(text) end # Make all the Communicator's methods available from JS def self.isSelectorExcludedFromWebScript(sel); false end end class Browser attr_accessor :view, :windowScriptObject def initialize(window) @view = WebView.new window.contentView = @view @view.frameLoadDelegate = self end def load file_url = NSURL.fileURLWithPath('page.html') html = NSString.stringWithContentsOfURL(file_url) @view.mainFrame.loadHTMLString(html, baseURL:file_url) end def webView(view, didFinishLoadForFrame:frame) @com = Communicator.new #shouldnt need any of these @com.respondsToSelector("sayHello") @com.respondsToSelector("sayGoodBye") @com.respondsToSelector("speak:") # colon at the end of method name because it accepts arguments @windowScriptObject = view.windowScriptObject @windowScriptObject.setValue(@com, forKey: "Communicator") end end window = NSWindow.alloc.initWithContentRect([0.0, 0.0, 700.0, 300.0], # x, y, width, height styleMask: NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask, backing:NSBackingStoreBuffered, defer:false) browser = Browser.new(window).load window.center window.display window.makeKeyAndOrderFront(nil) window.orderFrontRegardless application.run </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload