Friday, June 18, 2021

#1 2021-06-11 07:22:10 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 810

Webview in modal view.

The benefit to try things and do things little different make you wonder if its possible to put something else in a accessory view. And the only thing I did was to add 2 handlers and created
some new arguments. That is the benefit to have handlers that are like building blocks.

This example use webview...

Ps. Okey admit it was not so simple, I need a way to abort the running modal loop.

Applescript:

use framework "Foundation"
use framework "AppKit"
use scripting additions

property cancelButton : "Cancel"
property returnCode : missing value

on run
   set webView to showAlert given arguments:{title:"WebView", webURL:"https://www.apple.com", message:"My message...", info:"My info...", buttons:{"Cancel", "OK"}}
end run

on showAlert given arguments:arguments
   try
       if my NSThread's isMainThread() as boolean then
           -- if we run in main thread set the arguments to dictionary.
           set arguments to current application's NSDictionary's dictionaryWithDictionary:arguments
           my performAlert:arguments
       else
           my performSelectorOnMainThread:"performAlert:" withObject:arguments waitUntilDone:true
       end if
       
       if button returned of returnCode is cancelButton then error number -128
       return returnCode
   on error error_message number error_number
       tell me to activate
       display alert "" & error_number & "" message error_message
       return returnCode
   end try
end showAlert

on performAlert:arguments
   set theView to createWebView(arguments's webURL, 400, 200)
   set theAlert to createAlert(arguments's title, arguments's message, arguments's info, arguments's buttons, theView)
   
   set button to theAlert's runModal()
   set buttonsArray to (buttons of arguments)'s reverseObjectEnumerator()'s allObjects()
   set button to item (button mod 1000 + 1) of buttonsArray
   set my returnCode to {button returned:button as text}
end performAlert:

on createAlert(title, message, info, buttons, theView)
   set theAlert to current application's NSAlert's alloc()'s init()
   theAlert's |window|'s setTitle:title
   theAlert's setMessageText:message
   theAlert's setInformativeText:info
   
   repeat with anItem in reverse of (buttons as list)
       (theAlert's addButtonWithTitle:anItem)
   end repeat
   
   -- Set the accessoryView to the alert.
   theAlert's setAccessoryView:theView
   return theAlert
end createAlert

on createWebView(theURLString, width, height)
   set theConfiguration to current application's WKWebViewConfiguration's alloc()'s init()
   set theFetch to my fetchJSSourceString(theURLString)
   set theUserScript to current application's WKUserScript's alloc()'s initWithSource:theFetch injectionTime:(current application's WKUserScriptInjectionTimeAtDocumentEnd) forMainFrameOnly:true
   set theUserContentController to current application's WKUserContentController's alloc()'s init()
   theUserContentController's addUserScript:theUserScript
   theConfiguration's setUserContentController:theUserContentController
   
   set webViewSize to current application's NSMakeRect(0, 0, width, height)
   set webView to current application's WKWebView's alloc()'s initWithFrame:webViewSize configuration:theConfiguration
   webView's setNavigationDelegate:me
   webView's setUIDelegate:me
   webView's setTranslatesAutoresizingMaskIntoConstraints:true
   
   set theURL to current application's |NSURL|'s URLWithString:theURLString
   set theRequest to current application's NSURLRequest's requestWithURL:theURL
   webView's loadRequest:theRequest
   return webView
end createWebView

on fetchJSSourceString(theURLString)
   set theURL to current application's |NSURL|'s URLWithString:theURLString
   set theSource to current application's NSString's stringWithContentsOfURL:theURL encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
   return theSource
end fetchJSSourceString

Last edited by Fredrik71 (2021-06-11 07:43:57 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)