Is it possible to launch a webview with obb? I have a Swift executable to do it but it would be nice to do it all in Clojure. This is what I have atm.
#!/usr/bin/env obb
(.import js/ObjC "Cocoa")
(.import js/ObjC "WebKit")
(doto (js/$.NSWindow.alloc.initWithContentRectStyleMaskBackingDefer
(js/$.NSMakeRect 0 0 800 600)
(bit-or js/$.NSTitledWindowMask
js/$.NSClosableWindowMask
js/$.NSResizableWindowMask
js/$.NSMiniaturizableWindowMask)
js/$.NSBackingStoreBuffered
false)
(.setTitle "Web View Test")
(.makeKeyAndOrderFront nil))
(let [webView (js/$.WKWebView.alloc.initWithFrame
(js/$.NSMakeRect 0 0 800 600))]
(.addSubview (.contentView (.keyWindow js/$.NSApp)) webView)
(.loadRequest webView (.NSURLRequest.requestWithURL (.NSURL.URLWithString ""))))
(doto js/$.NSApplication.sharedApplication
(.setActivationPolicy js/$.NSApplicationActivationPolicyRegular)
(.activateIgnoringOtherApps true))
(.run js/$.NSApp true) I'm not an expert with the underlying platform but hopefully someone else will be able to help you. Perhaps @zane
Anything you can do in JS should be able to work in obb as well
I haven't tried this in JS yet. I just found Obb last night and am exploring using it to handle the components of my app.
This seems to get "object is not a function":
(.addSubview (.contentView (.keyWindow js/$.NSApp)) webView)minimal repro: (.keyWindow js/$.NSApp)
Cool. yea I have been scoping the issue. I was just asking to see if it's known to work. I am assuming that it will.
This works:
(.import js/ObjC "Cocoa")
(.import js/ObjC "WebKit")
(doto (js/$.NSWindow.alloc.initWithContentRectStyleMaskBackingDefer
(js/$.NSMakeRect 0 0 800 600)
(bit-or js/$.NSTitledWindowMask
js/$.NSClosableWindowMask
js/$.NSResizableWindowMask
js/$.NSMiniaturizableWindowMask)
js/$.NSBackingStoreBuffered
false)
(.setTitle "Web View Test")
(.makeKeyAndOrderFront nil))
(let [webView (js/$.WKWebView.alloc.initWithFrame
(js/$.NSMakeRect 0 0 800 600))]
#_(.addSubview (.contentView (.keyWindow js/$.NSApp)) webView)
(.loadRequest webView (js/$.NSURLRequest.requestWithURL (js/$.NSURL.URLWithString ""))))
(doto js/$.NSApplication.sharedApplication
(.setActivationPolicy js/$.NSApplicationActivationPolicyRegular)
(.activateIgnoringOtherApps true))
(.run js/$.NSApp true) just have to fix the #_ expression
I don't see a contentView thing on KeyWindow https://developer.apple.com/documentation/appkit/nsapplication/1428406-keywindow
It seems that js/$.NSApp is empty
it's not empty if you run
(.import js/ObjC "Cocoa")hmm I have these at the top
(.import js/ObjC "Cocoa")
(.import js/ObjC "WebKit")try (js/console.log js/$.NSApp)
oh I was using println . noted use js/console.log
it is definitely to do with keywindow
It might be a timing issue. How do you sleep in obb?
a better question is how do you sleep in osascript, obb is just a layer on top of that
usually sleeping to solve a timing issue smells like a workaround
if you can sleep at all, usually JavaScript doesn't allow this synchronously
thats a good point
I think there is a way to check the window is loaded. I just wanted to do this as a quick test.
(js/delay 2)
seems to do itthe delay may prevent the window from loading, so I don't know if this helps ;)
haha
Well this doesn't break
#!/usr/bin/env obb
(.import js/ObjC "Cocoa")
(.import js/ObjC "WebKit")
(def window (let [newWindow (js/$.NSWindow.alloc.initWithContentRectStyleMaskBackingDefer
(js/$.NSMakeRect 0 0 800 600)
(bit-or js/$.NSTitledWindowMask
js/$.NSClosableWindowMask
js/$.NSResizableWindowMask
js/$.NSMiniaturizableWindowMask)
js/$.NSBackingStoreBuffered
false)]
(.setTitle newWindow "Web View Test")
(.makeKeyAndOrderFront newWindow nil)
newWindow
#_(js/delay 2)
#_(.addSubview (.contentView newWindow) webView)
#_(.loadRequest webView (js/$.NSURLRequest.requestWithURL (js/$.NSURL.URLWithString "")))))
(let [webview (js/$.WKWebView.alloc.initWithFrame
(js/$.NSMakeRect 0 0 800 600))]
(js/console.log "window" (.-contentView window))
(js/delay 2)
(.addSubview (.-contentView window) webview))
window
(doto js/$.NSApplication.sharedApplication
(.setActivationPolicy js/$.NSApplicationActivationPolicyRegular)
(.activateIgnoringOtherApps true))
(.run js/$.NSApp true) But it looks like we want to use a delegate
I tried creating a delegate and adding it as a method. But it doesn't seem possible to use addMethod.
I also tried it as (js/$.addMethod delegate "windowDidLoad:" #(window-did-load))
I'm also trying #squint now for this:
npx squint webview.cljs && esbuild webview.mjs --bundle --outfile=webview.min.mjs && osascript -l JavaScript webview.min.mjsmight give better error messages / locations (not sure)
if this is figured out and I can package it then I might be able to go without needing Swift which would be nice.
right
What would be the most elegant way to handle if possible using ZeroMQ in obb?
the underlying platform is called osascript, so you might just want to search for that
I am unsure whether what I am doing can be obtained. But I got very close! I shifted to launching the executable built with Swift. But then the application would hang. It might be because Webkit wants to run on the main thread.